Discrete Mathematics and Its Applications, 6th Edition

  • 5 2,728 10
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up

Discrete Mathematics and Its Applications, 6th Edition

LIST OF SYMBOLS TOP IC SYMBOL MEANIN G LOGIC ""'p p 1\ q pvq P (J)q p-+q p ++ q p=.q T F P(xJ,... ,xn) VxP(x) 3xP(x)

3,334 2,193 25MB

Pages 1008 Page size 541.44 x 714.48 pts Year 2009

Report DMCA / Copyright

DOWNLOAD FILE

Recommend Papers

File loading please wait...
Citation preview

LIST OF SYMBOLS TOP IC

SYMBOL

MEANIN G

LOGIC

""'p p 1\ q pvq P (J)q p-+q p ++ q p=.q T F P(xJ,... ,xn) VxP(x) 3xP(x) 3!xP(x)

negation of p conjunction of p and q disjunction of p and q exclusive or of p and q the implication p implies q biconditional of p and q equivalence of p and q tautology contradiction propositional function universal quantification of P(x) existential quantification of P(x) uniqueness quantification of P(x) therefore partial correctness of S

p{S}q

SETS

PAGE 3 4 4

5 6 9 22 24 24 32 34 36 37 63 323

x is a member of S x is not a member of S

U Ai

list of elements of a set set builder notation set of natural numbers set of integers set of positive integers set of rational numbers set of real numbers set equality the empty (or null) set S is a subset of T S is a proper subset of T cardinality of S the power set of S n-tuple ordered pair Cartesian product of A and B union of A and B intersection of A and B the difference of A and B complement of A

1 12 1 12 1 12 1 12 1 12 1 12 1 13 1 13 1 13 1 13 1 14 1 14 1 15 1 16 1 16 1 17 1 17 1 18 12 1 121 1 23 1 23

union of Ai, i= 1 , 2, . . . , n

1 27

n Ai

intersection of Ai, i= 1 , 2, . . . , n

128

A(J)B

symmetric difference of A and B

131

XES x fj S {aJ,... ,an} {x I P(x)} N

Z z+ Q

R S=T 0

SC;T SeT lSI peS) (aJ,... ,an) (a, b) AxB AUB AnB A-B

A n

i=J n

i=J

TOP IC

SYMBOL

MEANIN G

FUNCTIONS

J (a ) J : A ---+ B JI+ h Jd2 J (5) LA (S ) J -I(x ) J og LxJ fx l

value of the function J at a function from A to B sum of the functions J I and J2 product of the functions J I and 12 image of the set 5 under J identity function on A inverse of J composition of J and g floor function of x ceiling function of x term of {ai} with subscript n

1 33 1 33 1 35 135 1 36 138 1 39 1 40 1 43 1 43 1 50

sum of ai, a2, ... , an

1 53

sum of aa over a

1 56

an n Lai i=1 L aa aES n fl an i=!

J (x) is O (g (x » n! J(x) is Q(g(x» J(x) is 8(g (x»

min (x , y) max(x , y) �

PA GE

E

5

product of ai, a2, ... , an

1 62

J (x ) is big-O o f g (x ) n factorial J (x ) is big-Omega of g (x ) J (x ) is big-Theta of g(x) asymptotic minimum of x and y maximum of x and y approximately equal to

1 80 1 85 1 89 1 89 1 92 216 217 395

INTEGERS

alb alb a div b a mod b a == b (mod m ) a =1= b (mod m ) gcd (a , b) 1cm (a, b) (akak-I ... alaO)b

a divides b a does not divide b quotient when a is divided by b remainder when a is divided by b a i s congruent to b modulo m a i s not congruent to b modulo m greatest common divisor of a and b least common multiple of a and b base b representation

20 1 20 1 202 202 203 203 215 217 219

MATRICES

[aij ] A+B AB In At AvB AAB A0B A[n]

matrix with entries aij matrix sum of A and B matrix product of A and B identity matrix of order n transpose of A join of A and B the meet of A and B Boolean product of A and B nth Boolean power of A

247 247 248 25 1 25 1 252 252 253 254

(List of Symbols continued at back of book)

Discrete Mathematics and Its Applications Sixth Edition

Kenneth

H.

Rosen

AT&T Laboratories

Boston Bangkok Milan

Burr Ridge, IL

Bogota

Montreal

Caracas New Delhi

Dubuque, IA

New York

Kuala Lumpur

Lisbon

Santiago

Seoul

San Francisco London

Singapore

St. Louis

Madrid

Sydney

Mexico City

Taipei

Toronto

The

McGraw Hilt Compames

4

DISCRETE MATHEMATICS AND ITS APPLICATIONS, SIXTH EDITION International Edition

2007

Exclusive rights by McGraw-Hill Education (Asia), for manufacture and export. This book cannot be re-exported from the country to which it is sold by McGraw-Hill. The International Edition is not available in North America.

1221

Published by McGraw-Hill, a business unit of The McGraw-Hill Companies, Inc., Avenue of the Americas, New York, NY Copyright by Kenneth H. Rosen. All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written consent of The McGraw-Hill Companies, Inc., including, but not limited to, in any network or other electronic storage or transmission, or broadcast for distance learning. Some ancillaries, including electronic and print components, may not be available to customers outside the United States.

10020.

© 2007

1020 0909 0808 0707 0606 05 04 03 02 01

CTF

BJE

The credits section for this book begins on page C-I and is considered an extension of the copyright page.

When ordering this title, use ISBN-13: 978-007-124474-9 or ISBN-I0: 007-124474-3 Printed in Singapore www.rnhhe.com

Contents Preface vii

T he MathZone Companion Website xviii

To the Student xx

1

The Foundations: Logic and Proofs .................................. 1

1.1 1 .2 1 .3 1 .4 1 .5 1 .6 1 .7

Propositional Logic . . ... .. . 1 Propositional Equivalences . . . 21 Predicates and Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Nested Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Rules of Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Introduction to Proofs . . . . 75 Proof Methods and Strategy . 86 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 04

2

Basic Structures: Sets, Functions, Sequences, and Sums ....... ... 111

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2 . 1 . Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . : . . . . . . . . 1 1 1 2.2 Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 1 2 .3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 3 2 .4 Sequences and Summations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 49 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 63 3

The Fundamentals: Algorithms, the Integers, and Matrices........ 167

3.1 3.2 3.3 3 .4 3.5 3.6 3.7 3.8

Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 67 The Growth of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 80 Complexity of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 93 The Integers and Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Primes and Greatest Common Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1 0 Integers and Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1 9 Applications of Number Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3 1 Matrices 246 End-of-Chapter Material . 257 .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4

Induction and Recursion .......................................... 263

4. 1 4.2 4.3 4.4

Mathematical Induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Strong Induction and Well-Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Recursive Definitions and Structural Induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Recursive Algorithms . .. . . 311 .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

iii

Contents

4.5

5

5. 1 5.2 5.3 5 .4 5.5 5.6

6

6. 1 6.2 6.3 6.4

Program Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Counting

.

.

.

.

.

.

.

.

.

.

.

7. 1 7.2 7.3 7.4 7.5 7.6

8

8. 1 8.2 8.3 8.4 8.5 8.6

9

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

335

.

The Basics of Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 The Pigeonhole Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Permutations and Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Binomial Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Generalized Permutations and Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 Generating Permutations and Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Discrete Probability

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

393

.

An Introduction to Discrete Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Probability Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Bayes' Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1 7 Expected Value and Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 End-of-Chapter Material . . . . 442 .

7

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advanced Counting Techniques

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

449

Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Solving Linear Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 Divide-and-Conquer Algorithms and Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . 474 Generating Functions . . .... . 484 Inclusion-Exclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 Applications of Inclusion-Exclusion . . . ........ ... 505 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1 3 .

Relations

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

519

.

Relations and Their Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1 9 n-ary Relations and Their Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 Representing Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Closures of Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 Equivalence Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 Partial Orderings . . . . . . . .. 566 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 1 Graphs

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

589

.

9. 1 9.2 9.3

Graphs and Graph Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 Graph Terminology and Special Types of Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 Representing Graphs and Graph Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1 1 62 1 9.4 Connectivity .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Contents

v

9.5 9.6 9.7 9 .8

Euler and Hamilton Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 Shortest-Path Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Planar Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657 Graph Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675

10

Trees

1 0. 1 1 0.2 1 0.3 1 0.4 1 0.5

Introduction to Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683 Applications of Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695 Tree TraversaL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1 0 Spanning Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 Minimum Spanning Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743

11

Boolean Algebra

1 1.1 1 1 .2 1 1 .3 1 1 .4

Boolean Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749 Representing Boolean Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757 Logic Gates 760 Minimization of Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 1

12

Modeling Computation

12.1 1 2 .2 1 2 .3 1 2 .4 1 2 .5

Languages and Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785 Finite-State Machines with Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796 Finite-State Machines with No Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804 Language Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1 7 Turing Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 End-of-Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

.

.

.

.

.

.

.

.

.

.

Appendixes

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

749

.

.

.

683

.

785

A-I

A- I Axioms for the Real Numbers and the Positive Integers . . . . . . . . . . . . . . . . . . . . . . . . . . A- I A-2 Exponential and Logarithmic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-7 A-3 Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A- I O Suggested Readings

B-1

Answers to Odd-Numbered Exercises Photo Credits C-l

Index of Biographies 1-1

Index 1-2

S-1

About the Author K enneth H. Rosen has had a long career as a Distinguished Member of the Technical Staff at AT&T Laboratories in Monmouth County, New Jersey. He currently holds the position

of visiting research professor at Monmouth University where he is working on the security and privacy aspects of the Rapid Response Database Project and where he is teaching a course on cryptographic applications. Dr. Rosen received his B.S. in Mathematics from the University of Michigan, Ann Arbor ( 1 972), and his Ph.D. in Mathematics from M.LT. ( 1 976), where he wrote his thesis in the area of number theory under the direction of Harold Stark. Before joining Bell Laboratories in 1 982, he held positions at the University of Colorado, Boulder; The Ohio State University, Columbus; and the University of Maine, Orono, where he was an associate professor of mathematics. While working at AT&T Labs, he taught at Monmouth University, teaching courses in discrete mathematics, coding theory, and data security. Dr. Rosen has published numerous articles in professional journals in the areas of number theory and mathematical modeling. He is the author ofthe textbooks Elementary Number Theory and Its App lications, published by Addison-Wesley and currently in its fifth edition, and Discrete Mathematics and Its App lications, published by McGraw-Hili and currently in its sixth edition. Both of these textbooks have been used extensively at hundreds of universities throughout the world. Discrete Mathematics and Its Applications has sold more than 300,000 copies in its lifetime with translations into Spanish, French, Chinese, and Korean. He is also co-author of UNIX The Complete Reference; UNIX System V Release 4: An Introduction; and Best UNIX Tips Ever, each published by Osborne McGraw-Hili. These books have sold more than 1 50,000 copies with translations into Chinese, German, Spanish, and Italian. Dr. Rosen is also the editor of the Handbook of Discrete and Combinatorial Mathematics, published by CRC Press, and he is the advisory editor of the CRC series of books in discrete mathematics, consisting of more than 30 volumes on different aspects of discrete mathematics, most of which are introduced in this book. He is also interested in integrating mathematical software into the educational and professional environments, and has worked on projects with Waterloo Maple Inc.'s Maple software in both these areas. At Bell Laboratories and AT&T Laboratories, Dr. Rosen worked on a wide range of projects, including operations research studies and product line planning for computers and data commu­ nications equipment. He helped plan AT&T's products and services in the area of multimedia, including video communications, speech recognition and synthesis, and image networking. He evaluated new technology for use by AT&T and did standards work in the area of image network­ ing. He also invented many new services, and holds or has submitted more than 70 patents. One of his more interesting projects involved helping evaluate technology for the AT&T attraction at EPCOT Center.

Preface I discrete mathematics. For the student, my purpose was to present material in a precise, readable manner, with the concepts and techniques of discrete mathematics clearly presented n writing this book, I was guided by my long-standing experience and interest in teaching

and demonstrated. My goal was to show the relevance and practicality of discrete mathematics to students, who are often skeptical. I wanted to give students studying computer science all of the mathematical foundations they need for their future studies. I wanted to give mathematics students an understanding of important mathematical concepts together with a sense of why these concepts are important for applications. And most importantly, I wanted to accomplish these goals without watering down the material. For the instructor, my purpose was to design a flexible, comprehensive teaching tool using proven pedagogical techniques in mathematics. I wanted to provide instructors with a package of materials that they could use to teach discrete mathematics effectively and efficiently in the most appropriate manner for their particular set of students. I hope that I have achieved these goals. I have been extremely gratified by the tremendous success of this text. The many improve­ ments in the sixth edition have been made possible by the feedback and suggestions of a large number of instructors and students at many of the more than 600 schools where this book has been successfully used. There are many enhancements in this edition. The companion website has been substantially enhanced and more closely integrated with the text, providing helpful material to make it easier for students and instructors to achieve their goals. This text is designed for a one- or two-term introductory discrete mathematics course taken by students in a wide variety of majors, including mathematics, computer science, and engineering. College algebra is the only explicit prerequisite, although a certain degree of mathematical maturity is needed to study discrete mathematics in a meaningful way.

Goals of a Discrete Mathematics Course A discrete mathematics course has more than one purpose. Students should learn a particular set of mathematical facts and how to apply them; more importantly, such a course should teach students how to think logically and mathematically. To achieve these goals, this text stresses mathematical reasoning and the different ways problems are solved. Five important themes are interwoven in this text: mathematical reasoning, combinatorial analysis, discrete structures, algorithmic thinking, and applications and modeling. A successful discrete mathematics course should carefully blend and balance all five themes.

1 . Mathematical Reasoning: Students must understand mathematical reasoning in order to read, comprehend, and construct mathematical arguments. This text starts with a discussion of mathematical logic, which serves as the foundation for the subsequent discussions of methods of proof. Both the science and the art of constructing proofs are addressed. The technique of mathematical induction is stressed through many different types of examples of such proofs and a careful explanation of why mathematical induction is a valid proof technique. 2 . Combinatorial Analysis: An important problem-solving skill is the ability to count or enu­ merate objects. The discussion of enumeration in this book begins with the basic techniques of counting. The stress is on performing combinatorial analysis to solve counting problems and analyze algorithms, not on applying formulae. vii

vJli

Preface

3 . Discrete Structures: A course in discrete mathematics should teach students how to work with discrete structures, which are the abstract mathematical structures used to represent discrete objects and relationships between these objects. These discrete structures include sets, permutations, relations, graphs, trees, and finite-state machines.

4. Algorithmic Thinking: Certain classes of problems are solved by the specification of an algorithm. After an algorithm has been described, a computer program can be constructed implementing it. The mathematical portions of this activity, which include the specification of the algorithm, the verification that it works properly, and the analysis ofthe computer memory and time required to perform it, are all covered in this text. Algorithms are described using both English and an easily understood form of pseudocode. 5. Applications and Modeling: Discrete mathematics has applications to almost every conceiv­ able area of study. There are many applications to computer science and data networking in this text, as well as applications to such diverse areas as chemistry, botany, zoology, linguis­ tics, geography, business, and the Internet. These applications are natural and important uses of discrete mathematics and are not contrived. Modeling with discrete mathematics is an extremely important problem-solving skill, which students have the opportunity to develop by constructing their own models in some of the exercises.

Changes in the Sixth Edition The fifth edition of this book has been used successfully at over 600 schools in the United States, dozens of Canadian universities, and at universities throughout Europe, Asia, and Oceania. Although the fifth edition has been an extremely effective text, many instructors, including longtime users, have requested changes designed to make this book more effective. I have devoted a significant amount of time and energy to satisfy these requests and I have worked hard to find my own ways to make the book better. The result is a sixth edition that offers both instructors and students much more than the fifth edition did. Most significantly, an improved organization of topics has been implemented in this sixth edition, making the book a more effective teaching tool. Changes have been implemented that make this book more effective for students who need as much help as possible, as well as for those students who want to be challenged to the maximum degree. Substantial enhancements to . the material devoted to logic, method of proof, and proof strategies are designed to help students master mathematical reasoning. Additional explanations and examples have been added to clarify material where students often have difficulty. New exercises, both routine and challenging, have been inserted into the exercise sets. Highly relevant applications, including many related to the Internet and computer science, have been added. The MathZone companion website has benefited from extensive development activity and now provides tools students can use to master key concepts and explore the world of discrete mathematics. Improved Organization •

The first part of the book has been restructured to present core topics in a more efficient, more effective, and more flexible way.



• •

Coverage of mathematical reasoning and proof is con­ centrated in Chapter 1 , flowing from propositional and predicate logic, to rules of inference, to basic proof techniques, to more advanced proof techniques and proof strategies.



A separate chapter on discrete structures--Chapter 2 in this new edition---covers sets, functions, sequence, and sums. Material on basic number theory, covered in one sec­ tion in the fifth edition, is now covered in two sections, the first on divisibility and congruences and the second on pnmes. The new Chapter 4 is entirely devoted to induction and recursion.

Preface ix Logic •

Coverage of logic has been amplified with key ideas explained in greater depth and with more care.



Conditional statements and De Morgan's laws receive expanded coverage.



The construction of truth tables is introduced earlier and in more detail.



More care is devoted to introducing predicates and quantifiers, as well as to explaining how to use and work with them.



The application of logic to system specifications-a topic of interest to system, hardware, and software engineers-has been expanded.



Material on valid arguments and rules of inference is now presented in a separate section.

Writing and Understanding Proofs •

Proof methods and proof strategies are now treated in separate sections of Chapter 1 .



An appendix listing basic axioms for real numbers and for the integers, and how these axioms are used to prove new results, has been added. The use of these axioms and basic results that follow from them has been made explicit in many proofs in the text.



The process of making conjectures, and then using different proof methods and strategies to attack these

conjectures, is illustrated using the easily accessible topic of tilings of checkerboards. •



Separate and expanded sections on mathematical in­ duction and on strong induction begin the new Chapter 4. These sections include more motivation and a rich collection of examples, providing many examples dif­ ferent than those usually seen. More proofs are displayed in a way that makes it pos­ sible to explicitly list the reason for each step in the proof.

Algorithms and Applications • •

More coverage is devoted to the use of strong induc­ tion to prove that recursive algorithms are correct. How Bayes' Theorem can be used to construct spam filters is now described.



Examples and exercises from computational geometry have been added, including triangulations of polygons.



The application of bipartite graphs to matching prob­ lems has been introduced.

Number Theory, Combinatorics, and Probability Theory •

Coverage of number theory is now more flexible, with four sections covering different aspects of the subject and with coverage of the last three of these sections optional.



The introduction of basic counting techniques, and permutations and combinations, has been enhanced.



Coverage of counting techniques has been expanded; counting the ways in which objects can be distributed in boxes is now covered.



Coverage of probability theory has been expanded with the introduction of a new section on Bayes' Theorem.



Examples illustrating the construction of finite-state automata that recognize specified sets have been added.



Minimization of finite-state machines is now men­ tioned and developed in a series of exercises. Coverage of Turing machines has been expanded with a brief introduction to how Turing machines arise in the study of computational complexity, decidability, and computability.

Graphs and Theory of Computation •

The introduction to graph theory has been streamlined and improved.



A quicker introduction to terminology and applica­ tions is provided, with the stress on making the correct decisions when building a graph model rather than on terminology.



Material on bipartite graphs and their applications has been expanded.



x

Preface

Exercises and Examples •

Many new routine exercises and examples have been added throughout, especially at spots where key con­ cepts are introduced.



Extra effort has been made to ensure that both odd­ numbered and even-numbered exercises are provided for basic concepts and skills.



A better correspondence has been made between examples introducing key concepts and routine exercises.



Many new challenging exercises have been added.



Over 400 new exercises have been added, with more on key concepts, as well as more introducing new topics.

Additional Biographies, Historical Notes, and New Discoveries • •

Biographies have been added for Archimedes, Hopper, Stirling, and Bayes. Many biographies found in the previous edition have been enhanced, including the biography of Augusta Ada.

The MathZone Companion Website



The historical notes included in the main body of the book and in the footnotes have been enhanced.



New discoveries made since the publication ofthe pre­ vious edition have been noted.

(www.mhhe.comlrosen)



MathZone course management and online tutorial sys­ tem now provides homework and testing questions tied directly to the text.



Expanded annotated links to hundreds of Internet resources have been added to the Web Resources Guide.



Additional Extra Examples are now hosted online, covering all chapters of the book. These Extra Examples have benefited from user review and feedback.



Additional Self Assessments of key topics have been added, with 1 4 core topics now addressed.



Existing Interactive Demonstration Applets support­ ing key algorithms are improved. Additional applets have also been developed and additional explanations are given for integrating them with the text and in the classroom.



An updated and expanded Exploring Discrete Mathe­ matics with Maple companion workbook is also hosted online.

Special Features This text has proved to be easily read and understood by beginning students. There are no mathematical prerequisites beyond college algebra for almost all of this text. Students needing extra help will find tools on the MathZone companion website for bringing their mathematical maturity up to the level of the text. The few places in the book where calculus is referred to are explicitly noted. Most students should easily understand the pseudocode used in the text to express algorithms, regardless ofwhether they have formally studied programming languages. There is no formal computer science prerequisite. Each chapter begins at an easily understood and accessible level. Once basic mathematical concepts have been carefully developed, more difficult material and applications to other areas of study are presented. ACCESSIBILITY

This text has been carefully designed for flexible use. The dependence of chapters on previous material has been minimized. Each chapter is divided into sections of approximately the same length, and each section is divided into subsections that form natural blocks of material for teaching. Instructors can easily pace their lectures using these blocks.

FLEXIBILITY

Preface xi WRITING STYLE The writing style in this book is direct and pragmatic. Precise mathe­ matical language is used without excessive formalism and abstraction. Care has been taken to balance the mix of notation and words in mathematical statements. MATHEMATICAL RIGOR AND PRECISION All definitions and theorems in this text are stated extremely carefully so that students will appreciate the precision of language and rigor needed in mathematics. Proofs are motivated and developed slowly; their steps are all carefully justified. The axioms used in proofs and the basic properties that follow from them are explicitly described in an appendix, giving students a clear idea of what they can assume in a proof. Recursive definitions are explained and used extensively. WORKED EXAMPLES Over 750 examples are used to illustrate concepts, relate different topics, and introduce applications. In most examples, a question is first posed, then its solution is presented with the appropriate amount of detail. APPLICATIONS The applications included in this text demonstrate the utility of discrete mathematics in the solution of real-world problems. This text includes applications to a wide va­ riety of areas, including computer science, data networking, psychology, chemistry, engineering, linguistics, biology, business, and the Internet. ALGORITHMS Results in discrete mathematics are often expressed in terms of algo­ rithms; hence, key algorithms are introduced in each chapter of the book. These algorithms are expressed in words and in an easily understood form of structured pseudocode, which is described and specified in Appendix A.3. The computational complexity of the algorithms in the text is also analyzed at an elementary level. HISTORICAL INFORMATION The background of many topics is succinctly described in the text. Brief biographies of more than 65 mathematicians and computer scientists, accom­ panied by photos or images, are included as footnotes. These biographies include information about the lives, careers, and accomplishments of these important contributors to discrete mathe­ matics and images of these contributors are displayed. In addition, numerous historical footnotes are included that supplement the historical information in the main body of the text. Efforts have been made to keep the book up-to-date by reflecting the latest discoveries. KEY TERMS AND RESULTS A list of key terms and results follows each chapter. The key terms include only the most important that students should learn, not every term defined in the chapter. EXERCISES There are over 3800 exercises in the text, with many different types of ques­ tions posed. There is an ample supply of straightforward exercises that develop basic skills, a large number of intermediate exercises, and many challenging exercises. Exercises are stated clearly and unambiguously, and all are carefully graded for level of difficulty. Exercise sets contain special discussions that develop new concepts not covered in the text, enabling students to discover new ideas through their own work. Exercises that are somewhat more difficult than average are marked with a single star *; those that are much more challenging are marked with two stars **. Exercises whose solutions require calculus are explicitly noted. Exercises that develop results used in the text are clearly identified with the symbol G>. Answers or outlined solutions to all odd-numbered exercises are provided at the back of the text. The solutions include proofs in which most of the steps are clearly spelled out. REVIEW QUESTIONS A set of review questions is provided at the end of each chapter. These questions are designed to help students focus their study on the most important concepts

Iii

Preface

and techniques of that chapter. To answer these questions students need to write long answers, rather than just perform calculations or give short replies. SUPPLEMENTARY EXERCISE SETS Each chapter is followed by a rich and varied set of supplementary exercises. These exercises are generally more difficult than those in the exercise sets following the sections. The supplementary exercises reinforce the concepts of the chapter and integrate different topics more effectively. COMPUTER PROJECTS Each chapter is followed by a set of computer projects. The approximately 1 50 computer projects tie together what students may have learned in computing and in discrete mathematics. Computer projects that are more difficult than average, from both a mathematical and a programming point of view, are marked with a star, and those that are extremely challenging are marked with two stars. COMPUTATIONS AND EXPLORATIONS A set of computations and explorations is included at the conclusion of each chapter. These exercises (approximately 1 00 in total) are designed to be completed using existing software tools, such as programs that students or instructors have written or mathematical computation packages such as Maple or Mathematica. Many of these exercises give students the opportunity to uncover new facts and ideas through computation. (Some of these exercises are discussed in the Exploring Discrete Mathematics with Maple companion workbook available online.) WRITING PROJECTS Each chapter is followed by a set of writing projects. To do these projects students need to consult the mathematical literature. Some of these projects are historical in nature and may involve looking up original sources. Others are designed to serve as gateways to new topics and ideas. All are designed to expose students to ideas not covered in depth in the text. These projects tie mathematical concepts together with the writing process and help expose students to possible areas for future study. (Suggested references for these projects can be found online or in the printed Student's Solutions Guide.) APPENDIXES There are three appendixes to the text. The first introduces axioms for real numbers and the integers, and illustrates how facts are proved directly from these axioms. The second covers exponential and logarithmic functions, reviewing some basic material used heavily in the course. The third specifies the pseudocode used to describe algorithms in this text. SUGGESTED READINGS A list of suggested readings for each chapter is provided in a section at the end of the text. These suggested readings include books at or below the level of this text, more difficult books, expository articles, and articles in which discoveries in discrete mathematics were originally published. Some of these publications are classics, published many years ago, while others have been published within the last few years.

How to Use This Book This text has been carefully written and constructed to support discrete mathematics courses at several levels and with differing foci. The following table identifies the core and optional sections. An introductory one-term course in discrete mathematics at the sophomore level can be based on the core sections of the text, with other sections covered at the discretion of the instructor. A two-term introductory course can include all the optional mathematics sections in addition to the core sections. A course with a strong computer science emphasis can be taught by covering some or all of the optional computer science sections.

Preface xiii Optional Computer Science Sections

Chapter

Core Sections

1 2 3 4 5 6 7 8 9 10 11 12

1 . 1-1 .7 (as needed) 2 . 1-2 .4 (as needed) 3 . 1-3.5, 3 . 8 (as needed) 4. 1--4.3 5 . 1-5.3 6. 1 7. 1 , 7.5 8. 1 , 8.3, 8.5 9. 1-9.5 1 0. 1

Optional Mathematics Sections

3.6 4.4, 4.5 5.6 6.4 7.3 8.2

3 .7 5 .4, 5.5 6.2, 6.3 7.2, 7.4, 7.6 8.4, 8.6 9.6-9.8 1 0.4, 1 0.5

1 0.2, 1 0.3 1 1 . 1-1 1 .4 1 2 . 1- 1 2 . 5

Instructors using this book can adjust the level o f difficulty o f their course b y choosing either to cover or to omit the more challenging examples at the end of sections, as well as the more challenging exercises. The dependence of chapters on earlier chapters is shown in the following chart. Chapter I

I

Chapter 2

I

Chapter 3

I

Chapter 4

I

Chapter 5

---� I� 8 9 11

Chapler 6

Chapler 7

Chapler

Chapter

Chapter

Chapter 12

I

Chapter 10

Ancillaries STUDENT'S SOLUTIONS GUIDE This student manual, available separately, contains full solutions to all odd-numbered problems in the exercise sets. These solutions explain why a particular method is used and why it works. For some exercises, one or two other possible approaches are described to show that a problem can be solved in several different ways. Suggested references for the writing projects found at the end of each chapter are also included in this volume. Also included are a guide to writing proofs and an extensive description of common mistakes students make in discrete mathematics, plus sample tests and a sample crib sheet for each chapter designed to help students prepare for exams.

(ISBN-lO: 0-07-310779-4)

(ISBN-J3: 978-0-07-310779-0)

INSTRUCTOR'S RESOURCE GUIDE This manual, available by request for instructors, contains full solutions to even-numbered exercises in the text. Suggestions on how to teach the material in each chapter of the book are provided, including the points to stress in each section and how to put the material into perspective. It also offers sample tests for each chapter and a test

xiv

Preface bank containing over 1 300 exam questions to choose from. Answers to all sample tests and test bank questions are included. Finally, several sample syllabi are presented for courses with differ­ ing emphasis and student ability levels, and a complete section and exercise migration guide is included to help users of the fifth edition update their course materials to match the sixth edition. (ISBN-10: 0-07-310781-6)

(ISBN-13: 978-0-07-310781-3)

INSTRUCTOR'S TESTING AND RESOURCE CD An extensive test bank of more than 1 300 questions using Brownstone Diploma testing software is available by request for use on Windows or Macintosh systems. Instructors can use this software to create their own tests by selecting questions of their choice or by random selection. They can also sort questions by section, difficulty level, and type; edit existing questions or add their own; add their own headings and instructions; print scrambled versions of the same test; export tests to word processors or the Web; and create and manage course grade books. A printed version of this test bank, including the questions and their answers, is included in the Instructor s Resource

Guide.

(ISBN-1O: 0-07-310782-4)

(ISBN-13: 978-0-07-310782-0)

Acknowledgments I would like to thank the many instructors and students at a variety of schools who have used this book and provided me with their valuable feedback and helpful suggestions. Their input has made this a much better book than it would have been otherwise. I especially want to thank Jerrold Grossman, John Michaels, and George Bergman for their technical reviews of the sixth edition and their "eagle eyes," which have helped ensure the accuracy of this book. I also appreciate the help provided by all those who have submitted comments via the website. I thank the reviewers of this sixth and the five previous editions. These reviewers have provided much helpful criticism and encouragement to me. I hope this edition lives up to their high expectations. Reviewers for the Sixth Edition

Charles Ashbacher, Mount Mercy College

Greg Cameron, Brigham Young University

Beverly Diamond, College o/Charleston

Ian Barlami, Rice University

John Carter, University o/ Toronto

Thomas Dunion, Atlantic Union College

George Bergman, University o/California, Berkeley

Greg Chapman, Cosumnes River College

Bruce Elenbogen, University 0/Michigan, Dearborn

David Berman, University 0/ North Carolina, Wilmington

Chao-Kun Cheng, Virginia Commonwealth University

Herbert Enderton, University o/ California, Los Angeles

Miklos Bona, University 0/ Florida

Thomas Cooper, Georgia Perimeter College, Lawrenceville

Anthony Evans, Wright State University

Prosenjit Bose, Carleton University

Barbara Cortzen, DePaul University

Kim Factor, Marquette University

Kirby Brown,

Daniel Cunningham , Buffalo State College

William Farmer, McMaster University

George Davis, University 0/ Georgia

Li Feng, Albany State University

Polytechnic

University

Michael Button,

The Master s College

Preface xv Stephanie Fitchett, Florida Atlantic University

Jonathan Knappenberger, LaSalle University

Henry Ricardo, Medgar Evers College

Jerry Fluharty, Eastern Shore Community College

EdKorntved, Northwest Nazarene University

Oskars Rieksts, Kutztown University

Joel Fowler, Southern Polytechnic State University

Przemo Kranz, University ofMississippi

Stefan Robila, Montclair State University

Loredana Lanzani, University ofArkansas

Chris Rodger, Auburn University

Yoonjin Lee, Smith College

Robert Rodman, North Carolina State University

Miguel Lerma, Northwestern University

Shai Simonson, Stonehill College

Jason Levy, University ofHawaii

Barbara Smith, Cochise College

Lauren Lilly, Tabor College

Wasin So, San Jose State University

Ekaterina Lioutikova, Saint Joseph College

Diana Staats, Dutchess Community College

V ladimir Logvinenko, De Anza College

Lorna Stewart, University ofAlberta

Joan Lukas, University ofMassachusetts, Boston

Bogdan Suceava, California State University, Fullerton

William Gasarch, University ofMaryland Sudpito Ghosh, Colorado State University Jerrold Grossman, Oakland University Ruth Haas, Smith College Patricia Hammer, Hollins University Keith Harrow, Brooklyn College Bert Hartnell, St. Mary s University Julia Hassett, Oakton College Kavita Hatwal, Portland Community College John Helm, Radford University Arthur Hobbs, Texas A&M University Fannie Howell, Roanoke High School Wei Hu, Houghton College Nan Jiang, University ofSouth Dakota Margaret Johnson, Stariford University Martin Jones, College of Charleston

Lester McCann, University ofArizona Jennifer McNulty, University ofMontana John G. Michaels, SUNY Brockport Michael Oppedisano, Morrisville State College Michael O'Sullivan, San Diego State University Charles Parry, Virginia Polytechnic Institute Linda Powers, Virginia Polytechnic Institute Dan Pritikin, Miami University

Kathleen Sullivan, Seattle University Laszlo Szekely, University ofSouth Carolina Daniel Tauritz, University ofMissouri, Rolla Don VanderJagt, Grand Valley State University Fran Vasko, Kutztown University Susan Wallace, University ofNorth Florida Zhenyuan Wang, University ofNebraska, Omaha

Anthony Quas, University ofMemphis

Tom Wineinger, University of Wisconsin, Eau Claire

Eric Rawdon, Duquesne University

Charlotte Young, South Plains College

Eric Allender, Rutgers University

Zhaojun Bai, University ofCalifornia, Davis

Alfred E. Borm, Southwest Texas State University

Stephen Andrilli, La Salle University

Jack R. Barone, Baruch College

Kendall Atkinson, University ofIowa, Iowa City

Klaus Bichteler, University of Texas, Austin

TimKearns, California Polytechnic State University, San Luis Obispo Reviewers of Previous Editions

Ken W Bosworth, University ofMaryland Lois Brady, California Polytechnic State University, San Luis Obispo

xvi

Preface

Scott Buffett, University ofNew Brunswick

David Jonah, Wayne State University

HoKuen Ng, San Jose State University

Russell Campbell, University ofNorthern Iowa

Akihiro Kanamori, Boston University

Timothy S. Norfolk, Sr. University ofAkron

E. Rodney Canfield, University of Georgia

W ThomasKiley, George Mason University

Truc T. Nguyen, Bowling Green State University

Kevin Carolan, Marist College

TakashiKimura, Boston University

George Novacky, University ofPittsburgh

Tim Carroll, Bloomsburg University

NancyKinnersley, University ofKansas

Jeffrey Nunemacher, Ohio Wesleyan University

Kit C. Chan, Bowling Green State University

Gary Klatt, University of Wisconsin

Allan C. Cochran, University ofArkansas

NicholasKrier, Colorado State University

Peter Collinge, Monroe Community College

Lawrence S. Kroll, San Francisco State University

Ron Davis, Millersville University

Shui F. Lam, California State University, Long Beach

Jaroslav Opatmy, Concordia University Jonathan Pakianathan, University ofRochester Thomas W Parsons, Hofstra University Mary K. Prisco, University of Wisconsin, Green Bay Halina Przymusinska, California Polytechnic State University, Pomona

Nachum Dershowitz, University ofIllinois, Urbana-Champaign

Robert Lavelle, lona College

Thomas Dowling, The Ohio State University

Harbir Lamba, George Mason University

Patrick C. Fischer, Vanderbilt University

Sheau Dong Lang, University of Central Florida

Jane Fritz, University ofNew Brunswick

Cary Lee, Grossmont Community College

Ladnor Geissinger, University ofNorth Carolina

Yi- Hsin Liu, University ofNebraska, Omaha

Jonathan Goldstine, Pennsylvania State University

Stephen C. Locke, Florida Atlantic University

Paul Gormley, Villanova University

George Luger, University ofNew Mexico

Brian Gray, Howard Community College

David S. McAllister, North Carolina State University

Jonathan Gross, Columbia University

Robert McGuigan, Wesifield State College

Laxmi N. Gupta, Rochester Institute of Technology

Michael Maller, Queens College

Michael 1. Schlosser, The Ohio State University

Daniel Gusfield, University of California, Davis

Ernie Manes, University ofMassachusetts

Steven R. Seidel, Michigan Technological University

David F. Hayes, San Jose State University

Francis Masat, Glassboro State College

Douglas Shier, Clemson University

Xin He, SUNY at Buffalo

1.

Alistair Sinclair, University of California, Berkeley

Donald Hutchison, Clackamas Community College

Thomas D. Morley, Georgia Institute of Technology

Carl H. Smith, University ofMaryland

Kenneth Johnson, North Dakota State University

D. R. Morrison, University ofNew Mexico

Hunter S. Snevily, University ofIdaho

M. Metzger, University ofNorth Dakota

Don Reichman, Mercer County Community College Harold Reiter, University ofNorth Carolina Adrian Riskin, Northern Arizona State University Amy L. Rocha, San Jose State University Janet Roll, University ofFindlay Matthew 1. Saltzman, Clemson University Alyssa Sankey, Slippery Rock University Dinesh Sarvate, College of Charleston

Preface

xvii

Daniel Somerville, University ofMassachusetts, Boston

Philip D. Tiu, Oglethorpe University

Thomas Upson, Rochester Institute of Technology

Patrick Tantalo, University of California, Santa Cruz

Lisa Townsley-Kulich, Illinois Benedictine College

Roman Voronka, New Jersey Institute of Technology

Bharti Temkin, Texas Tech University

George Trapp, West Virginia University

James Walker, University ofSouth Carolina

Wallace Terwilligen, Bowling Green State University

David S. Tucker, Midwestern State University

Anthony S. Wojcik, Michigan State University

I would like to thank the staff of McGraw-Hill for their strong support of this project. In particular, thanks go to LizHaefele, Publisher, for her backing; to Liz Covello, Senior Sponsoring Editor, for her advocacy and enthusiasm; and to Dan Seibert, Developmental Editor, for his dedication and attention. I would also like to thank the original editor, Wayne Yuhasz, whose insights and skills helped ensure the book's success, as well as all the other previous editors of this book. To the staff involved in the production of this sixth edition, I offer my thanks to Peggy Selle, Lead Project Manager; Michelle W hitaker, Senior Designer; JeffHuettman, Lead Media Producer; Sandy Schnee, Senior Media Project Manager; Melissa Leick, Supplements Coordi­ nator; and Kelly Brown, Executive Marketing Manager. I am also grateful to Jerry Grossman, who checked the entire manuscript for accuracy; Rose Kramer and Gayle Casel, the technical proofreaders; Pat Steele, the manuscript copyeditor; and Georgia Mederer, who checked the accuracy of the solutions in the Student's Solutions Guide and Instructor's Resource Guide.

Kenneth H. Rosen

The MathZone Companion Website MathZon�

'",

T he extensive companion website accompanying this text has been substantially enhanced lL for the sixth edition and is now integrated with MathZone, McGraw- Hill 's customizable Web-based course management system. Instructors teaching from the text can now use Math­ Zone to administer online homework, quizzing, testing, and student assessment through sets of questions tied to the text which are automatical ly graded and accessible in an integrated, exportable grade book. MathZone also provides a wide variety of interactive student tutorials, including practice problems with step-by-step solutions; Interactive Demonstration applets; and NetTutor, a live, personalized tutoring service. This website is accessible at www.mathzone . com by choosing thi s text off the pull-down menu, or by navigating to www.mhhe. com/rosen. The homepage shows the Information Center, and contains login links for the site 's Student Edition and Instructor Edition. Key features of each area are described below:

T H E I N FO R M AT I O N C E NT E R The Information Center contains basic information about the book including the Table of Contents, Preface, descriptions of the Ancil laries, and a sample chapter. Instructors can also learn more about MathZone 's key features, and learn about custom publishing options for the book. M ATHZO N E STUDENT E DI T I ON The Student Edition contains a wealth of resources available for student use, including the following, tied into the text wherever the special icons below are shown in the text: a

E xtra Exam ples You can find a large number of additional examples on the site, covering all chapters of the book. These examples are concentrated in areas where students often ask for additional material. Although most of these examples amplify the basic concepts, more-challenging examples can also be found here.

a

I n teractive Demon stration Applets These applets enable you to interactively explore how important algorithms work, and are tied directly to material in the text such as examples and exercises. Additional resources are provided on how to use and apply these applets.

a

Self Assessments These interactive guides help you assess your understanding of 1 4 key concepts, providing a question bank where each question includes a brief tutorial followed by a multiple-choice question. If you select an incorrect answer, advice is provided to help you understand your error. Using these Self Assessments, you should be able to diagnose your problems and focus on available remedies.

a

Web Resources G uide This guide provides annotated links to hundreds of external websites containing relevant material such as historical and biographical information, puzzles and problems, discussions, applets, programs, and more . These links are keyed to the text by page number. Additional resources in the MathZone Student Edition include :

a

xviii

Exploring Discrete Mathematics with Maple This ancillary provides help for using the Maple computer algebra system to do a wide range of computations in discrete mathematics. Each chapter provides a description of relevant Maple functions and how they are used, Mapl e programs to carry out computations in di screte mathematics, examples, and exercises that can be worked using Maple.

The MathZone Companion Website

xix



Applications of Discrete Mathematics



A Guide to Proof- Writing



Common Mistakes in Discrete Mathematics This guide includes a detailed list of com­ mon misconceptions that students of discrete mathematics often have and the kinds of errors they tend to make. You are encouraged to review this list from time to time to help avoid these common traps. (Also available in the Student's Solutions Guide.)



Advice on Writing Projects



NetTutor™

This ancillary contains 24 chapters--each with its own set of exercises-presenting a wide variety of interesting and important applications covering three general areas in discrete mathematics: discrete structures, combinatorics, and graph theory. These applications are ideal for supplementing the text or for independent study. This guide provides additional help for writing proofs, a skill that many students find difficult to master. By reading this guide at the beginning of the course and periodically thereafter when proof writing is required, you will be rewarded as your proof-writing ability grows. (Also available in the Student's Solutions Guide.)

This guide offers helpful hints and suggestions for the W rit­ ing Projects in the text, including an extensive bibliography of helpful books and articles for research; discussion of various resources available in print and online; tips on doing library re­ search; and suggestions on how to write well. (Also available in the Student's Solutions Guide.)

NetTutor is a live, Web-based service that provides online tutorial help. Students can ask questions relating to this text and receive answers in real- time during regular, scheduled hours or ask questions and later receive answers.

This part of the website provides access to all of the resources in the MathZone Student Edition, as well as the following resources for instructors:

MATHZONE INSTRUCTOR EDITION •

Course Management Using MathZone's course management capabilities instructors can create and share courses and assignments with colleagues; automatically assign and grade homework, quiz, and test questions from a bank of questions tied directly to the text; create and edit their own questions or import their own course content; manage course announcements and due dates; and track student progress.



Instructor's Resource Library



Suggested Syllabi



Migration Guide



Teaching Suggestions This guide contains detailed teaching suggestions for instructors, including chapter overviews for the entire text, detailed remarks on each section, and com­ ments on the exercise sets.



Printable Tests

Printable tests are offered in Word and PDF format for every chapter, and are ideal for in-class exams. These tests were created from the Brownstone Diploma electronic test bank available separately by request on the Instructor's Testing a nd Resource CD.



Image Bank PowerPoints Downloadable PowerPoints files are offered containing images of all Figures and Tables from the text. These image banks are ideal for preparing instructor class materials customized to the text.

Collected here are a wide variety of additional instructor teaching resources, created by our wide community of users. These resources include lecture notes, classroom presentation materials such as transparency masters and PowerPoint slides, and supplementary reading and assignments. We will periodically update and add to this library, and encourage users to contact us with their materials and ideas for possible future additions. Several detailed course outlines are shown, offering suggestions for courses with different emphases and different student backgrounds and ability levels.

This guide provides instructors with a complete correlation of all sections and exercises from the fifth edition to the sixth edition, and is very useful for instructors migrating their syllabi to the sixth edition.

To the Student

W

hat is discrete mathematics? Discrete mathematics is the part of mathematics devoted to the study of discrete objects. (Here discrete means consisting of distinct or unconnected elements.) The kinds of problems solved using discrete mathematics include: a

How many ways are there to choose a valid password on a computer system?

a

W hat is the probability of winning a lottery?

a

Is there a link between two computers in a network?

a

How can I identify spam e- mail messages?

a

How can I encrypt a message so that no unintended recipient can read it?

a

W hat is the shortest path between two cities using a transportation system?

a

How can a list of integers be sorted so that the integers are in increasing order?

a

How many steps are required to do such a sorting?

a

How can it be proved that a sorting algorithm correctly sorts a list?

a

How can a circuit that adds two integers be designed?

a

How many valid Internet addresses are there?

You wi11 learn the discrete structures and techniques needed to solve problems such as these. More generally, discrete mathematics is used whenever objects are counted, when relation­ ships between finite (or countable) sets are studied, and when processes involving a finite number of steps are analyzed. A key reason for the growth in the importance of discrete mathematics is that information is stored and manipulated by computing machines in a discrete fashion. There are several important reasons for studying discrete mathematics. First, through this course you can develop your mathematical maturity : that is, your ability to understand and create mathematical arguments. You will not get very far in your studies in the mathematical sciences without these skills. Second, discrete mathematics is the gateway to more advanced courses in all parts of the mathematical sciences. Discrete mathematics provides the mathematical foundations for many computer science courses including data structures, algorithms, database theory, automata theory, formal languages, compiler theory, computer security, and operating systems. Students find these courses much more difficult when they have not had the appropriate mathematical foundations from discrete math. One student has sent me an e- mail message saying that she used the contents of this book in every computer science course she took ! Math courses based on the material studied in discrete mathematics include logic, set theory, number theory, linear algebra, abstract algebra, combinatorics, graph theory, and probability theory (the discrete part of the subject). Also, discrete mathematics contains the necessary mathematical background for solving problems in operations research (including many discrete optimization techniques), chemistry, engineering, biology, and so on. In the text, we will study applications to some of these areas. Many students find their introductory discrete mathematics course to be significantly more challenging than courses they have previously taken. One reason for this is that one of the primary goals of this course is to teach mathematical reasoning and problem solving, rather than a discrete set of skills. The exercises in this book are designed to reflect this goal. Although there are plenty of exercises in this text similar to those addressed in the examples, a large

WHY STUDY DISCRETE MATHEMATICS?

xx

To the Student

xxi

percentage of the exercises require original thought. This is intentional. The material discussed in the text provides the tools needed to solve these exercises, but your job is to successfully apply these tools using your own creativity. One of the primary goals of this course is to learn how to attack problems that may be somewhat different from any you may have previously seen. Unfortunately, learning how to solve only particular types of exercises is not sufficient for success in developing the problem-solving skills needed in subsequent courses and professional work. This text addresses many different topics, but discrete mathematics is an extremely diverse and large area of study. One of my goals as an author is to help you develop the skills needed to master the additional material you will need in your own future pursuits. I would like to offer some advice about how you can best learn discrete mathematics (and other subjects in the mathematical and computing sciences). You will learn the most by actively working exercises. I suggest that you solve as many as you possibly can. After working the exercises your instructor has assigned, I encourage you to solve additional exercises such as those in the exercise sets following each section of the text and in the supplementary exercises at the end of each chapter. (Note the key explaining the markings preceding exercises.)

THE EXERCISES

Key to the E xercises No marking

A routine exercise

*

A difficult exercise

**

An extremely challenging exercise An exercise containing a result used in the book (The Table below shows where each of these exercises are used.)

(Requires calculus)

An exercise whose solution requires the use of limits or concepts from differential or integral calculus

Hand-Icon Exercises and Where They Are Used Section

Exercise

Section Where Used

Page Where Used

1 .2 1 .6 2.3 3.1 3.2 3.7 4. 1 4.2 4.3 4.3 5.4 5.4 6.2 8.1 9.4 10. 1 10. 1 10. 1

42

1 1 .2 1 .6 2.4 3.1 10.2 6.2 4. 1 4.2 4.3 4.3

758 81 158 1 74 700 412 270 287 296 297 414 428 414 545 685 688 693 700 763 477

I Ll

A.2

81 75a 43 62 30 79 28 56 57 17 21 15 24 49 15 30 48 12 4

6.2 6.4 6.2 8.4 10. 1 10. 1 10. 1 10.2 1 1 .3 7.3

xxii

To the Student The best approach is to try exercises yourself before you consult the answer section at the end of this book. Note that the odd- numbered exercise answers provided in the text are answers only and not full solutions; in particular, the reasoning required to obtain answers is omitted in these answers. The Student 50 Solutions Guide, available separately, provides complete, worked solutions to all odd- numbered exercises in this text. When you hit an impasse trying to solve an odd- numbered exercise, I suggest you consult the Student 50 Solutions Guide and look for some guidance as to how to solve the problem. The more work you do yourself rather than passively reading or copying solutions, the more you will learn. The answers and solutions to the even­ numbered exercises are intentionally not available from the publisher; ask your instructor if you have trouble with these. You are strongly encouraged to take advantage of additional re­ sources available on the Web, especially those on the MathZone companion website for this book found at www.mhhe.com/rosen. You will find many Extra Examples designed to clar­ ify key concepts; Self Assessments for gauging how well you understand core topics; Interactive Demonstration Applets exploring key algorithms and other concepts; a Web Resources Guide containing an extensive selection of links to external sites relevant to the world of discrete mathematics; extra explanations and practice to help you master core concepts; added instruc­ tion on writing proofs and on avoiding common mistakes in discrete mathematics; in- depth discussions of important applications; and guidance on utilizing Maple software to explore the computational aspects of discrete mathematics. Places in the text where these additional online resources are available are identified in the margins by special icons. You will also find NetTutor, an online tutorial service that you can use to receive help from tutors either via real- time chat or via messages. For more details on these online resources, see the description of the MathZone companion website immediately preceding this "To the Student" message.

WEB RESOURCES

THE VALUE OF THIS BOOK My intention is to make your investment in this text an excellent value. The book, the associated ancillaries, and MathZone companion website have taken many years of effort to develop and refine. I am confident that most of you will find that the text and associated materials will help you master discrete mathematics. Even though it is likely that you will not cover some chapters in your current course, you should find it helpful­ as many other students have-to read the relevant sections of the book as you take additional courses. Most of you will return to this book as a useful tool throughout your future studies, especially for those of you who continue in computer science, mathematics, and engineering. I have designed this book to be a gateway for future studies and explorations, and I wish you luck as you begin your journey.

Kenneth H. Rosen

C H A P T E R

The Foundations : Logic and Proofs 1.1 Propositional Logic

1.2 Propositional Equivalences

1.3 Predicates and Quantifiers

1.4 Nested

Quantifiers

1.5 Rules of

Inference

1.6 Introduction to Proofs

1.7 Proof Methods and Strategy

1.1

T he rules of logic specify the meaning of mathematical statements. For instance, these rules 11. help us understand and reason with statements such as "There exists an integer that is not

the sum of two squares" and "For every positive integer n , the sum of the positive integers not exceeding n is n (n + 1 )/2." Logic is the basis of all mathematical reasoning, and of all automated reasoning. It has practical applications to the design of computing machines, to the specification of systems, to artificial intelligence, to computer programming, to programming languages, and to other areas of computer science, as well as to many other fields of study. To understand mathematics, we must understand what makes up a correct mathematical argument, that is, a proof. Once we prove a mathematical statement is true, we call it a theorem. A collection oftheorems on a topic organize what we know about this topic. To learn a mathematical topic, a person needs to actively construct mathematical arguments on this topic, and not j ust read exposition. Moreover, because knowing the proof of a theorem often makes it possible to modify the result to fit new situations, proofs play an essential role in the development of new ideas. Students of computer science often find it surprising how important proofs are in computer science. In fact, proofs play essential roles when we verify that computer programs produce the correct output for all possible input values, when we show that algorithms always produce the correct result, when we establish the security of a system, and when we create artificial intelligence. Automated reasoning systems have been constructed that allow computers to construct their own proofs. In this chapter, we will explain what makes up a correct mathematical argument and intro­ duce tools to construct these arguments. We will develop an arsenal of different proof methods that will enable us to prove many different types of results. After introducing many different methods of proof, we will introduce some strategy for constructing proofs. We will introduce the notion of a conjecture and explain the process of developing mathematics by studying conj ectures.

Propositional Lo g ic Introduction The rules of logic give precise meaning to mathematical statements. These rules are used to distinguish between valid and invalid mathematical arguments. Because a maj or goal ofthis book is to teach the reader how to understand and how to construct correct mathematical arguments, we begin our study of discrete mathematics with an introduction to logic. In addition to its importance in understanding mathematical reasoning, logic has numerous applications in computer science. These rules are used in the design of computer circuits, the construction of computer programs, the verification of the correctness of programs, and in many other ways. Furthermore, software systems have been developed for constructing proofs automatically. We will discuss these applications of logic in the upcoming chapters.

Pro p ositions

I-I

Our discussion begins with an introduction to the basic building blocks of logic-propositions. A proposition is a declarative sentence (that is, a sentence that declares a fact) that is either true or false, but not both.

2

1 -2

1 / The Foundations: Logic and Proofs

EXAMPLE l

Exam= �

All the following declarative sentences are propositions.

1 . Washington, D.C., is the capital of the United States of America. 2 . Toronto is the capital of Canada. 3. 1 + 1 = 2 . 4. 2 + 2 = 3 . Propositions

1

and

3

are true, whereas

2

and 4 are false.

Some sentences that are not propositions are given in Example

EXAMPLE 2

2.

Consider the following sentences.

1. 2. 3. 4.

What time is it? Read this carefully.

x + 1 = 2. x + y = Z.

Sentences 1 and 2 are not propositions because they are not declarative sentences. Sentences 3 and 4 are not propositions because they are neither true nor false. Note that each of sentences 3 and 4 can be turned into a proposition if we assign values to the variables. We will also discuss � other ways to turn sentences such as these into propositions in Section 1 .3 . We use letters to denote propositional variables (or statement variables), that is, variables that represent propositions, just as letters are used to denote numerical variables. The conven­ tional letters used for propositional variables are p , q , r, S , . The truth value of a proposition is true, denoted by T, if it is a true proposition and false, denoted by F, if it is a false proposition. The area of logic that deals with propositions is called the propositional calculus or propo­ sitional logic. It was first developed systematically by the Greek philosopher Aristotle more than 2 300 years ago. .

.

.

unkS � ARISTOTLE (384 B.C.E.-322 B.C.E.) Aristotle was born in Stagirus (Stagira) in northern Greece. His father was the personal physician of the King of Macedonia. Because his father died when Aristotle was young, Aristotle could not follow the custom of following his father's profession. Aristotle became an orphan at a young age when his mother also died. His guardian who raised him taught him poetry, rhetoric, and Greek. At the age of 1 7, his guardian sent him to Athens to further his education. Aristotle joined Plato's Academy where for 20 years he attended Plato's lectures, later presenting his own lectures on rhetoric. When Plato died in 347 B.C. E., Aristotle was not chosen to succeed him because his views differed too much from those of Plato. Instead, Aristotle joined the court of King Hermeas where he remained for three years, and married the niece of the King. When the Persians defeated Hermeas, Aristotle moved to Mytilene and, at the invitation of King Philip of Macedonia, he tutored Alexander, Philip's son, who later became Alexander the Great. Aristotle tutored Alexander for five years and after the death of King Philip, he returned to Athens and set up his own school, called the Lyceum. Aristotle's followers were called the peripatetics, which means "to walk about;' because Aristotle often walked around as he discussed philosophical questions. Aristotle taught at the Lyceum for 1 3 years where he lectured to his advanced students in the morning and gave popular lectures to a broad audience in the evening. When Alexander the Great died in 323 B.C.E., a backlash against anything related to Alexander led to trumped-up charges of impiety against Aristotle. Aristotle fled to Chalcis to avoid prosecution. He only lived one year in Chalcis, dying of a stomach ailment in 322 B.C.E. Aristotle wrote three types of works: those written for a popular audience, compilations of scientific facts, and systematic treatises. The systematic treatises included works on logic, philosophy, psychology, physics, and natural history. Aristotle's writings were preserved by a student and were hidden in a vault where a wealthy book collector discovered them about 200 years later. They were taken to Rome, where they were studied by scholars and issued in new editions, preserving them for posterity.

1 . 1 Propositional Logic 3

1-3

unkS �

DEFINITION 1

We now turn our attention to methods for producing new propositions from those that we already have. These methods were discussed by the English mathematician George Boole in 1 854 in his book The Laws of Th ought. Many mathematical statements are constructed by combining one or more propositions. New propositions, called compound propositions, are formed from existing propositions using logical operators.

Let p be a proposition. The negation ofp, denoted by p (also denoted by p), is the statement .....

"It is not the case that p."

The proposition p is read "not p." The truth value of the negation of p, ..... p, is the opposite of the truth value of p . .....

EXAMPLE 3

Exam: �

Find the negation of the proposition "Today is Friday." and express this in simple English.

Solution: The negation is "It is not the case that today is Friday." This negation can be more simply expressed by "Today is not Friday," or "It is not Friday today."

EXAMPLE 4

Find the negation of the proposition "At least

1 0 inches of rain fell today in Miami."

and express this in simple English.

Solution: The negation is "It is not the case that at least

10

inches of rain fell today in Miami."

This negation can be more simply expressed by "Less than

TABLE 1 The Truth Table for the Negation of a Proposition. p

"" p

T

F

F

T

1 0 inches of rain fell today in Miami."

Remark: Strictly speaking, sentences involving variable times such as those in Examples 3 and

4 are not propositions unless a fixed time is assumed.

The same holds for variable places unless a fixed place is assumed and for pronouns unless a particular person is assumed. We will always assume fixed times, fixed places, and particular people in such sentences unless otherwise noted.

Table 1 displays the truth table for the negation of a proposition p . This table has a row for each of the two possible truth values of a proposition p . Each row shows the truth value of -'p corresponding to the truth value of p for this row.

4

1 / The Foundations: Logic and Proofs

/-4

The negation of a proposition can also be considered the result of the operation of the

negation operator on a proposition. The negation operator constructs a new proposition from a single existing proposition. We will now introduce the logical operators that are used to form new propositions from two or more existing propositions. These logical operators are also called

connectives.

DEFINITION 2

Let p and q be propositions. The conjunction of p and q , denoted by p /\ q, is the proposition " " p and q . The conjunction p /\ q is true when both p and q are true and is false otherwise .

Table 2 displays the truth table of p /\ q . This table has a row for each of the four possible combinations of truth values of p and q . The four rows correspond to the pairs of truth values TT, TF, FT, and FF, where the first truth value in the pair is the truth value of p and the second truth value is the truth value of q . Note that in logic the word "but" sometimes is used instead of "and" in a conjunction. For example, the statement "The sun is shining, but it is raining" is another way of saying "The sun is shining and it is raining." (In natural language, there is a subtle difference in meaning between "and" and "but"; we will not be concerned with this nuance here.)

EXAMPLE 5

Find the conjunction of the propositions p and q where p is the proposition "Today is Friday" and q is the proposition "It is raining today."

S olution: The conjunction of these propositions, p /\ q , is the proposition "Today is Friday and

it is raining today." This proposition is true on rainy Fridays and is false on any day that is not a � Friday and on Fridays when it does not rain.

DEFINITION 3

Let p and q be propositions. The disjunction of p and q , denoted by p V q , is the proposition " p or q . The disj unction p V q is false when both p and q are false and is true otherwise.

"

Table 3 displays the truth table for p V q . The use of the connective or in a disjunction corresponds to one of the two ways the word or is used in English, namely, in an inclusive way. A disjunction is true when at least one of the two propositions is true. For instance, the inclusive or is being used in the statement "Students who have taken calculus or computer science can take this class."

TABLE 2 The Truth Table for the Conjunction of Two Propositions. p

TABLE 3 The Truth Table for the Disjunction of Two Propositions.

q

p /\ q

p

q

pVq

T T T F

T

T

T

T

T

T

F

F

T

F

F

T

F

F

F

F

F

F

T F

1 . 1 Propositional Logic

1-5

5

Here, we mean that students who have taken both calculus and computer science can take the class, as well as the students who have taken only one of the two subjects. On the other hand, we are using the exclusive or when we say "Students who have taken calculus or computer science, but not both, can enroll in this class." Here, we mean that students who have taken both calculus and a computer science course cannot take the class. Only those who have taken exactly one of the two courses can take the class. Similarly, when a menu at a restaurant states, "Soup or salad comes with an entree," the restaurant almost always means that customers can have either soup or salad, but not both. Hence, this is an exclusive, rather than an inclusive, or.

EXAMPLE 6

What is the disjunction of the propositions p and q where p and q are the same propositions as in Example 5?

Solution: The disjunction o f p and q , p v q, i s the proposition "Today is Friday or it is raining today." This proposition is true on any day that is either a Friday or a rainy day (including rainy Fridays). .... It is only false on days that are not Fridays when it also does not rain. As was previously remarked, the use of the connective or in a disjunction corresponds to one of the two ways the word or is used in English, namely, in an inclusive way. Thus, a disjunction is true when at least one of the two propositions in it is true. Sometimes, we use or in an exclusive sense. When the exclusive or is used to connect the propositions p and q , the " proposition p or q (but not both)" is obtained. This proposition is true when p is true and q is false, and when p is false and q is true. It is false when both p and q are false and when both are true.

DEFINITION 4

Let p and q be propositions. The exclusive or of p and q , denoted by p E9 q , is the proposition that is true when exactly one of p and q is true and is false otherwise.

The truth table for the exclusive or of two propositions is displayed in Table 4.

unkS � GEORGE BOOLE ( 1 8 1 5-1 864) George Boole, the son of a cobbler, was born in Lincoln, England, in November 1 8 1 5 . Because of his family's difficult financial situation, Boole had to struggle to educate himself while supporting his family. Nevertheless, he became one of the most important mathematicians of the 1 800s. Although he considered a career as a clergyman, he decided instead to go into teaching and soon afterward opened a school of his own. In his preparation for teaching mathematics, Boole-unsatisfied with textbooks of his day-decided to read the works of the great mathematicians. While reading papers of the great French mathematician Lagrange, Boole made discoveries in the calculus of variations, the branch of analysis dealing with finding curves and surfaces optimizing certain parameters. In 1 848 Boole published The Mathematical Analysis a/Logic, the first of his contributions to symbolic logic. In 1 849 he was appointed professor of mathematics at Queen's College in Cork, Ireland. In 1 854 he published The Laws 0/ Thought, his most famous work. In this book, Boole introduced what is now called Boolean algebra in his honor. Boole wrote textbooks on differential equations and on difference equations that were used in Great Britain until the end of the nineteenth century. Boole married in 1 855; his wife was the niece of the professor of Greek at Queen's College. In 1 864 Boole died from pneumonia, which he contracted as a result of keeping a lecture engagement even though he was soaking wet from a rainstorm.

6

I / The Foundations:

Logic and Proofs

1-6

TABLE 4 The Truth Table for

TABLE 5 The Truth Table for the Conditional Statement p -- q.

the Exclusive Or of Two Propositions. p

q

p ffi q

p

q

p -+ q

T

T

T

F

T

T

T

F

T

F

F

F

T

T T

F

T

T

F

F

F

F

F

T

Conditional Statements We will discuss several other important ways in which propositions can be combined.

DEFINITION 5

Assessment i::J

Let p and q be propositions. The conditional statement p � q is the proposition "if p, then q ." The conditional statement p � q is false when p is true and q is false, and true otherwise. In the conditional statement p � q , p is called the hypothesis (or antecedent or premise) and q is called the conclusion (or consequence).

The statement p � q i s called a conditional statement because p � q asserts that q i s true on the condition that p holds. A conditional statement is also called an implication. The truth table for the conditional statement p � q is shown in Table 5. Note that the statement p � q is true when both p and q are true and when p is false (no matter what truth value q has). Because conditional statements play such an essential role in mathematical reasoning, a variety of terminology is used to express p � q . You will encounter most if not all of the following ways to express this conditional statement:

"

"if p, then q "if p, q " " " p i s sufficient for q "q if p " "q when p " " "a necessary condition for p is q " " q unless -'p

"p implies q " " p only if q"

"a sufficient condition for q is p

" " q whenever p " " q is necessary for p " " q follows from p

"

A useful way to understand the truth value of a conditional statement is to think of an obligation or a contract. For example, the pledge many politicians make when running for office is "If I am elected, then I will lower taxes." If the politician is elected, voters would expect this politician to lower taxes. Furthermore, if the politician is not elected, then voters will not have any expectation that this person will lower taxes, although the person may have sufficient influence to cause those in power to lower taxes. It is only when the politician is elected but does not lower taxes that voters can say that the politician has broken the campaign pledge. This last scenario corresponds to the case when p is true but q is false in p � q . Similarly, consider a statement that a professor might make: "If you get

1 00% on the final, then you will get an A."

1 . 1 Propositional Logic

/-7

7

If you manage to get a 1 00% on the final, then you would expect to receive an A. If you do not get 1 00% you may or may not receive an A depending on other factors. However, if you do get 1 00%, but the professor does not give you an A, you will feel cheated. " " " Many people find it confusing that p only if q expresses the same thing as "if p then q . " " To remember this, note that p only if q says that p cannot be true when q is not true. That is, the statement is false if p is true, but q is false. When p is false, q may be either true or false, because the statement says nothing about the truth value of q . A common error is for people to " " think that q only if p is a way of expressing p -* q . However, these statements have different truth values when p and q have different truth values. " The word "unless" is often used to express conditional statements. Observe that q unless " " " -.p means that if -'p is false, then q must be true. That is, the statement q unless -.p is false " " when p is true and q is false, but it is true otherwise. Consequently, q unless -.p and p -* q always have the same truth value. We illustrate the translation between conditional statements and English statements in Example 7.

EXAMPLE 7

Let p be the statement "Maria learns discrete mathematics" and q the statement "Maria will find a good job." Express the statement p -* q as a statement in English.

Exlra �

Solution: From the definition of conditional statements, we see that when p is the statement

ExampleS �

"Maria learns discrete mathematics" and q is the statement "Maria will find a good job," p represents the statement

-*

q

"If Maria learns discrete mathematics, then she will find a good job." There are many other ways to express this conditional statement in English. Among the most natural of these are: "Maria will find a good job when she learns discrete mathematics." "For Maria to get a good job, it is sufficient for her to learn discrete mathematics." and "Maria will find a good job unless she does not learn discrete mathematics." Note that the way we have defined conditional statements is more general than the meaning attached to such statements in the English language. For instance, the conditional statement in Example 7 and the statement "If it is sunny today, then we will go to the beach." are statements used in normal language where there is a relationship between the hypothesis and the conclusion. Further, the first of these statements is true unless Maria learns discrete mathematics, but she does not get a good job, and the second is true unless it is indeed sunny today, but we do not go to the beach. On the other hand, the statement "If today is Friday, then

2 + 3 = 5."

is true from the definition of a conditional statement, because its conclusion i s true. (The truth value of the hypothesis does not matter then.) The conditional statement "If today is Friday, then 2 +

3 = 6."

i s true every day except Friday, even though 2 + 3 = 6 i s false. We would not use these last two conditional statements in natural language (except perhaps in sarcasm), because there is no relationship between the hypothesis and the conclusion in either

8

I / The Foundations: Logic and Proofs

1-8

statement. In mathematical reasoning, we consider conditional statements of a more general sort than we use in English. The mathematical concept of a conditional statement is independent of a cause- and- effect relationship between hypothesis and conclusion. Our definition of a conditional statement specifies its truth values; it is not based on English usage. Propositional language is an artificial language; we only parallel English usage to make it easy to use and remember. The if-then construction used in many programming languages is different from that used in logic. Most programming languages contain statements such as if p then S, where p is a . proposition and S is a program segment (one or more statements to be executed). When execution of a program encounters such a statement, S is executed if p is true, but S is not executed if p is false, as illustrated in Example 8.

EXAMPLE 8

What is the value of the variable

if 2 + 2

x=

x after the statement

= 4 then x := x + 1

:= stands for assignment. The I to x .) Solution: Because 2 + 2 = 4 is true, the assignment statement x := x + I is executed. Hence, .... x has the value 0 + I = I after this statement is encountered. if 0 before this statement is encountered? (The symbol statement + 1 means the assignment of the value of +

x := x

x

CONVERSE, CONTRAPOSITIVE, AND INVERSE We can form some new conditional statements starting with a conditional statement p -+ q . In particular, there are three related conditional statements that occur so often that they have special names. The proposition q -+ p is called the converse of p -+ q . The contrapositive of p -+ q is the proposition -.q -+ -'p. The proposition -'p -+ -.q is called the inverse of p -+ q. We will see that of these three conditional statements formed from p -+ q , only the contrapositive always has the same truth value as p -+ q . .. We first show that the contrapositive, -.q -+ -'p, of a conditional statement p -+ q always has the same truth value as p -+ q . To see this, note that the contrapositive is false only when -'p is false and -.q is true, that is, only when p is true and q is false. We now show that neither the converse, q -+ p, nor the inverse, -'p -+ -'q , has the same truth value as p -+ q for all possible truth values of p and q . Note that when p is true and q is false, the original conditional statement is false, but the converse and the inverse are both true. When two compound propositions always have the same truth value we call them equivalent, so that a conditional statement and its contrapositive are equivalent. The converse and the inverse of a conditional statement are also equivalent, as the reader can verify, but neither is equivalent to the original conditional statement. (We will study equivalent propositions in Section 1 .2.) Take note that one of the most common logical errors is to assume that the converse or the inverse of a conditional statement is equivalent to this conditional statement. We illustrate the use of conditional statements in Example 9.

EXAMPLE 9

What are the contrapositive, the converse, and the inverse of the conditional statement "The home team wins whenever it is raining."?

Solution: Because "q whenever p" is one of the ways to express the conditional statement

p -+ q , the original statement can be rewritten as "If it is raining, then the home team wins."

Consequently, the contrapositive of this conditional statement is "If the home team does not win, then it is not raining."

1 . 1 Propositional Logic

1-9

9

The converse is "If the home team wins, then it is raining." The inverse is "If it is not raining, then the home team does not win." Only the contrapositive is equivalent to the original statement.

BICONDITIONALS We now introduce another way to combine propositions that expresses that two propositions have the same truth value.

DEFINITION 6

Let p and q be propositions. The biconditional statement p *+ q is the proposition "p if and only if q ." The biconditional statement p *+ q is true when p and q have the same truth values, and is false otherwise. Biconditional statements are also called bi-implications.

The truth table for p *+ q is shown in Table 6. Note that the statement p *+ q is true when both the conditional statements p -+ q and q -+ P are true and is false otherwise. That is why we use the words "if and only if " to express this logical connective and why it is symbolically written by combining the symbols -+ and +-. There are some other common ways to express p *+ q :

"p is necessary and sufficient for q " "if p then q , and conversely" "p iff q ." The last way of expressing the biconditional statement p *+ q uses the abbreviation "iff" for "if and only if." Note that p *+ q has exactly the same truth value as (p -+ q ) 1\ (q -+ p).

EXAMPLE 10

Let p be the statement "You can take the flight" and let q be the statement "You buy a ticket." Then p *+ q is the statement "You can take the flight if and only if you buy a ticket." This statement is true if p and q are either both true or both false, that is, if you buy a ticket and can take the flight or if you do not buy a ticket and you cannot take the flight. It is false when p and q have opposite truth values, that is, when you do not buy a ticket, but you can take the flight (such as when you get a free trip) and when you buy a ticket and cannot take the flight � (such as when the airline bumps you).

TABLE 6 The Truth Table for the Biconditional p +-+ q. p

q

p +-+ q

T

T

T

T

F

F

F

T

F

F

F

T

10

1 / The Foundations: Logic and Proofs

1-10

IMPLICIT USE OF BICONDITIONALS You should be aware that biconditionals are not always explicit in natural language. In particular, the "if and only if" construction used in biconditionals is rarely used in common language. Instead, biconditionals are often expressed using an "if, then" or an "only if" construction. The other part of the "if and only if" is implicit. That is, the converse is implied, but not stated. For example, consider the statement in English "If you finish your meal, then you can have dessert." What is really meant is "You can have dessert if and only if you finish your meal." This last statement is logically equivalent to the two statements "If you finish your meal, then you can have dessert" and "You can have dessert only if you finish your meal." Because of this imprecision in natural language, we need to make an assumption whether a conditional statement in natural language implicitly includes its converse. Because precision is essential in mathematics and in logic, we will always distinguish between the conditional statement p --+ q and the biconditional statement p ++ q .

Truth Tables o f Compound Propositions

Demo �

EXAMPLE 11

We have now introduced four important logical connectives--conjunctions, disjunctions, con­ ditional statements, and biconditional statements-as well as negations. We can use these con­ nectives to build up complicated compound propositions involving any number of propositional variables. We can use truth tables to determine the truth values of these compound propositions, as Example 1 1 illustrates. We use a separate column to find the truth value of each compound expression that occurs in the compound proposition as it is built up. The truth values of the compound proposition for each combination of truth values of the propositional variables in it is found in the final column of the table. Construct the truth table of the compound proposition (p

v

-.q) --+ (p

/\

q).

Solution: Because this truth table involves two propositional variables p and q , there are four

rows in this truth table, corresponding to the combinations of truth values TT, TF, FT, and FE The first two columns are used for the truth values of p and q , respectively. In the third column we find the truth value of -'q , needed to find the truth value of p v q found in the fourth column. The truth value of p /\ q is found in the fifth column. Finally, the truth value of (p v -.q ) --+ (p /\ q) is found in the last column. The resulting truth table is shown in � Table 7. -'

,

Precedence of Logical Operators We can construct compound propositions using the negation operator and the logical operators defined so far. We will generally use parentheses to specify the order in which logical operators

TABLE 7 The Truth Table of (p v .., q) P

q

-+

..., q

p V ""q

p /\ q

(p

/\

q). (P V ..., q )

->

T

T

F

T

T

T

T

F

T

T

F

F

F

T

T

F F

T

F

F T

F

F

F

(p /\ q )

1 . 1 Propositional Logic

1-1 1

TABLE S Precedence of Logical Operators. Operator Precedence �

1

1\

2 3

---+

4 5

v

*+

11

in a compound proposition are to b e applied. For instance, (p v q) /\ ( -,r ) i s the conjunction of p v q and -'r . However, to reduce the number of parentheses, we specify that the negation operator is applied before all other logical operators. This means that -'p /\ q is the conjunc­ tion of -'p and q , namely, (-,p) /\ q , not the negation of the conjunction of p and q , namely -'(p /\ q). Another general rule of precedence is that the conjunction operator takes precedence over the disjunction operator, so that p /\ q V r means (p /\ q ) V r rather than p /\ (q V r ) . Because this rule may be difficult to remember, we will continue to use parentheses so that the order of the disjunction and conjunction operators is clear. Finally, it is an accepted rule that the conditional and biconditional operators -+ and *+ have lower precedence than the conjunction and disjunction operators, /\ and v . Consequently, p v q -+ r is the same as (p v q ) -+ r . We will use parentheses when the order ofthe conditional operator and biconditional operator is at issue, although the conditional operator has precedence over the biconditional operator. Table 8 displays the precedence levels of the logical operators, -', /\, v, -+ , and *+ .

Translating English Sentences There are many reasons to translate English sentences into expressions involving propositional variables and logical connectives. In particular, English (and every other human language) is often ambiguous. Translating sentences into compound statements (and other types of logical expressions, which we will introduce later in this chapter) removes the ambiguity. Note that this may involve making a set of reasonable assumptions based on the intended meaning of the sentence. Moreover, once we have translated sentences from English into logical expressions we can analyze these logical expressions to determine their truth values, we can manipulate them, and we can use rules of inference (which are discussed in Section 1 .5) to reason about them. To illustrate the process oftranslating an English sentence into a logical expression, consider Examples 1 2 and 1 3 .

EXAMPLE 12

How can this English sentence be translated into a logical expression? "You can access the Internet from campus only if you are a computer science major or you are not a freshman." Solution: There are many ways to translate this sentence into a logical expression. Although it

is possible to represent the sentence by a single propositional variable, such as p, this would not be useful when analyzing its meaning or reasoning with it. Instead, we will use propositional variables to represent each sentence part and determine the appropriate logical connectives between them. In particular, we let a, c, and / represent "You can access the Internet from campus," "You are a computer science major," and "You are a freshman," respectively. Noting that "only if" is one way a conditional statement can be expressed, this sentence can be repre­ sented as

a -+ (c V -,/). EXAMPLE 13

How can this English sentence be translated into a logical expression? "You cannot ride the roller coaster if you are under 4 feet tall unless you are older than 1 6 years old."

12

1 / The Foundations: Logic and Proofs

1-12

Solution: Let q, r , and s represent "You can ride the roller coaster," "You are under 4 feet tall,"

and "You are older than 1 6 years old," respectively. Then the sentence can be translated to

Of course, there are other ways to represent the original sentence as a logical expression,

0 then x

:= x + 1 .

When this statement is encountered in a program, the value of the variable x at that point in the execution of the program is inserted into P (x), which is "x > 0." If P (x) is true for this value of x, the assignment statement x := x + 1 is executed, so the value of x is increased by 1 . If P(x) is false for this value of x, the assignment statement is not executed, so the value of x is � not changed. Predicates are also used in the verification that computer programs always produce the desired output when given valid input. The statements that describe valid input are known as preconditions and the conditions that the ouput should satisfy when the program has run are known as postconditions. As Example 7 illustrates, we use predicates to describe both preconditions and postconditions. We will study this process in greater detail in Section 4.4. EXAMPLE 7

Consider the following program, designed to interchange the values of two variables x and y. t emp

:= x

x

.

y

: = t emp

-

y

Find predicates that we can use as the precondition and the postcondition to verify the correctness of this program. Then explain how to use them to verify that for all valid input the program does what is intended. Solution: For the precondition, we need to express that x and y have particular values before

we run the program. So, for this precondition we can use the predicate P(x , y), where P(x , y) is the statement "x = a and y = b," where a and b are the values of x and y before we run the program. Because we want to verify that the program swaps the values of x and y for all input values, for the postcondition we can use Q(x , y), where Q(x , y) is the statement "x = b and y = a ." To verify that the program always does what it is supposed to do, suppose that the precon­ dition P(x , y) holds. That is, we suppose that the statement "x = a and y = b" is true. This means that x = a and y = b. The first step of the program, temp := x, assigns the value ofx to the variable temp, so after this step we know that x = a , temp = a, and y = b. After the second step of the program, x := y, we know that x = b, temp = a, and y = b. Finally, after the third step, we know that x = b, temp = a , and y = a . Consequently, after this program is run, the � postcondition Q(x , y) holds, that is, the statement "x = b and y = a" is true.

Quantifiers

Assessment �

When the variables in a propositional function are assigned values, the resulting statement becomes a proposition with a certain truth value. However, there is another important way, called quantification, to create a proposition from a propositional function. Quantification expresses the extent to which a predicate is true over a range of elements. In English, the words all, some, many, none, and few are used in quantifications. We will focus on two types of quantification here: universal quantification, which tells us that a predicate is true for every element under consideration, and existential quantification, which tells us that there is one or more element under consideration for which the predicate is true. The area of logic that deals with predicates and quantifiers is called the predicate calculus.

34

I

/ The Foundations: Logic and Proofs

Assessmenl �

DEFINITION 1

1-34

THE UNIVERSAL QUANTIFIER Many mathematical statements assert that a property is true for all values of a variable in a particular domain, called the domain of discourse (or the universe of discourse), often just referred to as the domain. Such a statement is expressed using universal quantification. The universal quantification of P (x) for a particular domain is the proposition that asserts that P(x) is true for all values of x in this domain. Note that the domain specifies the possible values of the variable x . The meaning of the universal quantification of P (x) changes when we change the domain. The domain must always be specified when a universal quantifier is used; without it, the universal quantification of a statement in not defined.

The universal quantification of P(x) is the statement "P(x) for all values ofx in the domain." The notation Vx P(x) denotes the universal quantification of P(x). Here V is called the

universal quantifier. We read Vx P(x) as "for all x P(x)" or "for every x P(x )." An element for which P(x) is false is called a counterexample ofVx P(x).

The meaning of the universal quantifier is summarized in the first row of Table 1. We illustrate the use of the universal quantifier in Examples 8-1 3. EXAMPLE S

Let P(x) be the statement "x + 1 > x ." What is the truth value of the quantification Vx P(x), where the domain consists of all real numbers? Solution: Because P(x) is true for all real numbers x, the quantification

Vx P (x) is true. Generally, an implicit assumption is made that all domains of discourse for quantifiers are nonempty. Note that if the domain is empty, then Vx P (x) is true for any propositional function P (x) because there are no elements x in the domain for which P(x) is false.

Remark:

Besides "for all" and "for every," universal quantification can be expressed in many other ways, including "all of," "for each," "given any," "for arbitrary," "for each," and "for any." It is best to avoid using "for any x" because it is often ambiguous as to whether "any" means "every" or "some." In some cases, "any" is unambiguous, such as when it is used in negatives, for example, "there is not any reason to avoid studying."

Remark:

TABLE 1 Quantifiers. Statement

When True?

When False?

Vx P (x)

P (x) is true for every x. There is an x for which P (x) is true.

There is an x for which P(x) is false.

3x P (x)

P (x) is false for every x.

1 .3 Predicates and Quantifiers 35

1-35

A statement 'Ix P (x ) is false, where P (x) is a propositional function, if and only if P (x) is not always true when x is in the domain. One way to show that P (x) is not always true when x is in the domain is to find a counterexample to the statement 'Ix P (x). Note that a single counterexample is all we need to establish that 'Ix P (x) is false. Example 9 illustrates how counterexamples are used. EXAMPLE 9

Let Q(x) be the statement "x < 2." What is the truth value of the quantification Vx Q (x), where the domain consists of all real numbers? Solution: Q(x) is not true for every real number x , because, for instance, Q (3) is false. That is, x = 3 is a counterexample for the statement Vx Q (x). Thus Vx Q(x)

is false. EXAMPLE 10

Suppose that P (x) is "x 2 > 0." To show that the statement Vx P (x) is false where the uni­ verse of discourse consists of all integers, we give a counterexample. We see that x = 0 is a counterexample because x 2 = 0 when x = 0, so that x 2 is not greater than 0 when x = O . .... Looking for counterexamples to universally quantified statements is an important activity in the study of mathematics, as we will see in subsequent sections of this book. When all the elements in the domain can be listed-say, X I , X2 , . . . , xn-it follows that the universal quantification 'Ix P (x) is the same as the conjunction

because this conjunction is true if and only if P (X I ), P (X2 ), . . . , P (xn ) are all true. EXAMPLE 11

What is the truth value of 'Ix P (x), where P (x) is the statement "x 2 consists of the positive integers not exceeding 4?


3." What is the truth value of the quantification 3x P (x), where the domain consists of all real numbers? Solution: Because "x

> 3" is sometimes true-for instance, when x tification of P (x), which is 3x P(x), is true.

= 4-the existential quan­

....

Observe that the statement 3x P (x) is false if and only ifthere is no element x in the domain for which P (x) is true. That is, 3x P (x) is false if and only P(x) is false for every element of the domain. We illustrate this observation in Example 1 5 . EXAMPLE 15

Let Q(x) denote the statement "x = x + I ." What is the truth value of the quantification 3x Q(x), where the domain consists of all real numbers?

1 .3 Predicates and Quantifiers 37

1-3 7

Solution: Because Q(x) is false for every real number x, the existential quantification of Q(x), which is 3x Q(x), is false. �

Generally, an implicit assumption is made that all domains of discourse for quantifiers are nonempty. If the domain is empty, then 3x Q(x) is false whenever Q(x) is a propositional function because when the domain is empty, there can be no element in the domain for which Q(x) is true.

Remark:

When all elements in the domain can be listed-say, XI , X2 , . . . , xn- the existential quan­ tification 3x P (x) is the same as the disjunction

because this disjunction is true if and only if at least one of P (X I ), P(X2 ), . . . , P (xn) is true. EXAMPLE 16

What is the truth value onx P (x), where P(x) is the statement "x 2 discourse consists of the positive integers not exceeding 4?

> 1 0"

and the universe of

Solution: Because the domain is { l , 2, 3 , 4}, the proposition 3x P (x) is the same as the

disjunction

P( l) v P(2) v P(3) v P(4). Because P(4), which is the statement "42

>

1 0," is true, it follows that 3x P(x) is true.

It is sometimes helpful to think in terms of looping and searching when determining the truth value of a quantification. Suppose that there are n objects in the domain for the variable x . To determine whether Yx P (x) is true, we can loop through all n values ofx to see if P (x) is always true. Ifwe encounter a value x for which P (x) is false, then we have shown that Yx P(x) is false. Otherwise, Yx P(x) is true. To see whether 3x P (x) is true, we loop through the n values of x searching for a value for which P (x) is true. If we find one, then 3x P (x) is true. If we never find such an x, then we have determined that 3x P (x) is false. (Note that this searching procedure does not apply if there are infinitely many values in the domain. However, it is still a useful way of thinking about the truth values of quantifications.)

Other Quantifiers We have now introduced universal and existential quantifiers. These are the most important quantifiers in mathematics and computer science. However, there is no limitation on the number of different quantifiers we can define, such as "there are exactly two," "there are no more than three," "there are at least 1 00," and so on. Of these other quantifiers, the one that is most often seen is the uniqueness quantifier, denoted by 3 ! or 31 . The notation 3 !x P (x) [or 3lx P(x)] states "There exists a unique x such that P (x) is true." Other phrases for uniqueness quantification include "there is exactly one" and "there is one and only one." Observe that we can use quantifiers and propositional logic to express uniqueness (see Exercise 52 in Section 1 . 4), so the uniqueness quantifier can be avoided. Generally, it is best to stick with existential and universal quantifiers so that rules of inference for these quantifiers can be used.

38

I

/ The Foundations: Logic and Proofs

/ -38

Quantifiers with Restricted Domains An abbreviated notation is often used to restrict the domain of a quantifier. In this notation, a condition a variable must satisfY is included after the quantifier. This is illustrated in Example 1 7. We will also describe other forms of this notation involving set membership in Section 2. 1 . EXAMPLE 17

What do the statements ' 0), ' 0 (z2 the domain in each case consists of the real numbers?

= 2) mean, where

0 (x 2 > 0) states that for every real number x with x < 0, x 2 > O . That is, it states "The square of a negative real number is positive." This statement is the same as ' 0). The statement ' 0 (z2 = 2) is another way of expressing 3z(z > 0 1\ z2 = 2).

Precedence of Quantifiers The quantifiers ' I ," if the

value of x when this statement is reached is a) x = O. b) x = 1 . c) x = 2.

5. Let P (x) be the statement "x spends more than five hours

every weekday in class," where the domain for x consists of all students. Express each of these quantifications in English.

1 .3 Predicates and Quantifiers 47

1-47

b) Vx P(x) a) 3x P(x ) d) "Ix ..... P (x) e ) 3 x ..... P (x) 6. Let N(x) be the statement "x has visited North Dakota,"

where the domain consists of the students in your school. Express each of these quantifications in English.

a) 3x N(x) d) 3x ..... N(x)

e) ..... 3x N (x) t) Vx ..... N (x)

b) Vx N(x) e) .....Vx N (x)

7. Translate these statements into English, where C (x ) is "x is a comedian" and F (x) is "x is funny" and the domain

8.

9.

10.

11.

consists of all people. b) Vx(C (x ) /\ F (x» a) Vx(C (x) -+ F(x» d) 3x(C (x) /\ F (x» e) 3x(C (x) -+ F (x» Translate these statements into English, where R(x) is "x is a rabbit" and H (x) is "x hops" and the domain consists of all animals. b) Vx(R(x ) /\ H (x» a) Vx(R(x) -+ H (x» d) 3x(R(x) /\ H (x» e) 3x(R(x) -+ H(x» Let P (x) be the statement "x can speak Russian" and let Q(x) be the statement "x knows the computer language C++." Express each of these sentences in terms of P (x), Q(x), quantifiers, and logical connectives. The domain for quantifiers consists of all students at your school. a) There is a student at your school who can speak Rus­ sian and who knows C++. b) There is a student at your school who can speak Rus­ sian but who doesn't know C++. e) Every student at your school either can speak Russian or knows C++. d) No student at your school can speak Russian or knows C++. Let C (x) be the statement "x has a cat," let D (x) be the statement "x has a dog," and let F(x) be the statement "x has a ferret." Express each of these statements in terms of C (x), D (x), F (x), quantifiers, and logical connectives. Let the domain consist of all students in your class. a) A student in your class has a cat, a dog, and a ferret. b) All students in your class have a cat, a dog, or a ferret. e) Some student in your class has a cat and a ferret, but not a dog. d) No student in your class has a cat, a dog, and a ferret. e) For each of the three animals, cats, dogs, and ferrets, there is a student in your class who has one of these animals as a pet. Let P (x) be the statement "x = x 2 ." If the domain con­ sists of the integers, what are the truth values?

b) P(l) a) P (O) e) 3x P (x) d) P(- l ) 12. Let Q(x) be the statement "x + 1

>

e) P (2 ) t) Vx P (x) 2x ." If the domain

consists of all integers, what are these truth values?

a) Q(O) d) 3x Q(x) g) Vx ..... Q(x)

b) Q(- I ) e) Vx Q (x)

e) Q ( l ) t) 3x ..... Q(x)

13. Determine the truth value of each of these statements if

the domain consists of all integers.

b) 3n (2n = 3n) d) Vn(n2 :::: n )

a) Vn(n + 1 > n ) e ) 3n(n = -n)

1 4 . Determine the truth value of each of these statements if

the domain consists of all real numbers. b) 3X(x4 < x2) a) 3x (x3 = - 1 )

e) VX« _X)2

= x2)

d) Vx(2x

>

x)

1 5. Determine the truth value of each of these statements if

the domain for all variables consists of all integers. a) Vn (n2 :::: 0) b) 3n(n2 = 2) e) Vn (n2 :::: n ) d ) 3n(n2 < 0) 1 6. Determine the truth value of each of these statements if the domain of each variable consists of all real numbers. b) 3x (x2 = - 1 ) a) 3x(x2 = 2) e) Vx(x2 + 2 :::: 1 ) d) Vx (x2 =1= x ) 17. Suppose that the domain o f the propositional function P (x) consists of the integers 0, 1 , 2, 3, and 4. Write out each of these propositions using disjunctions, conjunc­ tions, and negations.

a) 3x P (x) d) Vx ..... P (x )

b ) Vx P (x ) e ) ..... 3x P (x )

e) 3x ..... P (x) t) .....Vx P(x )

1 8 . Suppose that the domain of the propositional function P (x) consists of the integers -2, - 1 , 0, 1 , and 2. Write

out each of these propositions using disjunctions, con­ junctions, and negations.

a) 3x P (x ) d) Vx ..... P (x )

b) Vx P (x) e ) ..... 3x P (x )

e ) 3x ..... P (x) t) .....Vx P (x)

19. Suppose that the domain of the propositional function P (x ) consists of the integers 1 , 2, 3 , 4, and 5 . Express

these statements without using quantifiers, instead using only negations, disjunctions, and conjunctions.

b) Vx P (x) a) 3x P (x) d) .....Vx P (x) e) ..... 3x P (x) e) Vx« x =1= 3) -+ P (x» v 3x ..... P (x)

20. Suppose that the domain of the propositional function P (x ) consists of -5, 3 - 1 , 1 , 3 , and 5. Express these statements without using quantifiers, instead using only negations, disjunctions, and conjunctions. -

a) e) d) e)

,

b ) Vx P (x) 3x P (x ) Vx « x =1= 1 ) -+ P (x» 3x « x :::: 0) /\ P (x» 3x( ..... P (x» /\ Vx« x < 0) -+ P (x»

2 1 . For each of these statements find a domain for which the

statement is true and a domain for which the statement is false. a) Everyone is studying discrete mathematics. b) Everyone is older than 2 1 years. e) Every two people have the same mother. d) No two different people have the same grandmother. 22. For each of these statements find a domain for which the statement is true and a domain for which the statement is false. a) Everyone speaks Hindi. b) There is someone older than 2 1 years.

48

1 / The Foundations: Logic and Proofs

e) Every two people have the same first name. d) Someone knows more than two other people. 23. Translate in two ways each of these statements into logi­

cal expressions using predicates, quantifiers, and logical connectives. First, let the domain consist of the students in your class and second, let it consist of all people. a) Someone in your class can speak Hindi. b) Everyone in your class is friendly. e) There is a person in your class who was not born in California. d) A student in your class has been in a movie. e) No student in your class has taken a course in logic programming. 24. Translate in two ways each of these statements into logi­ cal expressions using predicates, quantifiers, and logical connectives. First, let the domain consist of the students in your class and second, let it consist of all people. a) Everyone in your class has a cellular phone. b) Somebody in your class has seen a foreign movie. e) There is a person in your class who cannot swim. d) All students in your class can solve quadratic equations. e) Some student in your class does not want to be rich. 25. Translate each of these statements into logical expressions using predicates, quantifiers, and logical connectives. a) No one is perfect. b) Not everyone is perfect. e) All your friends are perfect. d) At least one of your friends is perfect. e) Everyone is your friend and is perfect. f) Not everybody is your friend or someone is not perfect. 26. Translate each ofthese statements into logical expressions in three different ways by varying the domain and by using predicates with one and with two variables. a) Someone in your school has visited Uzbekistan. b) Everyone in your class has studied calculus and C++. e) No one in your school owns both a bicycle and a motorcycle. d) There is a person in your school who is not happy. e) Everyone in your school was born in the twentieth century. 27. Translate each ofthese statements into logical expressions in three different ways by varying the domain and by using predicates with one and with two variables. a) A student in your school has lived in Vietnam. b) There is a student in your school who cannot speak Hindi. e) A student in your school knows Java, Prolog, and C++. d) Everyone in your class enjoys Thai food. e) Someone in your class does not play hockey. 28. Translate each ofthese statements into logical expressions using predicates, quantifiers, and logical connectives. a) Something is not in the correct place.

1 -48

b) All tools are in the correct place and are in excellent

condition.

e) Everything is in the correct place and in excellent

condition.

d) Nothing is in the correct place and is in excellent

condition.

e) One of your tools is not in the correct place, but it is

in excellent condition.

29. Express each of these statements using logical operators,

predicates, and quantifiers. a) Some propositions are tautologies. b) The negation of a contradiction is a tautology. e) The disjunction of two contingencies can be a tautology. d) The conjunction of two tautologies is a tautology. 30. Suppose the domain of the propositional function P (x , y) consists of pairs x and y, where x is I , 2, or 3 and y is 1 , 2, or 3. Write out these propositions using disjunctions and conjunctions. a) 3x P(x , 3) b) 'v'y P( I , y) e)

3y-'P(2, y)

d)

'v'x -'P(x , 2)

31. Suppose that the domain of Q(x , y, z) consists of triples x , y, z , where x = 0, I , or 2, y = 0 or I , and z = 0 or 1 .

Write out these propositions using disjunctions and con­ junctions. a) 'v'y Q(O, y, 0) b) 3x Q(x , I , I ) e) 3z-'Q(O, 0, z ) d) 3x-'Q(x , O, I ) 32. Express each o f these statements using quantifiers. Then form the negation of the statement so that no negation is to the left of a quantifier. Next, express the negation in simple English. (Do not simply use the words "It is not the case that.") a) All dogs have fleas. b) There is a horse that can add. e) Every koala can climb. d) No monkey can speak French. e) There exists a pig that can swim and catch fish. 33. Express each of these statements using quantifiers. Then form the negation of the statement, so that no negation is to the left of a quantifier. Next, express the negation in simple English. (Do not simply use the words "It is not the case that.") a) Some old dogs can learn new tricks. b) No rabbit knows calculus. e) Every bird can fly. d) There is no dog that can talk. e) There is no one in this class who knows French and Russian. 34. Express the negation of these propositions using quanti­ fiers, and then express the negation in English. a) Some drivers do not obey the speed limit. b) All Swedish movies are serious. e) No one can keep a secret. d) There is someone in this class who does not have a good attitude.

1 .3 Predicates and Quantifiers 49

/-49

d) Video on demand can be delivered when there are at

35. Find a counterexample, if possible, to these universally

quantified statements, where the domain for all variables consists of all integers. a) b) c)

2 ::: x) \lx(x \lx(x \lx(x 01) x >

v

=




\lx(x 2

¥ 2)

37. Express each of these statements using predicates and

quantifiers. a) A passenger on an airline qualifies as an elite flyer if the passenger flies more than 25,000 miles in a year or takes more than 25 flights during that year. b) A man qualifies for the marathon if his best previous time is less than 3 hours and a woman qualifies for the marathon if her best previous time is less than 3.5 hours. c) A student must take at least 60 course hours, or at least 45 course hours and write a master's thesis, and receive a grade no lower than a B in all required courses, to receive a master's degree. d) There is a student who has taken more than 2 1 credit hours in a semester and received all A's. Exercises 38-42 deal with the translation between system specification and logical expressions involving quantifiers. 38. Translate these system specifications into English where the predicate y) is is in state y" and where the do­ main for and y consists of all systems and all possible states, respectively. a) open) b) malfunctioning) v diagnostic» c) open) v diagnostic) d) available) e) working) 39. Translate these specifications into English where is "Printer is out of service," is "Printer is busy," is "Print job is lost," and is "Print job is queued."

x S(x, "x \l3xS(x, x (S(x, 3xS(x, Sex, 3xS(x, \l3x..., x ...,SS(x,(x,

L(j) p 3p(F(p) \lpB(p) 3j(Q(j) (\lpB(p)

a) b) c) d)

B (p)Q(j)

1\

-+

1\

1\

j B(p» 3jL(j) 3jQ(j) L(j» 3pF(p) \ljQ(j» 3jL(j)

p F(p)j

least 8 megabytes of memory available and the con­ nection speed is at least 56 kilobits per second. 41. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives. a) At least one mail message, among the nonempty set of messages, can be saved if there is a disk with more than 1 0 kilobytes of free space. b) Whenever there is an active alert, all queued messages are transmitted. c) The diagnostic monitor tracks the status of all systems except the main console. d) Each participant on the conference call whom the host of the call did not put on a special list was billed. 42. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives. a) Every user has access to an electronic mailbox. b) The system mailbox can be accessed by everyone in the group if the file system is locked. c) The firewall is in a diagnostic state only if the proxy server is in a diagnostic state. d) At least one router is functioning normally if the throughput is between 1 00 kbps and 500 kbps and the proxy server is not in diagnostic mode. 43. Determine whether -+ and -+ are logically equivalent. JustifY your answer. 44. Determine whether � and � are logically equivalent. JustifY your answer. 45. Show that v and v are logically equivalent. Exercises 46-49 establish rules for null quantification that we can use when a quantified variable does not appear in part of a statement.

-+

occur as a free variable in nonempty. v

a) b) 47.

v

==

v

==

v

nonempty.

48.

40. Express each of these system specifications using predi­

cates, quantifiers, and logical connectives. a) When there is less than 30 megabytes free on the hard disk, a warning message is sent to all users. b) No directories in the file system can be opened and no files can be closed when system errors have been detected. c) The file system cannot be backed up if there is a user currently logged on.

does not A. Assume that thexdomain is (\lxP(x» A \lx(P(x) A) (3xP(x » A 3x(P(x) A) Establish these logical equivalences, where x does not occur as a free variable in A. Assume that the domain is

46. Establish these logical equivalences, where

-+

-+

\lx(P(x) Q(x» \lxP(x) \lx(P(x) Q(x» \Ix P(x) 3x(P(x) Q(x» 3xP(x) 3xQ(x)

\Ix Q(x) \lxQ(x)

(\IxP(x» A \lx(P(x) A) (3xP(x» A 3x(P(x) A) Establish these logical equivalences, where x does not occur as a free variable in A. Assume that the domain is nonempty. \lx(A P(x» A \lxP(x) 3x(A P(x» A 3xP(x) Establish these logical equivalences, where x does not occur as a free variable in Assume that the domain is a)

1\

==

1\

b)

1\

==

1\

a) b) 49.

==

-+

==

-+

A.

nonempty. a) b)

-+

-+

\lx(P(x) A) 3xP(x) A 3x(P(x) A) \lxP(x) A -+

-+

==

==

-+

-+

50

1 I The Foundations: Logic and Proofs

1 -50

"Ix P(x) VxQ(x) Vx(P(x) Q(x)) 3xP(x) 3xQ(x) 3x(P(x) Q(x» 3!x P(x) x P(x)

50. Show that V not logically equivalent.

and

v

are

51. Show that 1\ not logically equivalent.

and

1\

are

52. As mentioned in the text, the notation "There exists a unique

such that

denotes

is true."

If the domain consists of all integers, what are the truth values of these statements? b) = 1) a) > 1) c) +3 = = + 1) 53. What are the truth values o f these statements?

2 3!3!xx (x(x 2x) d) 3!3!xx (x(x x a) 3!xP(x) 3xP(x) VxP(x) 3!x-'P(x) 3!xP(x) -,VxP(x) 3!x P(x), 3, -+

b) -+ c) -+ 54. Write out where the domain consists ofthe inte­ gers 1 , 2, and in terms of negations, conjunctions, and disjunctions. 55. Given the Prolog facts in Example 28, what would Prolog return given these queries?

a)

? i n s t ru c t o r ( chan , math2 7 3 )

b) ? i ns t ruc t o r ( pa t e l , c s 3 0 1 ) c) ? enr o l l ed ( X , c s 3 0 1 )

d) e)

? enr o l l ed ( k i ko , Y ) ? t e aches ( g r o s sman , Y )

56. Given the Prolog facts in Example 28, what would Prolog return when given these queries? a) ? enr o l l ed ( kevin , e e 2 2 2 ) b) ? enr o l l ed ( k i ko , math2 7 3 ) c) ? i ns t ruc t o r ( g r o s sman , X ) d) ? i ns t ruc t o r ( X , c s 3 0 1 )

e)

? t e aches ( X , kevi n )

57. Suppose that Prolog facts are used to define the predicates mother(M, Y) andfather(F, X), which represent that M is the mother of Y and F is the father of X, respectively. Give a Prolog rule to define the predicate sibling(X, y), which represents that X and Y are siblings (that is, have the same mother and the same father). 58. Suppose that Prolog facts are used to define the pred­ icates mother(M, Y) and father(F, X), which repre­ sent that M is the mother of Y and F is the father of X, respectively. Give a Prolog rule to define the predicate grandfather(X, y), which represents that X is the grand­ father of Y . [Hint: You can write a disjunction in Prolog

either by using a semicolon to separate predicates or by putting these predicates on separate lines.]

59-62 P(x), "x "x Q(x), R(x)"x P(x), Q(x), R(x),

Exercises

are based on questions found in the book

Symbolic Logic by Lewis Carroll.

59. Let and be the statements is a profes­ sor," is ignorant," and is vain," respectively. Express each of these statements using quantifiers; logical con­ nectives; and and where the domain consists of all people.

a) No professors are ignorant. b) All ignorant people are vain. c) No professors are vain. Does (c) follow from (a) and (b)? 60. Let and be the statements is a clear explanation," is satisfactory," and is an excuse," respectively. Suppose that the domain for consists of all English text. Express each ofthese statements using quan­ tifiers, logical connectives, and and a) All clear explanations are satisfactory. b) Some excuses are unsatisfactory. c) Some excuses are not clear explanations. Does (c) follow from (a) and (b)?

d)

P(x), Q(x),"x R(x)

"xx "x P(x), Q(x), R(x).

*d)

P(x),"xQ(x), R(x), "x S(x) "x R(x), S(x).

"x

61. Let and be the statements is a baby," is logical," is able to manage a crocodile," and is despised," respectively. Suppose that the domain consists of all people. Express each of these statements using quantifiers; logical connectives; and and a) Babies are illogical. b) Nobody is despised who can manage a crocodile. c) Illogical persons are despised. Babies cannot manage crocodiles. >te) Does (d) follow from (a), (b), and (c)? If not, is there a correct conclusion?

P(x), Q(x),

d)

P(x), Q(x), R(x), S(x) "x "x "x P(x), Q(x), R(x), S(x).

"x

62. Let and be the statements is a duck," is one of my poultry," is an officer," and is willing to waltz," respectively. Express each of these statements using quantifiers; logical connectives; and and a) No ducks are willing to waltz. b) No officers ever decline to waltz. c) All my poultry are ducks. My poultry are not officers. Does (d) follow from (a), (b), and (c)? If not, is there a correct conclusion?

*e)d)

1 .4 Nested Quantifiers Introd udion In Section 1 .3 we defined the existential and universal quantifiers and showed how they can be used to represent mathematical statements. We also explained how they can be used to translate

1 .4 Nested Quantifiers

1-51

51

English sentences into logical expressions. In this section we will study nested quantifiers. Two quantifiers are nested if one is within the scope of the other, such as Vx3y(x + y = 0). Note that everything within the scope of a quantifier can be thought of as a propositional function. For example, Vx3y(x + y

= 0)

=

is the same thing as Vx Q (x), where Q(x) is 3yP (x , y), where P (x , y) is x + y O . Nested quantifiers commonly occur in mathematics and computer science. Although nested quantifiers can sometimes be difficult to understand, the rules we have already studied in Section 1 .3 can help us use them. To understand these statements involving many quantifiers, we need to unravel what the quantifiers and predicates that appear mean. This is illustrated in Examples I and 2. EXAMPLE 1

Assume that the domain for the variables x and y consists of all real numbers. The statement VxVy(x + y = y + x) says that x + y = y + x for all real numbers x and y. This is the commutative law for addition of real numbers. Likewise, the statement Vx 3y(x + y = 0) says that for every real number x there is a real number y such that x + y = O . This states that every real number has an additive inverse. Similarly, the statement VxVyVz(x + (y + z) = (x + y) + z) is the associative law for addition of real numbers.

EXAMPLE 2

Translate into English the statement VxVy« x

>

0) /\ (y


� , then (J2) 2 >

(J2) 2

= 2 > G) 2 = � ."

(D 2 . We know that J2 > � . Consequently,

Solution: Let p be the proposition "J2 > �" and q the proposition "2 > (�) 2 ." The premises of the argument are p ---+ q and p, and q is its conclusion. This argument is valid because it is constructed by using modus ponens, a valid argument form. However, one of its premises, J2 > �, is false. Consequently, we cannot conclude that the conclusion is true. Furthermore, .... note that the conclusion of this argument is false, because 2 < � . Table 1 lists the most important rules of inference for propositional logic. Exercises 9, 1 0, 1 5 , and 30 in Section 1 .2 ask for the verifications that these rules of inference are valid argument forms. We now give examples of arguments that use these rules of inference. In each argument, we first use propositional variables to express the propositions in the argument. We then show that the resulting argument form is a rule of inference from Table 1 . EXAMPLE 3

State which rule of inference is the basis of the following argument: "It is below freezing now. Therefore, it is either below freezing or raining now."

66

I / The Foundations: Logic and Proofs

1 - 66

TABLE 1 Rules of Inference. Name

Rule ofInference

Tautology

p p -+ q -:. q

[p

-.q p -+ q -:. -'p

[-.q

p -+ q q -+ r -:. p -+ r

[(p -+ q) 1\ (q -+ r)] -+ (p -+ r)

Hypothetical syllogism

p vq -'p -:. q

[(p V q)

Disjunctive syllogism

p -:. p v q

p -+ (p v q)

Addition

p I\ q -:. p

(p l\ q) -+ p

Simplification

p q -:. p 1\ q

[(p)

pvq -'p v r -:. q V r

[(p v q)

1\

(p -+ q)] -+ q

1\

1\

(p -+ q)] -+ -'p

1\

-'p] -+ q

(q )] -+ (p

1\

1\

q)

(-'P V r)] -+ (q V r)

Modus ponens

Modus tollens

Conjunction

Resolution

Solution: Let p be the proposition "It is below freezing now" and q the proposition "It is raining now." Then this argument is of the form p :. p v q This is an argument that uses the addition rule. EXAMPLE 4

State which rule of inference is the basis of the following argument: "It is below freezing and raining now. Therefore, it is below freezing now."

Solution: Let p be the proposition "It is below freezing now," and let q be the proposition "It is raining now." This argument is of the form

P I\ q p :. This argument uses the simplification rule.

1-67

1 .5 Rules of Inference 67

EXAMPLE 5

State which rule of inference is used in the argument: If it rains today, then we will not have a barbecue today. If we do not have a barbecue today, then we will have a barbecue tomorrow. Therefore, if it rains today, then we will have a barbecue tomorrow.

Solution: Let p be the proposition "It is raining today," let q be the proposition "We will not have a barbecue today," and let r be the proposition "We will have a barbecue tomorrow." Then this argument is of the form p -+ q q -+ r : . p -+ r

Hence, this argument is a hypothetical syllogism.

Using Rules of Inference to Build Arguments When there are many premises, several rules of inference are often needed to show that an argument is valid. This is illustrated by Examples 6 and 7, where the steps of arguments are displayed on separate lines, with the reason for each step explicitly stated. These examples also show how arguments in English can be analyzed using rules of inference. EXAMPLE 6

Show that the hypotheses "It is not sunny this afternoon and it is colder than yesterday," "We will go swimming only if it is sunny," "If we do not go swimming, then we will take a canoe trip," and "If we take a canoe trip, then we will be home by sunset" lead to the conclusion "We will be home by sunset."

Exam: C

Solution: Let p be the proposition "It is sunny this afternoon," q the proposition "It is colder than yesterday," r the proposition "We will go swimming," s the proposition "We will take a canoe trip," and t the proposition "We will be home by sunset." Then the hypotheses become -op 1\ q , r -+ p, -or -+ s, and s -+ t . The conclusion is simply t . We need to give a valid argument with hypotheses -op 1\ q , r -+ p, -or -+ s, and s -+ t and conclusion t . We construct an argument to show that our hypotheses lead to the desired conclusion as follows. Step 1 . -op 1\ q 2. -op 3. r -+ p 4. -or 5. -or -+ s

6. 7.

s

s

8. t

-+ t

Reason

Hypothesis Simplification using ( 1 ) Hypothesis Modus tollens using (2) and (3) Hypothesis Modus ponens using (4) and (5) Hypothesis Modus ponens using (6) and (7)

Note that we could have used a truth table to show that whenever each of the four hypotheses is true, the conclusion is also true. However, because we are working with five propositional ... variables, p, q , r, s, and t, such a truth table would have 32 rows. EXAMPLE 7

Show that the hypotheses "If you send me an e-mail message, then I will finish writing the program," "If you do not send me an e-mail message, then I will go to sleep early," and "If I go

68

1 / The Foundations: Logic and Proofs

1 - 68

to sleep early, then I will wake up feeling refreshed" lead to the conclusion "If I do not finish writing the program, then I will wake up feeling refreshed."

Solution: Let p be the proposition "You send me an e-mail message," q the proposition "I will finish writing the program," r the proposition "I will go to sleep early," and s the proposition "I will wake up feeling refreshed." Then the hypotheses are p -+ q , ""'p -+ r, and r -+ s . The desired conclusion is ..... q -+ s . We need to give a valid argument with hypotheses p -+ q , ""' p -+ r , and r -+ s and conclusion ..... q -+ s . This argument form shows that the hypotheses p lead to the desired conclusion. Step

Reason

1 . p -+ q 2 . ..... q -+ ""'p 3. ""'p -+ r 4 . .....q -+ r 5. r -+ s 6 . ..... q -+ s

Hypothesis Contrapositive of ( 1 ) Hypothesis Hypothetical syllogism using (2) and (3) Hypothesis Hypothetical syllogism using (4) and (5)

Resolution

Unks � �

Computer programs have been developed to automate the task of reasoning and proving theo­ rems. Many of these programs make use of a rule of inference known as resolution. This rule of inference is based on the tautology

«p v q) 1\ ( ..... p V r » -+ (q

V r).

(The verification that this is a tautology was addressed in Exercise 30 in Section 1 .2.) The final disjunction in the resolution rule, q v r , is called the resolvent. When we let q = r in this tautology, we obtain (p v q) 1\ ( .....p V q) -+ q . Furthermore, when we let r = F, we obtain (p v q) 1\ ( ..... p) -+ q (because q v F = q), which is the tautology on which the rule of disjunc­ tive syllogism is based. EXAMPLE S

Use resolution to show that the hypotheses "Jasmine is skiing or it is not snowing" and "It is snowing or Bart is playing hockey" imply that "Jasmine is skiing or Bart is playing hockey."

Extra � Examples �

Solution: Let p be the proposition "It is snowing," q the proposition "Jasmine is skiing," and r the proposition "Bart is playing hockey." We can represent the hypotheses as ""'p v q and p v r , respectively. Using resolution, the proposition q v r , "Jasmine i s skiing or Bart i s playing ... hockey," follows. Resolution plays an important role in programming languages based on the rules of logic, such as Prolog (where resolution rules for quantified statements are applied). Furthermore, it can be used to build automatic theorem proving systems. To construct proofs in propositional logic using resolution as the only rule of inference, the hypotheses and the conclusion must be expressed as clauses, where a clause is a disjunction of variables or negations of these variables. We can replace a statement in propositional logic that is not a clause by one or more equivalent statements that are clauses. For example, suppose we have a statement of the form p v (q 1\ r ) . Because p v (q 1\ r ) = (p V q) 1\ (p V r ) , we can replace the single statement p v (q 1\ r ) by two statements p v q and p v r , each of which is a clause. We can replace a statement of

1-69

1 .5 Rules of Inference

69

the form -'(p v q ) by the two statements -'p and -.q because De Morgan's law tells us that -'(p v q ) == -'p 1\ -'q . We can also replace a conditional statement p -+ q with the equivalent disjunction -'p v q . EXAMPLE 9

Show that the hypotheses (p

1\

q ) V r and r

-+ s

imply the conclusion p

v s.

Solution: We can rewrite the hypothesis (p 1\ q) V r as two clauses, p V r and q v r. We can also replace r -+ s by the equivalent clause -.r v s . Using the two clauses p V r and -'r v s , .... we can use resolution to conclude p v s .

Fallacies

unkS �

Several common fallacies arise in incorrect arguments. These fallacies resemble rules of infer­ ence but are based on contingencies rather than tautologies. These are discussed here to show the distinction between correct and incorrect reasoning. The proposition [(p -+ q) 1\ q] -+ p is not a tautology, because it is false when p is false and q is true. However, there are many incorrect arguments that treat this as a tautology. In other words, they treat the argument with premises p -+ q and q and conclusion p as a valid argument form, which it is not. This type of incorrect reasoning is called the fallacy of affirming the conclusion.

EXAMPLE 10

Is the following argument valid? If you do every problem in this book, then you will learn discrete mathematics. You learned discrete mathematics. Therefore, you did every problem in this book.

Solution: Let p be the proposition "You did every problem in this book." Let q be the proposition "You learned discrete mathematics." Then this argument is of the form: if p -+ q and q, then p. This is an example of an incorrect argument using the fallacy of affirming the conclusion. Indeed, it is possible for you to learn discrete mathematics in some way other than by doing every problem in this book. (You may learn discrete mathematics by reading, listening to lectures, .... doing some, but not all, the problems in this book, and so on.) The proposition [(p -+ q ) 1\ -'p] -+ -.q is not a tautology, because it is false when p is false and q is true. Many incorrect arguments use this incorrectly as a rule of inference. This type of incorrect reasoning is called the fallacy of denying the hypothesis. EXAMPLE 11

Let p and q be as in Example 1 0. If the conditional statement p -+ q is true, and -'p is true, is it correct to conclude that -.q is true? In other words, is it correct to assume that you did not learn discrete mathematics if you did not do every problem in the book, assuming that if you do every problem in this book, then you will learn discrete mathematics?

Solution: It is possible that you learned discrete mathematics even if you did not do every problem in this book. This incorrect argument is of the form p -+ q and -'p imply -'q , which .... is an example of the fallacy of denying the hypothesis.

70

1 / The Foundations: Logic and Proofs

1 - 70

Rules of Inference for Quantified Statements We have discussed rules of inference for propositions. We will now describe some important rules of inference for statements involving quantifiers. These rules of inference are used extensively in mathematical arguments, often without being explicitly mentioned. Universal instantiation is the rule of inference used to conclude that P (c) is true, where c is a particular member of the domain, given the premise Vx P (x). Universal instantiation is used when we conclude from the statement "All women are wise" that "Lisa is wise," where Lisa is a member of the domain of all women. Universal generalization is the rule of inference that states that Vx P(x) is true, given the premise that P (c) is true for all elements c in the domain. Universal generalization is used when we show that Vx P (x) is true by taking an arbitrary element c from the domain and showing that P (c) is true. The element c that we select must be an arbitrary, and not a specific, element of the domain. That is, when we assert from Vx P (x) the existence of an element c in the domain, we have no control over c and cannot make any other assumptions about c other than it comes from the domain. Universal generalization is used implicitly in many proofs in mathematics and is seldom mentioned explicitly. However, the error of adding unwarranted assumptions about the arbitrary element c when universal generalization is used is all too common in incorrect reasoning. Existential instantiation is the rule that allows us to conclude that there is an element c in the domain for which P (c) is true if we know that 3x P (x) is true. We cannot select an arbitrary value of c here, but rather it must be a c for which P(c) is true. Usually we have no knowledge of what c is, only that it exists. Because it exists, we may give it a name (c) and continue our argument. Existential generalization is the rule of inference that is used to conclude that 3x P(x) is true when a particular element c with P (c) true is known. That is, if we know one element c in the domain for which P (c) is true, then we know that 3x P (x) is true. We summarize these rules of inference in Table 2. We will illustrate how one of these rules of inference for quantified statements is used in Example 1 2.

EXAMPLE 12

Show that the premises "Everyone in this discrete mathematics class has taken a course in computer science" and "Marla is a student in this class" imply the conclusion "Marla has taken a course in computer science."

TABLE 2 Rules of Inference for Quantified Statements. Rule ofInference

Name

Vx P(x) :. P(c)

Universal instantiation

P(c) for an arbitrary c :. Vx P(x)

Universal generalization

3x P(x) :. P(c) for some element c

Existential instantiation

P(c) for some element c :. 3x P(x)

Existential generalization

1 - 71

l .5 Rules of lnference

71

Solution: Let D(x) denote "x is in this discrete mathematics class," and let C (x) denote "x has taken a course in computer science." Then the premises are Vx(D(x) ---+ C (x» and D(MarIa). The conclusion is C (Marla). The following steps can be used to establish the conclusion from the premises.

EXAMPLE 13

Step

Reason

1. 2. 3. 4.

Premise Universal instantiation from ( 1 ) Premise Modus ponens from (2) and (3)

Vx(D(x) ---+ C (x» D(Marla) ---+ C (Marla) D(MarIa) C(MarIa)

Show that the premises "A student in this class has not read the book," and "Everyone in this class passed the first exam" imply the conclusion "Someone who passed the first exam has not read the book."

Solution: Let C (x) be "x is in this class," B (x) be "x has read the book," and P (x) be "x passed the first exam." The premises are 3x (C (x ) 1\ -.B (x» and Vx (C (x) ---+ P (x» . The conclusion is 3x (P(x) 1\ -.B(x» . These steps can be used to establish the conclusion from the premises. Step

Reason

1. 2. 3. 4. 5. 6. 7.

Premise Existential instantiation from ( l ) Simplification from (2) Premise Universal instantiation from (4) Modus ponens from (3) and (5) Simplification from (2) Conjunction from (6) and (7) Existential generalization from (8)

3x (C(x) 1\ -.B(x» C(a) 1\ -.B(a) C(a) Vx(C (x) ---+ P (x» C (a) ---+ pea) pea) -.B(a) 8. p ea) 1\ -.B(a) 9 . 3x(P(x) 1\ -.B(x»

Combining Rules of Inference for Propositions and Quantified Statements We have developed rules of inference both for propositions and for quantified statements. Note that in our arguments in Examples 1 2 and 1 3 we used both universal instantiation, a rule of inference for quantified statements, and modus ponens, a rule of inference for propositional logic. We will often need to use this combination of rules of inference. Because universal instantiation and modus ponens are used so often together, this combination of rules is sometimes called universal modus ponens. This rule tells us that ifVx(P(x ) ---+ Q (x» is true, and if P (a) is true for a particular element a in the domain of the universal quantifier, then Q (a) must also be true. To see this, note that by universal instantiation, p ea) ---+ Q (a) is true. Then, by modus ponens, Q (a) must also be true. We can describe universal modus ponens as follows: Vx(P(x) ---+ Q (x» P (a), where a is a particular element in the domain : . Q(a) Universal modus ponens is commonly used in mathematical arguments. This is illustrated in Example 14. EXAMPLE 14

Assume that "For all positive integers n, if n is greater than 4, then n 2 is less than 2n " is true. Use universal modus ponens to show that 1 002 < 2 100 •

72

I I

The Foundations: Logic and Proofs

1 - 72

Solution: Let P (n) denote "n > 4" and Q (n) denote "n 2 < 2n ." The statement "For all positive integers n , ifn is greater than 4, then n 2 is less than 2n " can be represented by Vn(P(n) --+ Q(n» , where the domain consists of all positive integers. We are assuming that Vn(P(n) --+ Q(n» is true. Note that P ( l 00) is true because 1 00 > 4. It follows by universal modus ponens that Q(n) � is true, namely that 1 002 < 2 100 • Another useful combination of a rule of inference from propositional logic and a rule of inference for quantified statements is universal modus tollens. Universal modus toll ens combines universal instantiation and modus tollens and can be expressed in the following way: Vx(P(x) --+ Q (x» -' Q (a ) , where a is a particular element in the domain : . -, P (a )

We leave the verification of universal modus tollens to the reader (see Exercise 25). Exercise 26 develops additional combinations of rules of inference in propositional logic and quantified statements.

Exercises 1 . Find the argument form for the following argument and

determine whether it is valid. Can we conclude that the conclusion is true if the premises are true? If Socrates is human, then Socrates is mortal. Socrates is human. . . . Socrates is mortal. 2. Find the argument form for the following argument and

determine whether it is valid. Can we conclude that the conclusion is true if the premises are true? If George does not have eight legs, then he is not an insect. George is an insect. . ' . George has eight legs. 3. What rule of inference is used in each of these

arguments?

a) Alice is a mathematics major. Therefore, Alice is ei­ ther a mathematics major or a computer science major.

b) Jerry is a mathematics major and a computer science

major. Therefore, Jerry is a mathematics major. c) If it is rainy, then the pool will be closed. It is rainy. Therefore, the pool is closed. If it snows today, the university will close. The uni­ versity is not closed today. Therefore, it did not snow today. e) If ! go swimming, then I will stay in the sun too long. If! stay in the sun too long, then I will sunburn. There­ fore, if I go swimming, then I will sunburn. 4. What rule of inference is used in each ofthese arguments?

d)

a) Kangaroos live in Australia and are marsupials. There­ fore, kangaroos are marsupials.

b) It is either hotter than 1 00 degrees today or the pollu­

tion is dangerous. It is less than 1 00 degrees outside today. Therefore, the pollution is dangerous. c) Linda is an excellent swimmer. If Linda is an excellent swimmer, then she can work as a lifeguard. Therefore, Linda can work as a lifeguard. Steve will work at a computer company this summer. Therefore, this summer Steve will work at a computer company or he will be a beach bum. e) If! work all night on this homework, then I can answer all the exercises. If! answer all the exercises, I will un­ derstand the material. Therefore, if I work all night on this homework, then I will understand the material. 5. Use rules ofinference to show that the hypotheses "Randy works hard," "If Randy works hard, then he is a dull boy," and "If Randy is a dull boy, then he will not get the job" imply the conclusion "Randy will not get the job." 6. Use rules of inference to show that the hypotheses "If it does not rain or if it is not foggy, then the sailing race will be held and the lifesaving demonstration will go on," "If the sailing race is held, then the trophy will be awarded," and "The trophy was not awarded" imply the conclusion "It rained." 7. What rules of inference are used in this famous argu­ ment? "All men are mortal. Socrates is a man. Therefore, Socrates is mortal." 8. What rules of inference are used in this argument? "No man is an island. Manhattan is an island. Therefore, Manhattan is not a man."

d)

1 .5 Rules of Inference 73

1 - 73

9. For each of these sets of premises, what relevant conclu­ sion or conclusions can be drawn? Explain the rules of in­ ference used to obtain each conclusion from the premises. a) "If I take the day off, it either rains or snows." "I took Tuesday off or I took Thursday off." "It was sunny on Tuesday." "It did not snow on Thursday." b) "If ! eat spicy foods, then I have strange dreams." "I have strange dreams if there is thunder while I sleep." "I did not have strange dreams." c) "I am either clever or lucky." "I am not lucky." "If I am lucky, then I will win the lottery." d) "Every computer science major has a personal com­ puter." "Ralph does not have a personal computer." "Ann has a personal computer." "What is good for corporations is good for the United States." "What is good for the United States is good for you." "What is good for corporations is for you to buy lots of stuff." t) "All rodents gnaw their food." "Mice are rodents." "Rabbits do not gnaw their food." "Bats are not ro­ dents."

e)

10. For each of these sets of premises, what relevant conclu­ sion or conclusions can be drawn? Explain the rules of in­ ference used to obtain each conclusion from the premises.

a)

"If! play hockey, then I am sore the next day." "I use the whirlpool if I am sore." "I did not use the whirlpool." b) "If! work, it is either sunny or partly sunny." "I worked last Monday or I worked last Friday." "It was not sunny on Tuesday." "It was not partly sunny on Friday." c) "All insects have six legs." "Dragonflies are insects." "Spiders do not have six legs." "Spiders eat dragon­ flies." "Every student has an Internet account." "Homer does not have an Internet account." "Maggie has an Internet account." "All foods that are healthy to eat do not taste good." "Tofu is healthy to eat." "You only eat what tastes good." "You do not eat tofu." "Cheeseburgers are not healthy to eat." t) "I am either dreaming or hallucinating." "I am not dreaming." "If I am hallucinating, I see elephants run­ ning down the road." 1 1 . Show that the argument form with premises P I , P2 , . . . , Pn and conclusion q -+ r is valid ifthe argument form with premises P I , P2 , . . . , Pn , q , and conclusion r is valid. 12. Show that the argument form with premises /\ t) -+ (r v s), q -+ (u /\ t), u -+ and --.s and conclusion q -+ r is valid by first using Exercise 1 1 and then using rules of inference from Table 1 . 13. For each of these arguments, explain which rules of infer­ ence are used for each step.

d) e)

p,

a)

(p

"Doug, a student in this class, knows how to write programs in JAVA. Everyone who knows how to write programs in JAVA can get a high-paying job. There­ fore, someone in this class can get a high-paying job."

b) "Somebody in this class enjoys whale watching. Every person who enjoys whale watching cares about ocean pollution. Therefore, there is a person in this class who cares about ocean pollution." c) "Each of the 93 students in this class owns a personal computer. Everyone who owns a personal computer can use a word processing program. Therefore, Zeke, a student in this class, can use a word processing pro­ gram." d) "Everyone in New Jersey lives within 50 miles of the ocean. Someone in New Jersey has never seen the ocean. Therefore, someone who lives within 50 miles of the ocean has never seen the ocean." 14. For each of these arguments, explain which rules of infer­ ence are used for each step. a) "Linda, a student in this class, owns a red convertible. Everyone who owns a red convertible has gotten at least one speeding ticket. Therefore, someone in this class has gotten a speeding ticket." b) "Each of five roommates, Melissa, Aaron, Ralph, Ve­ neesha, and Keeshawn, has taken a course in discrete mathematics. Every student who has taken a course in discrete mathematics can take a course in algorithms. Therefore, all five roommates can take a course in al­ gorithms next year." c) "All movies produced by John Sayles are wonder­ ful. John Sayles produced a movie about coal min­ ers. Therefore, there is a wonderful movie about coal miners." d) "There is someone in this class who has been to France. Everyone who goes to France visits the Lou­ vre. Therefore, someone in this class has visited the Louvre." 1 5. For each of these arguments determine whether the argu­ ment is correct or incorrect and explain why.

a)

All students in this class understand logic. Xavier is a student in this class. Therefore, Xavier understands logic. b) Every computer science maj or takes discrete mathe­ matics. Natasha is taking discrete mathematics. There­ fore, Natasha is a computer science major. c) All parrots like fruit. My pet bird is not a parrot. There­ fore, my pet bird does not like fruit. d) Everyone who eats granola every day is healthy. Linda is not healthy. Therefore, Linda does not eat granola every day. 16. For each of these arguments determine whether the argu­ ment is correct or incorrect and explain why.

a)

Everyone enrolled in the university has lived in a dor­ mitory. Mia has never lived in a dormitory. Therefore, Mia is not enrolled in the university. b) A convertible car is fun to drive. Isaac's car is not a convertible. Therefore, Isaac's car is not fun to drive. c) Quincy likes all action movies. Quincy likes the movie Eight Men Out. Therefore, Eight Men Out is an action movie.

1 - 74

1 / The Foundations: Logic and Proofs

74

d)

All iobstennen set at least a dozen traps. Hamilton is a lobstennan. Therefore, Hamilton sets at least a dozen traps. 1 7. What is wrong with this argument? Let H(x ) be "x is happy." Given the premise 3x H (x), we conclude that H(Lola). Therefore, Lola is happy.

y)

18. What is wrong with this argument? Let S(x , be "x is shorter than Given the premise 3s S(s , Max), it follows that S(Max, Max). Then by existential generalization it follows that 3x S(x , x), so that someone is shorter than himself.

y ."

19. Detennine whether each of these arguments is valid. Ifan argument is correct, what rule of inference is being used? If it is not, what logical error occurs? a) If n is a real number such that n > 1 , then n 2 > 1 . Suppose that n 2 > 1 . Then n > 1 . b) Ifn is a real number with n > 3 , then n 2 > 9 . Suppose that n 2 :s 9. Then n :s 3 . c) Ifn is a real number with n > 2, then n 2 > 4 . Suppose that n :s 2. Then n 2 :s 4. 20. Detennine whether these are valid arguments. a) If x is a positive real number, then x 2 is a positive real number. Therefore, if a 2 is positive, where a is a real number, then a is a positive real number. b) If x 2 =1= 0, where x is a real number, then x =1= O. Let a be a real number with a 2 =1= 0; then a =1= O. 21. Which rules of inference are used to establish the conclusion of Lewis Carroll's argument described in Example 26 of Section 1 .3 ? 2 2 . Which rules of inference are used t o establish the conclu­ sion of Lewis Carroll's argument described in Example of Section 1 .3 ?

27

23. Identify the error o r errors i n this argument that sup­ posedly shows that if 3x P (x ) 1\ 3x Q (x ) is true then 3x( P (x) 1\ Q(x» is true. 3x P (x ) 1\ 3x Q(x) Premise Simplification from ( 1 ) 3x P (x ) Existential instantiation from (2) P ee) 3x Q (x ) Simplification from ( 1 ) 5 . Q (e) Existential instantiation from (4) 6. P ee) 1\ Q (e) Conjunction from (3) and (5) Existential generalization 3x(P(x) 1\ Q (x » 24. Identify the error or errors in this argument that sup­ posedly shows that if Vx ( P (x ) v Q (x » is true then Vx P(x) v Vx Q (x ) is true. Premise 1 . Vx (P(x ) v Q (x» Universal instantiation from ( 1 ) 2. P ee) v Q (e) Simplification from (2) 3 . Pee) Universal generalization from (3) 4. Vx P (x ) 5. Q(e) Simplification from (2) 6. Vx Q(x) Universal generalization from (5) Vx(P(x) v Vx Q (x » Conjunction from (4) and (6) 25. Justify the rule of universal modus tollens by showing that the premises Vx( P (x) � Q(x» and --. Q (a) for a partic­ ular element a in the domain, imply --. P (a). 1. 2. 3. 4.

7.

7.

26. Justify the rule of universal transitivity, which states that ifVx ( P (x) � Q(x » and Vx(Q(x) � R (x » are true, then Vx(P(x ) � R (x » is true, where the domains of all quantifiers are the same. 27. Use rules of inference to show that ifVx(P(x) � (Q(x) 1\ Sex»�) and Vx(P(x ) 1\ R(x» are true, then Vx(R(x) 1\ Sex»� is true. 28. Use rules of inference to show that if Vx(P(x) v Q (x » and Vx« --. P (x) 1\ Q(x» � R (x» are true, then Vx(--.R(x) � P (x» is also true, where the domains of all quantifiers are the same. 29. Use rules of inference to show that if Vx(P(x) v Q(x» , Vx(--. Q (x ) v Sex»�, Vx(R(x) � --.S(x» , and 3x --. P (x) are true, then 3x --.R(x) is true. 30. Use resolution to show the hypotheses "Allen is a bad boy or Hillary is a good girl" and "Allen is a good boy or David is happy" imply the conclusion "Hillary is a good girl or David is happy." 3 1 . Use resolution to show that the hypotheses "It is not rain­ ing or Yvette has her umbrella," "Yvette does not have her umbrella or she does not get wet," and "It is raining or Yvette does not get wet" imply that "Yvette does not get wet." 32. Show that the equivalence p 1\ --'p == F can be derived using resolution together with the fact that a condi­ tional statement with a false hypothesis is true. [Hint: Let q = r = F in resolution.] 33. Use resolution to show that the compound proposition (p v q) 1\ (--'P V q ) 1\ (p V --.q ) 1\ (--'P V --'q ) is not sat­ isfiable. *34. The Logic Problem, taken from WFF 'N PROOF, The Game ofLogic, has these two assumptions: 1. "Logic is difficult or not many students like logic." 2. "If mathematics is easy, then logic is not difficult." By translating these assumptions into statements involv­ ing propositional variables and logical connectives, deter­ mine whether each of the following are valid conclusions of these assumptions: a) That mathematics is not easy, if many students like logic. b) That not many students like logic, if mathematics is not easy. c) That mathematics is not easy or logic is difficult. That logic is not difficult or mathematics is not easy. e) That if not many students like logic, then either math­ ematics is not easy or logic is not difficult. *35. Detennine whether this argument, taken from Kalish and Montague [KaM064] , is valid.

d)

If Superman were able and willing to prevent evil, he would do so. If Supennan were unable to prevent evil, he would be impotent; if he were unwilling to prevent evil, he would be malevolent. Supennan does not prevent evil. If Supennan exists, he is nei­ ther impotent nor malevolent. Therefore, Supennan does not exist.

1 .6 Introduction to Proofs

1 - 75

75

1.6 Introduction to Proofs Introduction In this section we introduce the notion of a proof and describe methods for constructing proofs. A proof is a valid argument that establishes the truth of a mathematical statement. A proof can use the hypotheses of the theorem, if any, axioms assumed to be true, and previously proven theorems. Using these ingredients and rules of inference, the final step of the proof establishes the truth of the statement being proved. In our discussion we move from formal proofs of theorems toward more informal proofs. The arguments we introduced in Section 1 .5 to show that statements involving propositions and quantified statements are true were formal proofs, where all steps were supplied, and the rules for each step in the argument were given. However, formal proofs of useful theorems can be extremely long and hard to follow. In practice, the proofs of theorems designed for human consumption are almost always informal proofs, where more than one rule of inference may be used in each step, where steps may be skipped, where the axioms being assumed and the rules of inference used are not explicitly stated. Informal proofs can often explain to humans why theorems are true, while computers are perfectly happy producing formal proofs using automated reasoning systems. The methods of proof discussed in this chapter are important not only because they are used to prove mathematical theorems, but also for their many applications to computer science. These applications include verifying that computer programs are correct, establishing that operating systems are secure, making inferences in artificial intelligence, showing that system specifica­ tions are consistent, and so on. Consequently, understanding the techniques used in proofs is essential both in mathematics and in computer science.

Some Terminology

UDkS it:'l

Formally, a theorem is a statement that can be shown to be true. In mathematical writing, the term theorem is usually reserved for a statement that is considered at least somewhat important. Less important theorems sometimes are called propositions. (Theorems can also be referred to as facts or results.) A theorem may be the universal quantification of a conditional statement with one or more premises and a conclusion. However, it may be some other type of logical statement, as the examples later in this chapter will show. We demonstrate that a theorem is true with a proof. A proof is a valid argument that establishes the truth of a theorem. The statements used in a proof can include axioms (or postulates), which are statements we assume to be true (for example, see Appendix 1 for axioms for the real numbers), the premises, if any, of the theorem, and previously proven theorems. Axioms may be stated using primitive terms that do not require definition, but all other terms used in theorems and their proofs must be defined. Rules of inference, together with definitions of terms, are used to draw conclusions from other assertions, tying together the steps of a proof. In practice, the final step of a proof is usually just the conclusion of the theorem. However, for clarity, we will often recap the statement ofthe theorem as the final step of a proof. A less important theorem that is helpful in the proof of other results is called a lemma (plural lemmas or lemmata). Complicated proofs are usually easier to understand when they are proved using a series of lemmas, where each lemma is proved individually. A corollary is a theorem that can be established directly from a theorem that has been proved. A conjecture is a statement that is being proposed to be a true statement, usually on the basis of some partial evidence, a heuristic argument, or the intuition of an expert. When a proof of a conjecture is found, the conjecture becomes a theorem. Many times conjectures are shown to be false, so they are not theorems.

76

1 - 76

1 / The Foundations: Logic and Proofs

Understanding How Theorems Are Stated Extra � Examples �

Before we introduce methods for proving theorems, we need to understand how many math­ ematical theorems are stated. Many theorems assert that a property holds for all elements in a domain, such as the integers or the real numbers. Although the precise statement of such theorems needs to include a universal quantifier, the standard convention in mathematics is to omit it. For example, the statement "If x

>

y,

where x and y are positive real numbers, then x 2

>

y2 ."

really means "For all positive real numbers x and y, if x

>

y, then x 2

>

y2 ."

Furthermore, when theorems of this type are proved, the law of universal instantiation is often used without explicit mention. The first step of the proof usually involves selecting a general element of the domain. Subsequent steps show that this element has the property in question. Finally, universal generalization implies that the theorem holds for all members of the domain.

Methods of Proving Theorems

Assessmenl

iC

We now tum our attention to proofs of mathematical theorems. Proving theorems can be difficult. We need all the ammunition that is available to help us prove different results. We now introduce a battery of different proof methods. These methods should become part of your repertoire for proving theorems. To prove a theorem ofthe form Vx (P(x) --+ Q(x» , our goal is to show that P(c) --+ Q(c) is true, where c is an arbitrary element of the domain, and then apply universal generalization. In this proof, we need to show that a conditional statement is true. Because of this, we now focus on methods that show that conditional statements are true. Recall that p --+ q is true unless p is true but q is false. Note that when the statement p --+ q is proved, it need only be shown that q is true if p is true. The following discussion will give the most common techniques for proving conditional statements. Later we will discuss methods for proving other types of statements. In this section, and in Section 1 .7, we will develop an arsenal of many different proof techniques that can be used to prove a wide variety of theorems. When you read proofs, you will often find the words "obviously" or "clearly." These words indicate that steps have been omitted that the author expects the reader to be able to fill in. Unfortunately, this assumption is often not warranted and readers are not at all sure how to fill in the gaps. We will assiduously try to avoid using these words and try not to omit too many steps. However, if we included all steps in proofs, our proofs would often be excruciatingly long.

Direct Proofs A direct proof of a conditional statement p --+ q is constructed when the first step is the assumption that p is true; subsequent steps are constructed using rules of inference, with the final step showing that q must also be true. A direct proof shows that a conditional statement p --+ q is true by showing that if p is true, then q must also be true, so that the combination p true and q false never occurs. In a direct proof, we assume that p is true and use axioms, definitions, and previously proven theorems, together with rules of inference, to show that q must also be true. You will find that direct proofs of many results are quite straightforward, with a fairly obvious sequence of steps leading from the hypothesis to the conclusion. However, direct

1-77

1 .6 Introduction to Proofs

77

proofs sometimes require particular insights and can be quite tricky. The first direct proofs we present here are quite straightforward; later in the text you will see some that are less obvious. We will provide examples of several different direct proofs. Before we give the first example, we need a definition.

DEFINITION 1

EXAMPLE 1

Extra � Examples �

EXAMPLE 2

The integer n is even ifthere exists an integer k such that n = 2k, and n is odd ifthere exists an integer k such that n = 2k + 1 . (Note that an integer is either even or odd, and no integer is both even and odd.)

Give a direct proof of the theorem "If n is an odd integer, then n 2 is odd."

Solution: Note that this theorem states Vn P« n) -+ Q(n)), where P(n) is "n is an odd integer" and Q(n) is "n 2 is odd." As we have said, we will follow the usual convention in mathematical proofs by showing that P (n) implies Q(n), and not explicitly using universal instantiation. To begin a direct proof of this theorem, we assume that the hypothesis of this conditional statement is true, namely, we assume that n is odd. By the definition of an odd integer, it follows that n = 2k + I , where k is some integer. We want to show that n 2 is also odd. We can square both sides of the equation n 2k + I to obtain a new equation that expresses n 2 • When we do this, we find that n 2 = (2k + 1 ) 2 = 4k2 + 4k + I = 2(2k2 + 2k) + I . By the definition of an odd integer, we can conclude that n 2 is an odd integer (it is one more than twice an integer). ... Consequently, we have proved that if n is an odd integer, then n 2 is an odd integer.

=

Give a direct proof that if m and n are both perfect squares, then nm is also a perfect square. (An integer a is a perfect square if there is an integer b such that a = b2 . )

Solution: To produce a direct proof of this theorem, we assume that the hypothesis of this conditional statement is true, namely, we assume that m and n are both perfect squares. By the definition of a perfect square, it follows that there are integers s and t such that m s 2 and n = t 2 . The goal of the proof is to show that mn must also be a perfect square when m and n are; looking ahead we see how we can show this by multiplying the two equations m = s 2 and n = t 2 together. This shows that mn = s 2 t 2 , which implies that mn = (s t)2 (using commutativity and associativity of multiplication). By the definition of perfect square, it follows that mn is also a perfect square, because it is the square of s t, which is an integer. We have proved that if m and ... n are both perfect squares, then mn is also a perfect square.

=

Proof by Contraposition Direct proofs lead from the hypothesis of a theorem to the conclusion. They begin with the premises, continue with a sequence of deductions, and end with the conclusion. However, we will see that attempts at direct proofs often reach dead ends. We need other methods of proving theo­ rems ofthe form Vx (P (x ) -+ Q (x )) . Proofs oftheorems ofthis type that are not direct proofs, that is, that do not start with the hypothesis and end with the conclusion, are called indirect proofs. An extremely useful type of indirect proof is known as proof by contraposition. Proofs by contraposition make use of the fact that the conditional statement p -+ q is equivalent to its contrapositive, -.q -+ -'p. This means that the conditional statement p -+ q can be proved by showing that its contrapositive, -.q -+ -'p, is true. In a proof by contraposition of p -+ q , we take -.q as a hypothesis, and using axioms, definitions, and previously proven theorems, together with rules of inference, we show that -'p must follow. We will illustrate proof by contraposition

78

1 - 78

1 / The Foundations: Logic and Proofs

with two examples. These examples show that proof by contraposition can succeed when we cannot easily find a direct proof. EXAMPLE 3

Exam: C

Prove that if n is an integer and 3 n + 2 is odd, then n is odd.

Solution: We first attempt a direct proof. To construct a direct proof, we first assume that 3 n + 2 is an odd integer. This means that 3 n + 2 2k + 1 for some integer k. Can we use this fact to show that n is odd? We see that 3 n + 1 2k, but there does not seem to be any direct way to conclude that n is odd. Because our attempt at a direct proof failed, we next try a proof by contraposition. The first step in a proof by contraposition is to assume that the conclusion of the conditional statement "If 3 n + 2 is odd, then n is odd" is false; namely, assume that n is even. Then, by the definition of an even integer, n 2k for some integer k. Substituting 2k for n , we find that 3 n + 2 3(2k) + 2 6k + 2 2(3k + 1). This tells us that 3 n + 2 is even (because it is a multiple of 2), and therefore not odd. This is the negation of the hypothesis of the theorem. Because the negation of the conclusion of the conditional statement implies that the hypothesis is false, the original conditional statement is true. Our proof by contraposition succeeded; we .... have proved the theorem "If 3 n + 2 is odd, then n is odd."

= =

=

EXAMPLE 4

Prove that if n

=

=

=

= ab, where a and b are positive integers, then a :::: .jfi or b :::: .jfi.

Solution: Because there is no obvious way of showing that a :::: .jfi or b :::: .jfi directly from the equation n ab, where a and b are positive integers, we attempt a proof by contraposition. The first step in a proofby contraposition is to assume that the conclusion of the conditional statement "If n ab, where a and b are positive integers, then a :::: .jfi or b :::: .jfi" is false. That is, we assume that the statement (a :::: .jfi) V (b :::: .jfi) is false. Using the meaning of disjunction together with De Morgan's law, we see that this implies that both a :::: .jfi and b :::: .jfi are false. This implies that a > .jfi and b > .jfi. We can multiply these inequalities together (using the fact that if 0 < t and 0 < v, then su < tv) to obtain ab > .jfi . .jfi n . This shows that ab =I- n , which contradicts the statement n abo Because the negation of the conclusion of the conditional statement implies that the hy­ pothesis is false, the original conditional statement is true. Our proof by contraposition suc­ ceeded; we have proved that if n ab, where a and b are positive integers, then a :::: .jfi or .... b :::: .jfi.

=

=

=

=

=

VACUOUS AND TRIVIAL PROOFS We can quickly prove that a conditional statement p -+ q is true when we know that p is false, because p -+ q must be true when p is false. Consequently, if we can show that p is false, then we have a proof, called a vacuous proof, of the conditional statement p -+ q . Vacuous proofs are often used to establish special cases of theorems that state that a conditional statement is true for all positive integers [i.e., a theorem of the kind Vn P (n), where p en) is a propositional function] . Proof techniques for theorems of this kind will be discussed in Section 4. 1 . EXAMPLE 5

Show that the proposition P (O) is true, where p en) is "If n consists of all integers.

>

1 , then n 2

>

n" and the domain

Solution: Note that P (O) is "If 0 > 1 , then 02 > 0." We can show P(O) using a vacuous proof, .... because the hypothesis 0 > 1 is false. This tells us that P (O) is automatically true.

1 - 79

1 .6 Introduction to Proofs

79

Remark: The fact that the conclusion of this conditional statement, 02

> 0, is false is irrelevant to the truth value of the conditional statement, because a conditional statement with a false hypothesis is guaranteed to be true.

We can also quickly prove a conditional statement p --+ q if we know that the conclusion q is true. By showing that q is true, it follows that p --+ q must also be true. A proof of p --+ q that uses the fact that q is true is called a trivial proof. Trivial proofs are often important when special cases of theorems are proved (see the discussion of proof by cases in Section 1 .7) and in mathematical induction, which is a proof technique discussed in Section 4. 1 .

EXAMPLE 6

Let P(n) be "If a and b are positive integers with a ::: b, then a n ::: b n ," where the domain consists of all integers. Show that P (O) is true.

Solution: The proposition P(O) is "If a ::: b, then a o ::: b O ." Because a o = b O = I , the conclusion of the conditional statement "If a ::: b, then a O ::: b O " is true. Hence, this conditional statement, which is P (O), is true. This is an example of a trivial proof. Note that the hypothesis, which is .... the statement "a ::: b," was not needed in this proof. A LITILE PROOF STRATEGY We have described two important approaches for proving theorems of the form Vx(P(x) --+ Q(x» : direct proof and proofby contraposition. We have also given examples that show how each is used. However, when you are presented with a theorem of the form Vx(P(x) --+ Q(x» , which method should you use to attempt to prove it? We will provide a few rules of thumb here; in Section 1 .7 we will discuss proof strategy at greater length. When you want to prove a statement of the form Vx(P(x) --+ Q(x», first evaluate whether a direct proof looks promising. Begin by expanding the definitions in the hypotheses. Start to reason using these hypotheses, together with axioms and available theorems. If a direct proof does not seem to go anywhere, try the same thing with a proof by contraposition. Recall that in a proof by contraposition you assume that the conclusion of the conditional statement is false and use a direct proof to show this implies that the hypothesis must be false. We illustrate this strategy in Examples 7 and 8. Before we present our next example, we need a definition.

DEFINITION 2

EXAMPLE 7

The real number r is rational if there exist integers p and q with q =f:. 0 such that r = p / q. A real number that is not rational is called irrational.

Prove that the sum of two rational numbers is rational. (Note that if we include the implicit quantifiers here, the theorem we want to prove is "For every real number r and every real number s , if r and s are rational numbers, then r + s is rational.)

Solution: We first attempt a direct proof. To begin, suppose that r and s are rational numbers. From the definition of a rational number, it follows that there are integers p and q , with q =f:. 0, such that r = p / q , and integers t and u, with u =f:. 0, such that s = t / u . Can we use this information to show that r + s is rational? The obvious next step is to add r = p /q and s = t /u, to obtain P t pu + q t r + s = - + - = =----=-u qu q Because q =f:. 0 and u =f:. 0 , it follows that q u =f:. O . Consequently, we have expressed r + s as the ratio of two integers, pu + q t and qu, where qu =f:. O. This means that r + s is rational. We have proved that the sum of two rational numbers is rational; our attempt to find a direct proof .... succeeded.

80

I

/ The Foundations: Logic and Proofs

EXAMPLE 8

1 -80

Prove that if n is an integer and n 2 is odd, then n is odd.

Solution: We first attempt a direct proof. Suppose that n is an integer and n 2 is odd. Then, there exists an integer k such that n 2 2k + 1 . Can we use this information to show that n is odd? There seems to be no obvious approach to show that n is odd because solving for n produces the equation n = ±J2k + 1 , which is not terribly useful. Because this attempt to use a direct proof did not bear fruit, we next attempt a proof by contraposition. We take as our hypothesis the statement that n is not odd. Because every integer is odd or even, this means that n is even. This implies that there exists an integer k such that n 2k. To prove the theorem, we need to show that this hypothesis implies the conclusion that n 2 is not odd, that is, that n 2 is even. Can we use the equation n 2k to achieve this? By squaring both sides ofthis equation, we obtain n 2 = 4k2 = 2(2k2 ), which implies that n 2 is also even because n 2 2t , where t = 2k2 . We have proved that if n is an integer and n 2 is odd, then ... n is odd. Our attempt to find a proof by contraposition succeeded.

=

=

=

=

Proofs by Contradiction Suppose we want to prove that a statement p is true. Furthermore, suppose that we can find a contradiction q such that -'p -+ q is true. Because q is false, but -'p -+ q is true, we can conclude that -'p is false, which means that p is true. How can we find a contradiction q that might help us prove that p is true in this way? Because the statement r 1\ -'r is a contradiction whenever r is a proposition, we can prove that p is true if we can show that -'p -+ (r 1\ -.r ) is true for some proposition r . Proofs of this type are called proofs by contradiction. Because a proofby contradiction does not prove a result directly, it is another type of indirect proof. We provide three examples of proofby contradiction. The first is an example of an application of the pigeonhole principle, a combinatorial technique that we will cover in depth in Section 5.2. EXAMPLE 9

Show that at least four of any 22 days must fall on the same day of the week.

Solution: Let p be the proposition "At least four of 22 chosen days fall on the same day of the week." Suppose that -'p is true. This means that at most three of the 22 days fall on the same day of the week. Because there are seven days of the week, this implies that at most 2 1 days could have been chosen because for each of the days of the week, at most three of the chosen days could fall on that day. This contradicts the hypothesis that we have 22 days under consideration. That is, if r is the statement that 22 days are chosen, then we have shown that -'p -+ (r 1\ ....,r ) . Consequently, we know that p is true. We have proved that at least four of 22 chosen days fall ... on the same day of the week. EXAMPLE 10

Prove that V2 is irrational by giving a proof by contradiction.

Solution: Let p be the proposition " V2 is irrational." To start a proofby contradiction, we suppose that -'p is true. Note that ""'p is the statement "It is not the case that V2 is irrational," which says that V2 is rational. We will show that assuming that ""'p is true leads to a contradiction. If V2 is rational, there exist integers a and b with V2 alb, where a and b have no common factors (so that the fraction alb is in lowest terms.) (Here, we are using the fact that every rational number can be written in lowest terms.) Because V2 alb, when both sides of this equation are squared, it follows that

=

=

1-81

1 .6 Introduction to Proofs

81

Hence,

By the definition of an even integer it follows that a 2 is even. We next use the fact that if a 2 is even, a must also be even, which follows by Exercise 1 6. Furthermore, because a is even, by the definition of an even integer, a 2c for some integer c . Thus,

=

Dividing both sides of this equation by 2 gives

By the definition of even, this means that b 2 is even. Again using the fact that if the square of an integer is even, then the integer itself must be even, we conclude that b must be even as well. We have now shown that the assumption of ...... p leads to the equation ,J2 alb, where a and b have no common factors, but both a and b are even, that is, 2 divides both a and b. Note that the statement that ,J2 alb, where a and b have no common factors, means, in particular, that 2 does not divide both a and b. Because our assumption of """ p leads to the contradiction that 2 divides both a and b and 2 does not divide both a and b, """ p must be false. .... That is, the statement p, ",J2 is irrational," is true. We have proved that ,J2 is irrational.

=

=

Proofby contradiction can be used to prove conditional statements. In such proofs, we first assume the negation of the conclusion. We then use the premises of the theorem and the negation of the conclusion to arrive at a contradiction. (The reason that such proofs are valid rests on the logical equivalence of p -+ q and (p 1\ ......q ) -+ F. To see that these statements are equivalent, simply note that each is false in exactly one case, namely when p is true and q is false.) Note that we can rewrite a proof by contraposition of a conditional statement as a proof by contradiction. In a proof of p -+ q by contraposition, we assume that ...... q is true. We then show that """p must also be true. To rewrite a proof by contraposition of p -+ q as a proof by contradiction, we suppose that both p and ...... q are true. Then, we use the steps from the proof of ...... q -+ """ p to show that """ p is true. This leads to the contradiction p 1\ """ p , completing the proof. Example 1 1 illustrates how a proof by contraposition of a conditional statement can be rewritten as a proof by contradiction.

EXAMPLE 11

Give a proof by contradiction of the theorem "If 3n + 2 is odd, then n is odd."

Solution: Let p be "3n + 2 is odd" and q be "n is odd." To construct a proof by contradiction, assume that both p and ...... q are true. That is, assume that 3n + 2 is odd and that n is not odd. Because n is not odd, we know that it is even. Following the steps in the solution of Example 3 (a proofby contraposition), we can show that if n is even, then 3n + 2 is even. First, because n is even, there is an integer k such that n 2k. This implies that 3n + 2 3(2k) + 2 6k + 2 2(3k + 1). Because 3n + 2 is 2t, where t 3k + 1 , 3n + 2 is even. Note that the statement "3n + 2 is even" """p , because an integer is even if and only if it is not odd. Because both p and """ p are true, we have a contradiction. This completes the proof by contradiction, proving that if .... 3n + 2 is odd, then n is odd.

=

=

=

=

=

Note that we can also prove by contradiction that p -+ q is true by assuming that p and are true, and showing that q must be also be true. This implies that ......q and q are both true, a contradiction. This observation tells us that we can tum a direct proof into a proof by contradiction. ...... q

82

1 / The Foundations: Logic and Proofs

1 -82

PROOFS OF EQUIVALENCE To prove a theorem that is a biconditional statement, that is, a statement of the form P *+ q, we show that P -+ q and q -+ P are both true. The validity of this approach is based on the tautology (p *+ q ) *+ [(p -+ q ) 1\ (q -+ p)] .

EXAMPLE 12

Exam= �

Prove the theorem "If n is a positive integer, then n is odd if and only if n 2 is odd."

Solution: This theorem has the form "P if and only if q ," where P is "n is odd" and q is "n 2 is odd." (As usual, we do not explicitly deal with the universal quantification.) To prove this theorem, we need to show that P -+ q and q -+ P are true. We have already shown (in Example 1 ) that P -+ q is true and (in Example 8) that q -+ P is true. Because we have shown that both P -+ q and q -+ P are true, we have shown that the .... theorem is true. Sometimes a theorem states that several propositions are equivalent. Such a theorem states that propositions P I , P2 , P3 , . . . , Pn are equivalent. This can be written as

PI *+ P2 *+

.

.

.

*+ Pn ,

which states that all n propositions have the same truth values, and consequently, that for all i and j with 1 :s i :s n and 1 :s j :s n, Pi and Pj are equivalent. One way to prove these mutually equivalent is to use the tautology

This shows that if the conditional statements PI -+ P2 , P2 -+ P3 , . . . , Pn -+ P I can be shown to be true, then the propositions P I , P2 , . . . , Pn are all equivalent. This is much more efficient than proving that Pi -+ Pj for all i =f. j with 1 :s i :s n and 1 :s j :s n . When we prove that a group o f statements are equivalent, we can establish any chain of conditional statements we choose as long as it is possible to work through the chain to go from any one of the-se statements to any other statement. For example, we can show that P I , P2 , and P3 are equivalent by showing that P I -+ P3 , P3 -+ P2 , and P2 -+ P I .

EXAMPLE 13

Show that these statements about the integer n are equivalent:

PI : P2 : P3 :

n is even. n - 1 is odd. n 2 is even.

Solution: We will show that these three statements are equivalent by showing that the conditional statements PI -+ P2 , P2 -+ P3 , and P3 -+ P I are true. We use a direct proof to show that P I -+ P2 . Suppose that n is even. Then n 2k for some integer k. Consequently, n - 1 2k - 1 2(k - 1 ) + 1 . This means that n - 1 is odd because it is of the form 2m + 1 , where m is the integer k - 1 . We also use a direct proof to show that P2 -+ P3 . Now suppose n - 1 is odd. Then n 1 2k + 1 for some integer k. Hence, n 2k + 2 so that n 2 (2k + 2i 4k2 + 8k + 4 2(2k2 + 4k + 2). This means that n 2 is twice the integer 2k2 + 4k + 2, and hence is even. To prove P3 -+ PI , we use a proofby contraposition. That is, we prove that if n is not even, then n 2 is not even. This is the same as proving that if n is odd, then n 2 is odd, which we have .... already done in Example 1 . This completes the proof.

=

=

=

=

=

=

=

=

1 .6 Introduction to Proofs

1 -83

83

COUNTEREXAMPLES In Section 1 .3 we stated that to show that a statement of the form Vx P(x) is false, we need only find a counterexample, that is, an example x for which P (x) is false. When presented with a statement of the form Vx P (x), which we believe to be false or which has resisted all proof attempts, we look for a counterexample. We illustrate the use of counterexamples in Example 14.

EXAMPLE 14

Show that the statement "Every positive integer is the sum of the squares of two integers" is false.

Ex1ra � Examples IIiiiiiI

Solution: To show that this statement is false, we look for a counterexample, which is a par­ ticular integer that is not the sum of the squares of two integers. It does not take long to find a counterexample, because 3 cannot be written as the sum of the squares of two integers. To show this is the case, note that the only perfect squares not exceeding 3 are 0 2 = 0 and 1 2 = 1 . Furthermore, there is no way to get 3 as the sum of two terms each of which is 0 or I . Conse­ quently, we have shown that "Every positive integer is the sum of the squares of two integers" is .... false.

Mistakes in Proofs

unlls E:J EXAMPLE 15

There are many common errors made in constructing mathematical proofs. We will briefly describe some of these here. Among the most common errors are mistakes in arithmetic and basic algebra. Even professional mathematicians make such errors, especially when working with complicated formulae. Whenever you use such computations you should check them as carefully as possible. (You should also review any troublesome aspects of basic algebra, especially before you study Section 4. 1 .) Each step of a mathematical proof needs to be correct and the conclusion needs to follow logically from the steps that precede it. Many mistakes result from the introduction of steps that do not logically follow from those that precede it. This is illustrated in Examples 1 5-17. What i s wrong with this famous supposed "proof" that 1 = 2?

"Proof:" We use these steps, where a and b are two equal positive integers. Step

Reason

l. 2. 3. 4. 5. 6.

Given Multiply both sides of ( 1 ) by a Subtract b2 from both sides of (2) Factor both sides of (3) Divide both sides of (4) by a - b Replace a by b in (5) because a = b and simplify Divide both sides of (6) by b

a=b a 2 = ab a 2 - b 2 = ab - b2 (a - b)(a + b) = b(a - b) a+b=b 2b = b

7. 2 = 1

Solution: Every step is valid except for one, step 5 where we divided both sides by a - b. The error is that a - b equals zero; division of both sides of an equation by the same quantity is .... valid as long as this quantity is not zero. EXAMPLE 16

What is wrong with this "proof" ? "Theorem:" If n 2 is positive, then n is positive.

84

1 / The Foundations: Logic and Proofs

1 -84

"Proof:" Suppose that n 2 is positive. Because the conditional statement "If n is positive, then n 2 is positive" is true, we can conclude that n is positive.

Solution: Let P (n) be "n is positive" and Q(n) be "n 2 is positive." Then our hypothesis is Q(n). The statement "If n is positive, then n 2 is positive" is the statement "In(P(n) --+ Q(n)). From the hypothesis Q(n) and the statement "In(P(n) --+ Q(n)) we cannot conclude p en), because we are not using a valid rule of inference. Instead, this is an example of the fallacy of affirming the conclusion. A counterexample is supplied by n - 1 for which n 2 1 is positive, but n is � negative.

=

EXAMPLE 17

=

What is wrong with this "proof" ? "Theorem:" If n is not positive, then n 2 is not positive. (This is the contrapositive of the "theorem" in Example 1 6.)

"Proof:" Suppose that n is not positive. Because the conditional statement "If n is positive, then n 2 is positive" is true, we can conclude that n 2 is not positive.

Solution: Let P (n) and Q(n) be as in the solution of Example 1 6. Then our hypothesis is -,P(n) and the statement "Ifn is positive, then n 2 is positive" is the statement "In(P(n) --+ Q(n)). From the hypothesis -, P (n) and the statement "In(P(n) --+ Q(n)) we cannot conclude -'Q(n), because we are not using a valid rule of inference. Instead, this is an example of the fallacy of denying � the hypothesis. A counterexample is supplied by n - 1 , as in Example 1 6.

=

Finally, we briefly discuss a particularly nasty type of error. Many incorrect arguments are based on a fallacy called begging the question. This fallacy occurs when one or more steps of a proof are based on the truth of the statement being proved. In other words, this fallacy arises when a statement is proved using itself, or a statement equivalent to it. That is why this fallacy is also called circular reasoning.

EXAMPLE 18

Is the following argument correct? It supposedly shows that n is an even integer whenever n 2 is an even integer. Suppose that n 2 is even. Then n 2 This shows that n is even.

= 2k for some integer k. Let n = 21 for some integer I. =

Solution: This argument is incorrect. The statement "let n 21 for some integer I" occurs in the proof. No argument has been given to show that n can be written as 21 for some integer I. This is circular reasoning because this statement is equivalent to the statement being proved, namely, "n is even." Of course, the result itself is correct; only the method of proof is wrong. � Making mistakes in proofs is part of the learning process. When you make a mistake that someone else finds, you should carefully analyze where you went wrong and make sure that you do not make the same mistake again. Even professional mathematicians make mistakes in proofs. More than a few incorrect proofs of important results have fooled people for many years before subtle errors in them were found.

Just

a

Beginning

We have now developed a basic arsenal of proof methods. In the next section we will introduce other important proof methods. We will also introduce several important proof techniques in

/-85

1 .6 Introduction to Proofs

85

Chapter 4, including mathematical induction, which can be used to prove results that hold for all positive integers. In Chapter 5 we will introduce the notion of combinatorial proofs. In this section we introduced several methods for proving theorems of the form Vx(P(x) --+ Q(x )), including direct proofs and proofs by contraposition. There are many theorems of this type whose proofs are easy to construct by directly working through the hypotheses and definitions of the terms of the theorem. However, it is often difficult to prove a theorem without resorting to a clever use of a proof by contraposition or a proof by contradiction, or some other proof technique. In Section 1 .7 we will address proof strategy. We will describe various approaches that can be used to find proofs when straightforward approaches do not work. Constructing proofs is an art that can be learned only through experience, including writing proofs, having your proofs critiqued, and reading and analyzing other proofs.

Exercises 1. Use a direct proofto show that the sum of two odd integers is even. 2. Use a direct proof to show that the sum oftwo even inte­ gers is even. 3. Show that the square of an even number is an even number using a direct proof. 4. Show that the additive inverse, or negative, of an even number is an even number using a direct proof. 5. Prove that if m + n and n + p are even integers, where m , n , and p are integers, then m + p is even. What kind of proof did you use? 6. Use a direct proof to show that the product of two odd numbers is odd. 7. Use a direct proof to show that every odd integer is the difference of two squares. 8. Prove that if n is a perfect square, then n + 2 is not a perfect square. 9. Use a proof by contradiction to prove that the sum of an irrational number and a rational number is irrational. 10. Use a direct proof to show that the product of two rational numbers is rational. 1 1 . Prove or disprove that the product of two irrational num­ bers is irrational. 12. Prove or disprove that the product of a nonzero rational number and an irrational number is irrational. 13. Prove that if x is irrational, then 1 /x is irrational. 14. Prove that if x is rational and x =1= 0, then I /x is rational. 15. Use a proof by contraposition to show that if x + y :::: 2, where x and y are real numbers, then x :::: I or y :::: I . S' 16. Prove that if m and n are integers and m n is even, then m is even or n is even. 17. Show that if n is an integer and n 3 + 5 is odd, then n is even using

a) a proof by contraposition. b) a proof by contradiction. 18. Prove that if n is an integer and even using

3n + 2 is even, then n

is

a) a proof by contraposition. b) a proof by contradiction. 19. Prove the proposition P (O), where P (n ) is the proposition "If n is a positive integer greater than I , then n 2 > n ." What kind of proof did you use? 20. Prove the proposition P ( I ), where pen) is the proposi­ tion "If n is a positive integer, then n 2 :::: n ." What kind of proof did you use? 2 1 . Let p en) be the proposition "If a and b are positive real numbers, then (a + b)n :::: a n + bn ." Prove that P ( I ) is true. What kind of proof did you use? 22. Show that if you pick three socks from a drawer contain­ ing j ust blue socks and black socks, you must get either a pair of blue socks or a pair of black socks. 23. Show that at least 1 0 of any 64 days chosen must fall on the same day of the week. 24. Show that at least 3 of any 25 days chosen must fall in the same month of the year. 25. Use a proofby contradiction to show that there is no ratio­ nal number r for which r 3 + r + = O . [Hint: Assume that r = a / b is a root, where a and b are integers and a / b is in lowest terms. Obtain an equation involving integers by multiplying by b 3 • Then look at whether a and b are each odd or even.] 26. Prove that if n is a positive integer, then n is even if and only if n + 4 is even.

I

7

27. Prove that if n is a positive integer, then n is odd if and only if 5 n + 6 is odd. 28. Prove that m 2 = n 2 if and only if m = n or m = -n . 29. Prove or disprove that if m and n are integers such that mn = 1 , then either m = I and n = I , or else m = - I and n = - 1 . 30. Show that these three statements are equivalent, where a and b are real numbers: (i) a is less than b, (ii) the average of a and b is greater than a , and (iii) the average of a and b is less than b. 3 1 . Show that these statements about the integer x are equiv­ alent: (i) 3x + 2 is even, (ii) x + 5 is odd, (iii) x 2 is even.

1 / The Foundations: Logic and Proofs

86

1 -86

32. Show that these statements about the real number x are equivalent: (i) x is rational, (ii) x /2 is rational, and (iii) 3x - 1 is rational.

37.

33. Show that these statements about the real number x are equivalent: (i) x is irrational, (ii) 3x + 2 is irrational, (iii) x /2 is irrational. 34. Is this reasoning for finding the solutions of the equation .J2x 2 - 1 = x correct? (1) .J2x 2 - 1 = x is given; (2) 2X 2 - 1 = x 2 , obtained by squaring both sides of ( 1 ); (3) x 2 - 1 = 0, obtained by subtracting x 2 from both sides of (2); (4) (x - 1 )(x + 1 ) = 0, obtained by factoring the left­ hand side of x 2 - 1 ; (5) x = 1 or x = - 1 , which follows because ab = 0 implies that a = 0 or b = O . 35. Are these steps for finding the solutions of .Jx + = - x correct? (1) .Jx + = - x is given; (2) x + 3 = x 2 - 6x + obtained by squaring both sides of ( I ); (3) 0 = x 2 - 7x + 6, obtained by subtracting x + from both sides of (2); (4) 0 = (x - 1 )(x - 6), obtained by factoring the right-hand side of (5) x = 1 or x = 6, which follows from (4) because ab = 0 implies that a = 0 or b = O . 36. Show that the propositions PI , P2 , P 3 , and P4 can be shown

3 3

9,

3 3

3

(3);

38.

39.

40.

41.

42.

to be equivalent by showing that PI ++ P4, P2 ++ P3 , and P I ++ P3 · Show that the propositions PI , P2 , P3 , P4, and P5 can be shown to be equivalent by proving that the conditional statements P I -+ P4 , P3 -+ PI . P4 -+ P2 , P2 -+ P5 , and P5 -+ P3 are true. Find a counterexample to the statement that every positive integer can be written as the sum of the squares of three integers. Prove that at least one of the real numbers a I . a 2 , . . . , an is greater than or equal to the average of these numbers. What kind of proof did you use? Use Exercise to show that if the first 10 positive inte­ gers are placed around a circle, in any order, there exist three integers in consecutive locations around the circle that have a sum greater than or equal to 1 7 . Prove that if is an integer, these four statements are equivalent: (i) is even, (ii) + 1 is odd, (iii) + 1 is odd, (iv) is even. Prove that these four statements about the integer are equivalent: (i) n 2 is odd, (ii) 1 - is even, (iii) n 3 is odd, (iv) n 2 + 1 is even.

39

nn 3n

n

3n

n

n

1.7 Proof Methods and Strategy Introduction

Assessment �

In Section 1 .6 we introduced a variety of different methods of proof and illustrated how each method is used. In this section we continue this effort. We will introduce several other important proof methods, including proofs where we consider different cases separately and proofs where we prove the existence of objects with desired properties. In Section 1 .6 we only briefly discussed the strategy behind constructing proofs. This strategy includes selecting a proof method and then successfully constructing an argument step by step, based on this method. In this section, after we have developed a wider arsenal of proof methods, we will study some additional aspects of the art and science of proofs. We will provide advice on how to find a proof of a theorem. We will describe some tricks of the trade, including how proofs can be found by working backward and by adapting existing proofs. When mathematicians work, they formulate conjectures and attempt to prove or disprove them. We will briefly describe this process here by proving results about tiling checkerboards with dominoes and other types of pieces. Looking at tilings of this kind, we will be able to quickly formulate conjectures and prove theorems without first developing a theory. We will conclude the section by discussing the role of open questions. In particular, we will discuss some interesting problems either that have been solved after remaining open for hundreds of years or that still remain open.

Exhaustive Proof and Proof by Cases Sometimes we cannot prove a theorem using a single argument that holds for all possible cases. We now introduce a method that can be used to prove a theorem, by considering different cases

1 .7 Proof Methods and Strategy 87

/-87

separately. This method is based on a rule of inference that we will now introduce. To prove a conditional statement of the form

the tautology

can be used as a rule of inference. This shows that the original conditional statement with a hypothesis made up ofa disjunction of the propositions PI , P2 , . . . , Pn can be proved by proving each of the n conditional statements Pi -+ q , i 1 , 2, . . . , n , individually. Such an argument is called a proof by cases. Sometimes to prove that a conditional statement P -+ q is true, it is convenient to use a disjunction PI v P2 V . . . V P instead of P as the hypothesis of the n conditional statement, where P and PI v P2 V . . . V P are equivalent. n

=

EXHAUSTIVE PROOF Some theorems can be proved by examining a relatively small num­ ber of examples. Such proofs are called exhaustive proofs, because these proofs proceed by exhausting all possibilities. An exhaustive proof is a special type of proof by cases where each case involves checking a -single example. We now provide some illustrations of exhaustive proofs.

EXAMPLE 1

Prove that (n + 1 )2 � 3n if n is a positive integer with n :s 4.

Solution: We use a proof by exhaustion. We only need verify the inequality (n + 1 ) 2 ::: 3n when n 1 , 2, 3 , and 4. For n 1 , we have (n + 1 )2 2 2 4 and 3n 3 1 3 ; for n 2, we have (n + Ii 3 2 9 and 3n 3 2 9; for n 3, we have (n + 1 )3 43 64 and 3n 3 3 27; and for n 4, we have (n + 1 ) 3 5 3 1 25 and 3n 3 4 8 1 . In each of these four cases, we see that (n + 1 ) 2 � 3n . We have used the method of exhaustion to prove that (n + 1 ) 2 � 3n if n .... is a positive integer with n :s 4.

=

Ex1ra � Examples IIiiiiiiI EXAMPLE 2

= = = = = = = = = =

= = =

= = = =

= = =

Prove that the only consecutive positive integers not exceeding 1 00 that are perfect powers are 8 and 9. (An integer is a perfect power if it equals na, where a is an integer greater than 1 .)

Solution: We can prove this fact by showing that the only pair n, n + 1 of consecutive positive integers that are both perfect powers with n < 1 00 arises when n 8. We can prove this fact by examining positive integers n not exceeding 1 00, first checking whether n is a perfect power, and if it is, checking whether n + 1 is also a perfect power. A quicker way to do this is simply to look at all perfect powers not exceeding 1 00 and checking whether the next largest integer is also a perfect power. The squares of positive integers not exceeding 1 00 are 1 , 4, 9, 1 6, 25, 36, 49, 64, 8 1 , and 1 00. The cubes of positive integers not exceeding 1 00 are 1 , 8, 27, and 64. The fourth powers of positive integers not exceeding 1 00 are 1 , 1 6, and 8 1 . The fifth powers of positive integers not exceeding 1 00 are 1 and 32. The sixth powers of positive integers not exceeding 1 00 are 1 and 64. There are no powers of positive integers higher than the sixth power not exceeding 1 00, other than 1 . Looking at this list of perfect powers not exceeding 1 00, we see that n = 8 is the only perfect power n for which n + 1 is also a perfect power. That .... is, 23 = 8 and 3 2 9 are the only two consecutive perfect powers not exceeding 1 00.

=

=

People can carry out exhaustive proofs when it is necessary to check only a relatively small number of instances of a statement. Computers do not complain when they are asked to check a much larger number of instances of a statement, but they still have limitations. Note that not even a computer can check all instances when it is impossible to list all instances to check.

88

1 -88

1 / The Foundations: Logic and Proofs

PROOF BY CASES A proof by cases must cover all possible cases that arise in a theorem. We illustrate proofby cases with a couple of examples. In each example, you should check that all possible cases are covered.

EXAMPLE 3

Prove that if n is an integer, then n 2 :::: n .

=

Solution: We can prove that n 2 :::: n for every integer by considering three cases, when n 0, when n :::: 1 , and when n :::: - 1 . We split the proof into three cases because it is straightforward to prove the result by considering zero, positive integers, and negative integers separately.

=

=

Case (i). When n 0, because 02 0, we see that 02 :::: 0. It follows that n 2 :::: n is true in this case. Case (ii). When n :::: 1 , when we multiply both sides of the inequality n :::: 1 by the positive integer n , we obtain n . n :::: n . 1 . This implies that n 2 :::: n for n :::: 1 . Case (iii). In this case n :::: - 1 . However, n 2 :::: 0 . It follows that n 2 :::: n . Because the inequality n 2 :::: n holds in all three cases, we can conclude that i f n i s an integer, 0. Hence, IxY I xy (-x)(-y) Ix l ly l .

=

=

=

=

=

=

=

Because we have completed all four cases and these cases exhaust all possibilities, we can 8 when Ix I ::: 3 and 3y2 > 8 when Iy I ::: 2. This leaves the cases when x takes on one of the values -2, - 1 , 0, 1 , or 2 and y takes on one of the values - 1 , 0, or 1 . We can finish using an exhaustive proof. To dispense with the remaining cases, we note that possible values for x 2 are 0, 1 , and 4, and possible values for 3y 2 are 0 and 3, and the largest sum of possible values for x 2 and 3y 2 is ... 7. Consequently, it is impossible for x 2 + 3y 2 = 8 to hold when x and y are integers. WITHOUT LOSS OF GENERALITY In the proof in Example 4, we dismissed case (iii), where x < 0 and y ::: 0, because it is the same as case (ii), where x ::: 0 and y < 0, with the roles of x and y reversed. To shorten the proof, we could have proved cases (ii) and (iii) together by assuming, without loss of generality, that x ::: 0 and y < O. Implicit in this statement is that we can complete the case with x < 0 and y ::: 0 using the same argument as we used for the case with x ::: 0 and y < 0, but with the obvious changes. In general, when the phrase "without loss of generality" is used in a proof(often abbreviated as WLOG), we assert that by proving one case of a theorem, no additional argument is required to prove other specified cases. That is, other cases follow by making straightforward changes to the argument, or by filling in some straightforward initial step. Of course, incorrect use of this principle can lead to unfortunate errors. Sometimes assumptions are made that lead to a loss in generality. Such assumptions can be made that do not take into account that one case may be substantially different from others. This can lead to an incomplete, and possibly unsalvageable, proof. In fact, many incorrect proofs of famous theorems turned out to rely on arguments that used the idea of "without loss of generality" to establish cases that could not be quickly proved from simpler cases. We now illustrate a proof where without loss of generality is used effectively.

90

1 -90

1 / The Foundations: Logic and Proofs

EXAMPLE 7

Show that (x + y Y < xr + yr whenever x and y are positive real numbers and r is a real number with 0 < r < 1 .

Solution: Without loss of generality we can assume that x + y = 1 . [To see this, suppose we have proved the theorem with the assumption that x + y = 1 . Suppose that x + y = t. Then (x / t ) + (y/ t ) = 1 , which implies that « x / t ) + (y/ t )Y < (x / tY + (y/ tY . Multiplying both sides of this last equation by tr shows that (x + y y < xr + yr .] Assuming that x + y = 1, because x and y are positive, we have 0 < x < 1 and 0 < y < 1 . I Because 0 < r < 1 , it follows that 0 < 1 r < 1 , so X -r < 1 and y l -r < 1 . This means that x < xr andy < yr . Consequently, xr + yr > X + y = 1 . This means that (x + yy = l r < xr + yr . This proves the theorem for x + y = 1 . Because we could assume x + y = 1 without loss of generality, we know that (x + yy < xr + yr whenever x and y are positive real numbers and r is a real number with 0 < r < 1 . .... -

COMMON ERRORS WITH EXHAUSTIVE PROOF AND PROOF BY CASES A common error of reasoning is to draw incorrect conclusions from examples. No matter how many separate examples are considered, a theorem is not proved by considering examples unless every possible case is covered. The problem of proving a theorem is analogous to showing that a computer program always produces the output desired. No matter how many input values are tested, unless all input values are tested, we cannot conclude that the program always produces the correct output.

EXAMPLE 8

Is it true that every positive integer is the sum of 1 8 fourth powers of integers?

Solution: To determine whether n can be written as the sum of 1 8 fourth powers of integers, we might begin by examining whether n is the sum of 1 8 fourth powers of integers for the smallest positive integers. Because the fourth powers of integers are 0, 1 , 1 6, 8 1 , . . . , if we can select 1 8 terms from these numbers that add up to n , then n is the sum of 1 8 fourth powers. We can show that all positive integers up to 78 can be written as the sum of 1 8 fourth powers. (The details are left to the reader.) However, if we decided this was enough checking, we would come to the wrong conclusion. It is not true that every positive integer is the sum of 1 8 fourth powers .... because 79 is not the sum of 1 8 fourth powers (as the reader can verify). Another common error involves making unwarranted assumptions that lead to incorrect proofs by cases where not all cases are considered. This is illustrated in Example 9.

EXAMPLE 9

What is wrong with this "proof" ? "Theorem:" If x is a real number, then x 2 is a positive real number.

"Proof:" Let P I be "x is positive," let P 2 be "x is negative," and let q be "x 2 is positive." To show that P I -+ q is true, note that when x is positive, x 2 is positive because it is the product of two positive numbers, x and x . To show that P2 -+ q , note that when x is negative, x 2 is positive because it is the product of two negative numbers, x and x . This completes the proof.

Solution: The problem with the proof we have given is that we missed the case x = O. When x = 0, x 2 = 0 is not positive, so the supposed theorem is false. If P is "x is a real number," then we can prove results where P is the hypothesis with three cases, P I , P2 , and P3 , where P I is "x is positive," P2 is "x is negative," and P 3 is "x = 0" because of the equivalence .... P B PI V P2 V P3 ·

1 .7 Proof Methods and Strategy 91

1-91

Existence Proofs Many theorems are assertions that objects of a particular type exist. A theorem of this type is a proposition of the form 3x P (x), where P is a predicate. A proof of a proposition of the form 3x P(x) is called an existence proof. There are several ways to prove a theorem of this type. Sometimes an existence proof of 3x P (x) can be given by finding an element a such that P (a) is true. Such an existence proof is called constructive. It is also possible to give an existence proof that is nonconstructive; that is, we do not find an element a such that P ea) is true, but rather prove that 3x P (x) is true in some other way. One common method of giving a nonconstructive existence proof is to use proof by contradiction and show that the negation of the existential quantification implies a contradiction. The concept of a constructive existence proof is illustrated by Example 1 0 and the concept of a nonconstructive existence proof is illustrated by Example 1 1 .

EXAMPLE 10

Extra ;'" Examples �

A Constructive Existence Proof Show that there is a positive integer that can be written as the sum of cubes of positive integers in two different ways.

Solution: After considerable computation (such as a computer search) we find that

Because we have displayed a positive integer that can be written as the sum of cubes in two � different ways, we are done.

EXAMPLE 11

A Nonconstructive Existence Proof

that xY is rational.

Show that there exist irrational numbers x and y such

Solution: By Example 1 0 in Section 1 .6 we know that .j2 is irrational. Consider the number v"i .j2 . If it is rational, we have two irrational numbers x and y with xY rational, namely, x = .j2 v"i v"i and y = .j2. On the other hand if .j2 is irrational, then we can let x = .j2 and y = .j2 r,;v"i h � v"i.v"i) r,; = v 2 2 = 2. so that xY = (v 2 )'-' 2 = '\1' 2 This proof is an example of a nonconstructive existence proof because we have not found irrational numbers x and y such that xY is rational. Rather, we have shown that either the pair v"i x = .j2, y = .j2 or the pair x = .j2 , Y = .j2 have the desired property, but we do not know � which of these two pairs works ! Nonconstructive existence proofs often are quite subtle, as Example 1 2 illustrates.

EXAMPLE 12

unkS�

Chomp is a game played by two players. In this game, cookies are laid out on a rectangular grid. The cookie in the top left position is poisoned, as shown in Figure l ea). The two players take turns making moves; at each move, a player is required to eat a remaining cookie, together with all cookies to the right and/or below it (see Figure I (b), for example). The loser is the player who has no choice but to eat the poisoned cookie. We ask whether one of the two players has a winning strategy. That is, can one of the players always make moves that are guaranteed to lead to a win?

Solution: We will give a nonconstructive existence proof of a winning strategy for the first player. That is, we will show that the first player always has a winning strategy without explicitly describing the moves this player must follow. First, note that the game ends and cannot finish in a draw because with each move at least one cookie is eaten, so after no more than m x n moves the game ends, where the initial grid is

92

1 - 92

1 / The Foundations: Logic and Proofs ...............

® -' @ " , . ' -. -

...

.

-" .

' ''',- - ! ' .. �

' \

(b)

FIGURE 1

(a) Chomp, the Top Left Cookie is Poison

(b) Three Possible Moves.

m x n _ Now, suppose that the first player begins the game by eating just the cookie in the bottom right comer. There are two possibilities, this is the first move of a winning strategy for the first player, or the second player can make a move that is the first move of a winning strategy for the second player. In this second case, instead of eating just the cookie in the bottom right comer, the first player could have made the same move that the second player made as the first move of a winning strategy (and then continued to follow that winning strategy). This would guarantee a win for the first player. Note that we showed that a winning strategy exists, but we did not specify an actual winning strategy. Consequently, the proof is a nonconstructive existence proof. In fact, no one has been able to describe a winning strategy for that Chomp that applies for all rectangular grids by describing the moves that the first player should follow. However, winning strategies can be described for certain special cases, such as when the grid is square and when the grid only has � = .J24 .] Can we prove that this inequality is always true?

Solution: To prove that (x + y) /2 > .jXY when x and y are distinct positive real numbers, we can work backward. We construct a sequence of equivalent inequalities. The equivalent inequalities are (x + y)/2 (x + y)2 /4

>

.jXY,

>

xy ,

>

4xy,

(x + y)2 x 2 + 2xy + y 2 x 2 - 2xy + y2

>

4xy,

>

0,

(x - y)2

>

o.

Because (x - y)2 > 0 when x =1= y, it follows that the final inequality is true. Because all these inequalities are equivalent, it follows that (x + y) /2 > .jXY when x =1= y. Once we have carried out this backward reasoning, we can easily reverse the steps to construct a proof using forward reasoning. We now give this proof. Suppose that x and y are distinct real numbers. Then (x - y)2 > 0 because the square of a nonzero real number is positive (see Appendix 1 ). Because (x - y)2 = x 2 - 2xy + y 2 , this implies that x 2 - 2xy + y2 > O. Adding 4xy to both sides, we obtain x 2 + 2xy + y 2 > 4xy. Because x 2 + 2xy + y 2 = (x + y)2 , this means that (x + y) 2 2: 4xy. Dividing both sides of this equation by 4, we see that (x + y)2 /4 > xy. Finally, taking square roots of both sides (which preserves the inequality because both sides are positive) yields (x + y)/2 > .jXY. We conclude that if x and y are distinct positive real numbers, then their arithmetic mean (x + y)/2 is greater � than their geometric mean .jXY.

EXAMPLE 15

Suppose that two people play a game taking turns removing one, two, or three stones at a time from a pile that begins with 1 5 stones. The person who removes the last stone wins the game. Show that the first player can win the game no matter what the second player does.

Solution: To prove that the first player can always win the game, we work backward. At the last step, the first player can win if this player is left with a pile containing one, two, or three stones. The second player will be forced to leave one, two, or three stones if this player has to remove stones from a pile containing four stones. Consequently, one way for the first person to win is to leave four stones for the second player on the next-to-last move. The first person can leave four stones when there are five, six, or seven stones left at the beginning of this player's move, which happens when the second player has to remove stones from a pile with eight stones. Consequently, to force the second player to leave five, six , or seven stones, the first player should leave eight stones for the second player at the second-to-last move for the first player. This means that there are nine, ten, or eleven stones when the first player makes this move. Similarly, the first player should leave twelve stones when this player makes the first move. We can reverse this argument to show that the first player can always make moves so that this player wins the game no matter what the second player does. These moves successively leave twelve, eight, and � four stones for the second player.

96

1 / The Foundations: Logic and Proofs

1 -96

ADAPTING EXISTING PROOFS An excellent way to look for possible approaches that can be used to prove a statement is to take advantage of existing proofs. Often an existing proof can be adapted to prove a new result. Even when this is not the case, some of the ideas used in existing proofs may be helpful. Because existing proofs provide clues for new proofs, you should read and understand the proofs you encounter in your studies. This process is illustrated in Example 1 6.

EXAMPLE 16

In Example 1 0 of Section 1 .6 we proved that ..ti is irrational. We now conjecture that J3 is irrational. Can we adapt the proof in Example l O in Section 1 .6 to show that J3 is irrational?

Ex1ra � Examples �

Solution: To adapt the proof in Example l O in Section 1 .6, we begin by mimicking the steps in that proof, but with ..ti replaced with J3. First, we suppose that J3 d / c where the fraction c / d is in lowest terms. Squaring both sides tells us that 3 c2 / d2 , so that 3d2 c2 . Can we use this equation to show that 3 must be a factor of both c and d, similar to how we used the equation 2b 2 a 2 in Example 1 0 in Section 1 .6 to show that 2 must be a factor of both a and b? (Recall that an integer s is a factor of the integer t if t /s is an integer. An integer n is even if and only if 2 is a factor of n .) In turns out that we can, but we need some ammunition from number theory, which we will develop in Chapter 3 . We sketch out the remainder of the proof, but leave the justification of these steps until Chapter 3 . Because 3 is a factor of c2 , it must also be a factor of c. Furthermore, because 3 is a factor of c, 9 is a factor of c2 , which means that 9 is a factor of 3d2 • This implies that 3 is a factor of d2 , which means that 3 is a factor of that d. This makes 3 a factor of both c and d, which is a contradiction. After we have filled in the justification for these steps, we will have shown that J3 is irrational by adapting the proof that ..ti is irrational. Note that this proof can be extended to show that In is irrational whenever n � is a positive integer that is not a perfect square. We leave the details of this to Chapter 3 .

=

=

=

=

A good tip is to look for existing proofs that you might adapt when you are confronted with proving a new theorem, particularly when the new theorem seems similar to one you have already proved.

Looking for Counterexamples In Section 1 .5 we introduced the use of counterexamples to show that certain statements are false. When confronted with a conjecture, you might first try to prove this conjecture, and if your attempts are unsuccessful, you might try to find a counterexample. If you cannot find a counterexample, you might again try to prove the statement. In any case, looking for counterex­ amples is an extremely important pursuit, which often provides insights into problems. We will illustrate the role of counterexamples with a few examples.

EXAMPLE 17

In Example 14 in Section 1 .6 we showed that the statement "Every positive integer is the sum of two squares of integers" is false by finding a counterexample. That is, there are positive integers that cannot be written as the sum of the squares of two integers. Although we cannot write every positive integer as the sum of the squares of two integers, maybe we can write every positive integer as the sum of the squares of three integers. That is, is the statement "Every positive integer is the sum of the squares of three integers" true or false?

Solution: Because we know that not every positive integer can be written as the sum of two squares of integers, we might initially be skeptical that every positive integer can be written as the sum of three squares of integers. So, we first look for a counterexample. That is, we can show that the statement "Every positive integer is the sum of three squares of integers"

1 .7 Proof Methods and Strategy

97

is false if we can find a particular integer that is not the sum of the squares of three integers. To look for a counterexample, we try to write successive positive integers as a sum of three squares. We find that 1 = 02 + 02 + 1 2 , 2 = 02 + 1 2 + 1 2 , 3 = 1 2 + 1 2 + 1 2 , 4 = 02 + 02 + 22 , 5 = 02 + 1 2 + 2 2 , 6 = 1 2 + 1 2 + 22 , but we cannot find a way to write 7 as the sum of three squares. To show that there are not three squares that add up to 7, we note that the only possible squares we can use are those not exceeding 7, namely, 0, 1 , and 4. Because no three terms where each term is 0, 1 , or 4 add up to 7, it follows that 7 is a counterexample. We conclude that the statement "Every positive integer is the sum of the squares of three integers" is false. We have shown that not every positive integer is the sum of the squares of three integers. The next question to ask is whether every positive integer is the sum of the squares of four positive integers. Some experimentation provides evidence that the answer is yes. For example, 7 = 1 2 + 1 2 + 1 2 + 22 , 25 = 42 + 22 + 2 2 + 1 2 , and 87 = 92 + 2 2 + 1 2 + 1 2 . It turns out the conjecture "Every positive integer is the sum of the squares offour integers" is true. For a proof, � see [Ro05].

Proof Strategy in Action Mathematics is generally taught as if mathematical facts were carved in stone. Mathematics texts (including the bulk of this book) formally present theorems and their proofs. Such presen­ tations do not convey the discovery process in mathematics. This process begins with exploring concepts and examples, asking questions, formulating conjectures, and attempting to settle these conjectures either by proof or by counterexample. These are the day-to-day activities of math­ ematicians. Believe it or not, the material taught in textbooks was originally developed in this way. People formulate conjectures on the basis of many types of possible evidence. The exam­ ination of special cases can lead to a conjecture, as can the identification of possible patterns. Altering the hypotheses and conclusions of known theorems also can lead to plausible conjec­ tures. At other times, conjectures are made based on intuition or a belief that a result holds. No matter how a conjecture was made, once it has been formulated, the goal is to prove or disprove it. When mathematicians believe that a conjecture may be true, they try to find a proof. If they can­ not find a proof, they may look for a counterexample. When they cannot find a counterexample, they may switch gears and once again try to prove the conjecture. Although many conjectures are quickly settled, a few conjectures resist attack for hundreds of years and lead to the devel­ opment of new parts of mathematics. We will mention a few famous conjectures later in this section.

Tilings

unkS �

We can illustrate aspects of proof strategy through a brief study of tHings of checkerboards. Looking at tHings of checkerboards is a fruitful way to quickly discover and prove many different results, with these proofs using a variety of proof methods. There are almost an endless number of conjectures that can be made and studied in this area too. To begin, we need to define some terms. A checkerboard is a rectangle divided into squares of the same size by horizontal and vertical lines. The game of checkers is played on a board with 8 rows and 8 columns; this board is called the standard checkerboard and is shown in Figure 2. In this section we use the term board to refer to a checkerboard of any rectangular size as well as parts of checkerboards obtained by removing one or more squares. A domino is a rectangular piece that is one square by two squares, as shown in Figure 3 . We say that a board is tiled by dominoes when all its squares are covered with no overlapping dominoes and no dominoes overhanging the board. We now develop some results about tiling boards using dominoes.

98

1 / The Foundations: Logic and Proofs

1 - 98

B

rn FIGURE

EXAMPLE 18

2

The Standard Checkerboard.

FIGURE 3

Two Dominoes.

Can we tile the standard checkerboard using dominoes?

Solution: We can find many ways to tile the standard checkerboard using dominoes. For example, we can tile it by placing 32 dominoes horizontally, as shown in Figure 4. The existence of one such tiling completes a constructive existence proof. Of course, there are a large number of other ways to do this tiling. We can place 32 dominoes vertically on the board or we can place some tiles vertically and some horizontally. But for a constructive existence proof we needed to .... find just one such tiling. EXAMPLE 19

Can we tile a board obtained by removing one ofthe four comer squares of a standard checker­ board?

Solution: To answer this question, note that a standard checkerboard has 64 squares, so removing a square produces a board with 63 squares. Now suppose that we could tile a board obtained from the standard checkerboard by removing a comer square. The board has an even number of squares because each domino covers two squares and no two dominoes overlap and no dominoes overhang the board. Consequently, we can prove by contradiction that a standard checkerboard with one square removed cannot be tiled using dominoes because such a board has an odd .... number of squares. We now consider a trickier situation. EXAMPLE 20

Can we tile the board obtained by deleting the upper left and lower right comer squares of a standard checkerboard, shown in Figure 5?

Solution: A board obtained by deleting two squares of a standard checkerboard contains 64 2 = 62 squares. Because 62 is even, we cannot quickly rule out the existence of a tiling of the standard checkerboard with its upper left and lower right squares removed, unlike Example 1 9, where we ruled out the existence of a tiling of the standard checkerboard with one comer square removed. Trying to construct a tiling of this board by successively placing dominoes might be a first approach, as the reader should attempt. However, no matter how much we try, we cannot find such a tiling. Because our efforts do not produce a tiling, we are led to conjecture that no tiling exists.

1-99

1 .7 Proof Methods and Strategy

99

OJ OJ OJ OJOJOJOJ FIGURE 4

Tiling the Standard Checkerboard.

FIGURE 5 The Standard Checkerboard with the Upper Left and Lower Right Corners Removed.

We might try to prove that no tiling exists by showing that we reach a dead end however we successively place dominoes on the board. To construct such a proof, we would have to consider all possible cases that arise as we run through all possible choices of successively placing dominoes. For example, we have two choices for covering the square in the second column of the first row, next to the removed top left comer. We could cover it with a horizontally placed tile or a vertically placed tile. Each of these two choices leads to further choices, and so on. It does not take long to see that this is not a fruitful plan of attack for a person, although a computer could be used to complete such a proof by exhaustion. (Exercise 2 1 asks you to supply such a proof to show that a 4 x 4 checkerboard with opposite comers removed cannot be tiled.) We need another approach. Perhaps there is an easier way to prove there is no tiling of a standard checkerboard with two opposite comers. As with many proofs, a key observation can help. We color the squares of this checkerboard using alternating white and black squares, as in Figure 2. Observe that a domino in a tiling of such a board covers one white square and one black square. Next, note that this board has unequal numbers of white square and black squares. We can use these observations to prove by contradiction that a standard checkerboard with opposite comers removed cannot be tiled using dominoes. We now present such a proof.

Eb FIGURE 6 A Right Triomino and a Straight Triomino.

Proof: Suppose we can use dominoes to tile a standard checkerboard with opposite comers removed. Note that the standard checkerboard with opposite comers removed contains 64 - 2 = 62 squares. The tiling would use 62/2 = 3 1 dominoes. Note that each domino in this tiling covers one white and one black square. Consequently, the tiling covers 3 1 white squares and 3 1 black squares. However, when we remove two opposite comer squares, either 32 of the remaining squares are white and 30 are black or else 30 are white and 32 are black. This contradicts the assumption that we can use dominoes to cover a standard checkerboard with opposite comers removed, completing the proof. .... We can use other types of pieces besides dominoes in tilings. Instead of dominoes we can study tilings that use identically shaped pieces constructed from congruent squares that are connected along their edges. Such pieces are called polyominoes, a term coined in 1 953 by the mathematician Solomon Golomb, the author of an entertaining book about them [G094] . We will consider two polyominoes with the same number of squares the same if we can rotate and/or flip one ofthe polyominoes to get the other one. For example, there are two types of triominoes (see Figure 6), which are polyominoes made up of three squares connected by their sides. One

100

1 / The Foundations: Logic and Proofs

/ - / 00

type of triomino, the straight triomino, has three horizontally connected squares; the other type, right triominoes, resembles the letter L in shape, flipped and/or rotated, if necessary. We will study the tHings of a checkerboard by straight triominoes here; we will study tilings by right triominoes in Section 4. 1 .

EXAMPLE 21

Can you use straight triominoes to tile a standard checkerboard?

Solution: The standard checkerboard contains 64 squares and each triomino covers three squares. Consequently, if triominoes tile a board, the number of squares of the board must be a mul­ tiple of 3 . Because 64 is not a multiple of 3, triominoes cannot be used to cover an 8 x 8 .... checkerboard. In Example 22, we consider the problem of using straight triominoes to tile a standard checkerboard with one comer missing.

EXAMPLE 22

Can we use straight triominoes to tile a standard checkerboard with one of its four comers removed? An 8 x 8 checkerboard with one comer removed contains 64 1 = 63 squares. Any tiling by straight triominoes of one of these four boards uses 63/3 = 2 1 triominoes. However, when we experiment, we cannot find a tiling of one of these boards using straight triominoes. A proof by exhaustion does not appear promising. Can we adapt our proof from Example 20 to prove that no such tiling exists? -

Solution: We will color the squares of the checkerboard in an attempt to adapt the proof by contradiction we gave in Example 20 of the impossibility of using dominoes to tile a standard checkerboard with opposite comers removed. Because we are using straight triominoes rather than dominoes, we color the squares using three colors rather than two colors, as shown in Figure 7. Note that there are 2 1 green squares, 2 1 black squares, and 22 white squares in this coloring. Next, we make the crucial observation that when a straight triomino covers three squares of the checkerboard, it covers one green square, one black square, and one white square. Next, note that each of the three colors appears in a comer square. Thus without loss of generality, we may assume that we have rotated the coloring so that the missing square is colored green. Therefore, we assume that the remaining board contains 20 green squares, 2 1 black squares, and 22 white squares. If we could tile this board using straight triominoes, then we would use 63/3 = 2 1 straight triominoes. These triominoes would cover 2 1 green squares, 2 1 black squares, and 2 1 white squares. This contradicts the fact that this board contains 20 green squares, 2 1 black squares, .... and 22 white squares. Therefore we cannot tile this board using straight triominoes.

The Role of Open Problems Many advances in mathematics have been made by people trying to solve famous unsolved problems. In the past 20 years, many unsolved problems have finally been resolved, such as the proof of a conjecture in number theory made more than 300 years ago. This conjecture asserts the truth of the statement known as Fermat's Last Theorem.

THEOREM

1

FERMAT'S LAST THEOREM

The equation

x n + yn = z n

has no solutions in integers x,

y,

and z with xyz i= 0 whenever n is an integer with n

>

2.

1 .7 Proof Methods and Strategy

1-101

101

FIGURE 7 Coloring the Squares of the Standard Checkerboard with Opposite Corners Removed with Three Colors.

unkS



2

Remark: The equation x + y

2 = z 2 has infinitely many solutions in integers x, y, and z; these

solutions are called Pythagorean triples and correspond to the lengths of the sides of right triangles with integer lengths. See Exercise 30.

This problem has a fascinating history. In the seventeenth century, Fermat jotted in the margin of his copy of the works of Diophantus that he had a "wondrous proof" that there are no integer solutions of x n + y n = zn when n is an integer greater than 2 with xyz =1= O. However, he never published a proof (Fermat published almost nothing), and no proof could be found in the papers he left when he died. Mathematicians looked for a proof for three centuries without success, although many people were convinced that a relatively simple proof could be found. (Proofs of special cases were found, such as the proof of the case when n = 3 by Euler and the proof of the n = 4 case by Fermat himself.) Over the years, several established mathematicians thought that they had proved this theorem. In the nineteenth century, one ofthese failed attempts led to the development of the part of number theory called algebraic number theory. A correct proof, requiring hundreds of pages of advanced mathematics, was not found until the 1 990s, when Andrew Wiles used recently developed ideas from a sophisticated area of number theory called the theory of elliptic curves to prove Fermat's last theorem. Wiles's quest to find a proof of Fermat's last theorem using this powerful theory, described in a program in the Nova series on public television, took close to ten years ! (The interested reader should consult [Ro05] for more information about Fermat's Last Theorem and for additional references concerning this problem and its resolution.) We now state an open problem that is simple to describe, but that seems quite difficult to resolve. EXAMPLE 23 unkS



The 3x + 1 Conjecture Let T be the transformation that sends an even integer x to x /2 and an odd integer x to 3x + 1 . A famous conjecture, sometimes known as the 3x + 1 conjecture, states that for all positive integers x, when we repeatedly apply the transformation T , we will eventually reach the integer 1 . For example, starting with x = 1 3 , we find T ( 1 3) = 3 . 1 3 + 1 = 40, T (40) = 40/2 = 20, T (20) = 20/2 = 1 0, T ( 1 0) = 1 0/2 = 5 , T (5) = 3 · 5 + 1 = 1 6, T ( 1 6) = 8, T (8) = 4, T (4) = 2, and T (2) = 1 . The 3x + 1 conjecture has been verified for all integers x up to 5 . 6 · 1 0 1 3 .

102

I / The Foundations: Logic and Proofs

1 - 1 02

The 3x + 1 conjecture has an interesting history and has attracted the attention of math­ ematicians since the 1 950s. The conjecture has been raised many times and goes by many other names, including the Collatz problem, Hasse's algorithm, Ulam's problem, the Syracuse problem, and Kakutani's problem. Many mathematicians have been diverted from their work to spend time attacking this conjecture. This led to the joke that this problem was part of a conspiracy to slow down American mathematical research. See the article by Jeffrey Lagarias [La85] for a fascinating discussion of this problem and the results that have been found by .... mathematicians attacking it. In Chapter 3 we will describe additional open questions about prime numbers. Students already familiar with the basic notions about primes might want to explore Section 3 .4, where these open questions are discussed. We will mention other important open questions throughout the b09k.

Additional Proof Methods In this chapter we introduced the basic methods used in proofs. We also described how to leverage these methods to prove a variety of results. We will use these proof methods in Chapters 2 and 3 to prove results about sets, functions, algorithms, and number theory. Among the theorems we will prove is the famous halting theorem which states that there is a problem that cannot be solved using any procedure. However, there are many important proof methods besides those we have covered. We will introduce some of these methods later in this book. In particular, in Section 4. 1 we will discuss mathematical induction, which is an extremely useful method for proving statements of the form Vn P (n ) , where the domain consists of all positive integers. In Section 4.3 we will introduce structural induction, which can be used to prove results about recursively defined sets. We will use the Cantor diagonalization method, which can be used to prove results about the size of infinite sets, in Section 2.4. In Chapter 5 we will introduce the notion of combinatorial proofs, which can be used to prove results by counting arguments. The reader should note that entire books have been devoted to the activities discussed in this section, including many excellent works by George P6lya ([P06 1 ] , [P07 1 ] , [P090]). Finally, note that we have not given a procedure that can be used for proving theorems in mathematics. It is a deep theorem of mathematical logic that there is no such procedure.

Exercises 1.

Prove that n 2 + I ::: 1 :::: n ::::

4.

2n

when n is a positive integer with

2. Prove that there are no positive perfect cubes less 1 000 that are the sum of the cubes of two positive integers. 3. Prove that if x and y are real numbers, then max(x , y) + min(x , y) = x + y. [Hint: Use a proof by cases, with the two cases corresponding to x ::: y and x < y, respectively. ] 4. Use a proof by cases to show that min(a , min(b, c» = min(min(a , b), c) whenever a, b, and c are real numbers. 5. Prove the triangle inequality, which states that if x and y are real numbers, then Ix l + I y l ::: Ix + y l (where Ix l rep­ resents the absolute value of x, which equals x if x ::: 0 and equals -x if x < 0).

6. Prove that there is a positive integer that equals the sum of the positive integers not exceeding it. Is your proof constructive or nonconstructive? 7. Prove that there are 1 00 consecutive positive integers that are not perfect squares. Is your proof constructive or non­ constructive? 8. Prove that either 1 05 00 + 1 5 or 1 0500 + 1 6 is





not a perfect square. Is your proof constructive or nonconstructive? 9. Prove that there exists a pair of consecutive integers such that one of these integers is a perfect square and the other is a perfect cube. 10. Show that the product of two of the numbers 65 1000 - 5 8 1 92 + + 2 2001 , and 8 2001 + 3 1 77 , 7 9 1 2 1 2 _

92399

244493

1-103

1 .7 Proof Methods and Strategy

7 1 777 is nonnegative. Is your proof constructive or noncon­ structive? Do not try to evaluate these numbers ! ] 1 1 . Prove or disprove that there i s a rational number x and an irrational number y such that xY is irrational. 12. Prove or disprove that if a and b are rational numbers, then a b is also rational. 13. Show that each of these statements can be used to ex­ press the fact that there is a unique element x such that P(x) is true. [Note that we can also write this statement as 3 ! x P (x).] a) 3xVy(P(y) ++ x = y) b) 3x P(x) /\ VxVy(P(x) /\ P (y) -+ X = y) c) 3x(P(x) /\ Vy(P(y) -+ x = y» 14. Show that if a, b, and c are real numbers and a =I 0, then there is a unique solution of the equation ax + b = c. 15. Suppose that a and b are odd integers with a =I b. Show there is a unique integer c such that la - cl = I b - c l . 16. Show that i f r i s an irrational number, there i s a unique integer such that the distance between r and is less than 1 /2. 17. Show that if is an odd integer, then there is a unique integer k such that is the sum of k - 2 and k + 3 . 18. Prove that given a real number x there exist unique num­ bers and E such that x = + E, is an integer, and O � E < l. 19. Prove that given a real number x there exist unique num­ bers n and E such that x = - E , is an integer, and O � E < l. 20. Use forward reasoning to show that if x is a nonzero real number, then x 2 + l /x 2 :::: 2. Start with the inequality (x - l /x)2 :::: 0 which holds for all nonzero real numbers x .] 21. The harmonic mean of two real numbers x and y equals 2xy /(x + y). By computing the harmonic and geometric means of different pairs of positive real numbers, formu­ late a conjecture about their relative sizes and prove your conjecture. 22. The quadratic mean of two real numbers x and y equals J(x 2 + y2 )/2. By computing the arithmetic and quadratic means of different pairs of positive real num­ bers, formulate a conjecture about their relative sizes and prove your conjecture. *23. Write the numbers 1 , 2, on a blackboard, where is an odd integer. Pick any two of the numbers, and k, write Ij - kl on the board and erase and k. Continue this process until only one integer is written on the board. Prove that this integer must be odd. *24. Suppose that five ones and four zeros are arranged around a circle. Between any two equal bits you insert a 0 and between any two unequal bits you insert a 1 to produce nine new bits. Then you erase the nine original bits. Show that when you iterate this procedure, you can never get nine zeros. Work backward, assuming that you did end up with nine zeros.]

[Hint:

n

n

n n

n

n

n

n

n

[Hint:

. . . , 2n

[Hint:

j

j n

103

25. Formulate a conjecture about the decimal digits that ap­

pear as the final decimal digit of the fourth power of an integer. Prove your conjecture using a proof by cases. 26. Formulate a conjecture about the final two decimal digits of the square of an integer. Prove your conjecture using a proof by cases. 27. Prove that there is no positive integer such that 2 +

n3

=

n

t oo.

n

28. Prove that there are no solutions in integers x and y to the

equation 2x 2 + 5y 2 = 14. 29. Prove that there are no solutions in positive integers x and y to the equation x4 + y4 = 625 . 30. Prove that there are infinitely many solutions i n posi­ tive integers x, y, and z to the equation x 2 + y2 = z2 . Let x = m 2 - n 2 , y = and z = m 2 + 2 , where m and are integers.] 31. Adapt the proof in Example in Section 1 .6 to prove that if = abc, where a , b, and c are positive integers, then a � �, b � �, or c � �. 32. Prove that � is irrational. 33. Prove that between every two rational numbers there is an irrational number. 34. Prove that between every rational number and every irra­ tional number there is an irrational number. *35. Let = xl Y I + X2 Y2 + . . . + Xn Yn , where X I , X2 , . . . , Xn and Y I , Y2 , , Yn are orderings of two different se­ quences of positive real numbers, each containing elements. a) Show that takes its maximum value over all order­ ings of the two sequences when both sequences are sorted (so that the elements in each sequence are in nondecreasing order). b) Show that takes its minimum value over all order­ ings ofthe two sequences when one sequence is sorted into nondecreasing order and the other is sorted into nonincreasing order. 36. Prove or disprove that if you have an 8-gallon jug of water and two empty jugs with capacities of 5 gal­ lons and 3 gallons, respectively, then you can measure 4 gallons by successively pouring some of or all of the water in a jug into another jug. 37. Verify the 3x + 1 conjecture for these integers. a) 6 b) 7 c) 1 7 d) 2 1 38. Verify the 3x + 1 conjecture for these integers. a) 1 6 b) 1 1 c) 35 d) 1 1 3 39. Prove or disprove that you can use dominoes to tile the standard checkerboard with two adjacent comers removed (that is, comers that are not opposite). 40. Prove or disprove that you can use dominoes to tile a stan­ dard checkerboard with all four comers removed. 41. Prove that you can use dominoes to tile a rectangular checkerboard with an even number of squares. 42. Prove or disprove that you can use dominoes to tile a 5 x 5 checkerboard with three comers removed.

[Hint: n

S

n

. • .

S

S

4

2mn,

n

n

104

1 / The Foundations: Logic and Proofs

1 - 1 04

43. Use a proof by exhaustion to show that a tiling using

dominoes of a 4 x 4 checkerboard with opposite cor­ ners removed does not exist. First show that you can assume that the squares in the upper left and lower right corners are removed. Number the squares of the original checkerboard from 1 to 1 6, starting in the first row, moving right in this row, then starting in the left­ most square in the second row and moving right, and so on. Remove squares 1 and 1 6 . To begin the proof, note that square is covered either by a domino laid horizontally, which covers squares and 3, or vertically, which covers squares and 6. Consider each of these cases separately, and work through all the subcases that arise.] *44. Prove that when a white square and a black square are removed from an 8 x 8 checkerboard (colored as in the text) you can tile the remaining squares of the checkerboard using dominoes. Show that when one black and one white square are removed, each part of the partition of the remaining cells formed by insert­ ing the barriers shown in the figure can be covered by dominoes.] 45. Show that by removing two white squares and two black squares from an 8 x 8 checkerboard (colored as in the text) you can make it impossible to tile the remaining squares using dominoes. *46. Find all squares, ifthey exist, on an 8 x 8 checkerboard so that the board obtained by removing one of these square can be tiled using straight triominoes. First use

[Hint:

2

2

2

[Hint:

[Hint:

Figure for Exercise 44. arguments based on coloring and rotations to eliminate as many squares as possible from consideration.] *47. a) Draw each of the five different tetrominoes, where a tetromino is a polyomino consisting of four squares. b) For each of the five different tetrominoes, prove or disprove that you can tile a standard checkerboard using these tetrominoes. *48. Prove or disprove that you can tile a l O x 10 checkerboard using straight tetrominoes.

Key Terms and Results TERMS proposition: a statement that is true or false propositional variable: a variable that represents a proposition truth value: true or false ...., p (negation ofp): the proposition with truth value opposite

to the truth value of p

logical operators: operators used to combine propositions compound proposition: a proposition constructed by combin­

ing propositions using logical operators

truth table: a table displaying the truth values of propositions p V q (disj unction ofp and q): the proposition "p or q ," which

is true if and only if at least one of p and q is true q (conjunction of p and q): the proposition "p and q " which is true if and only if both p and q are true p EB q (exclusive or of p and q): the proposition "p XOR q " which is true when exactly one of p and q is true p - q (p implies q): the proposition "if p, then q ," which is false if and only if p is true and q is false converse ofp _ q: the conditional statement q � p contrapositive of p _ q: the conditional statement

p

/\

""'q � ""'p

inverse ofp - q: the conditional statement ""'p � ""'q p � q (biconditional): the proposition "p if and only if q ,"

which is true if and only if p and q have the same truth value bit: either a 0 or a 1 Boolean variable: a variable that has a value of 0 or 1 bit operation: an operation on a bit or bits bit string: a list of bits bitwise operations: operations on bit strings that operate on each bit in one string and the corresponding bit in the other string tautology: a compound proposition that is always true contradiction: a compound proposition that is always false contingency: a compound proposition that is sometimes true and sometimes false consistent compound propositions: compound propositions for which there is an assignment of truth values to the vari­ ables that makes all these propositions true logically equivalent compound propositions: compound propositions that always have the same truth values

1 - 1 05

predicate: part of a sentence that attributes a property to the subject propositional function: a statement containing one or more variables that becomes a proposition when each of its vari­ ables is assigned a value or is bound by a quantifier domain (or universe) of discourse: the values a variable in a propositional function may take 3x P(x) (existential quantification of P(x»: the proposition that is true if and only if there exists an x in the domain such that P(x) is true 'v'xP(x) (universal quantification of P(x»: the proposition that is true if and only if P (x) is true for every x in the domain logically equivalent expressions: expressions that have the same truth value no matter which propositional functions and domains are used free variable: a variable not bound in a propositional function bound variable: a variable that is quantified scope of a quantifier: portion of a statement where the quan­ tifier binds its variable argument: a sequence of statements argument form: a sequence of compound propositions involv­ ing propositional variables premise: a statement, in an argument, or argument form, other than the final one conclusion: the final statement in an argument or argument form valid argument form: a sequence of compound propositions involving propositional variables where the truth of all the premises implies the truth of the conclusion valid argument: an argument with a valid argument form rule of inference: a valid argument form that can be used in the demonstration that arguments are valid fallacy: an invalid argument form often used incorrectly as a rule of inference (or sometimes, more generally, an incor­ rect argument) circular reasoning or begging the question: reasoning where one or more steps are based on the truth of the statement being proved theorem: a mathematical assertion that can be shown to be true conjecture: a mathematical assertion proposed to be true, but that has not been proved

Review Questions

105

proof: a demonstration that a theorem is true axiom: a statement that is assumed to be true and that can be used as a basis for proving theorems lemma: a theorem used to prove other theorems corollary: a proposition that can be proved as a consequence of a theorem that has just been proved vacuous proof: a proof that p � q is true based on the fact that p is false trivial proof: a proof that p � q is true based on the fact that q is true direct proof: a proofthat p � q is true that proceeds by show­ ing that q must be true when p is true proof by contraposition : a proof that p � q is true that pro­ ceeds by showing that p must be false when q is false proof by contradiction: a proof that p is true based on the truth of the conditional statement �p � q, where q is a contradiction exhaustive proof: a proof that establishes a result by checking a list of all cases proof by cases: a proof broken into separate cases where these cases cover all possibilities without loss of generality: an assumption in a proofthat makes it possible to prove a theorem by reducing the number of cases needed in the proof counterexample: an element x such that P (x) is false constructive existence proof: a proof that an element with a specified property exists that explicitly finds such an ele­ ment nonconstructive existence proof: a proof that an element with a specified property exists that does not explicitly find such an element rational number: a number that can be expressed as the ratio of two integers p and q such that q # 0 uniqueness proof: a proof that there is exactly one element satisfying a specified property

RESULTS The logical equivalences given in Tables 6, 7, and 8 in Section 1 .2. De Morgan's laws for quantifiers. Rules of inference for propositional calculus. Rules of inference for quantified statements.

Review Questions 1. a) Define the negation of a proposition. b) What is the negation of "This is a boring course"? 2. a) Define (using truth tables) the disjunction, conjunc­ tion, exclusive or, conditional, and biconditional of the propositions p and q . b ) What are the disjunction, conjunction, exclusive or, conditional, and biconditional ofthe propositions "I'll go to the movies tonight" and "I'll finish my discrete mathematics homework"?

3. a) Describe at least five different ways to write the con­ ditional statement p � q in English. b) Define the converse and contrapositive ofa conditional statement. c) State the converse and the contrapositive of the con­ ditional statement "If it is sunny tomorrow, then I will go for a walk in the woods." 4. a) What does it mean for two propositions to be logically equivalent?

1 I

106

The Foundations: Logic and Proofs

b) Describe the different ways to show that two com­ pound propositions are logically equivalent. c) Show in at least two different ways that the compound propositions � P v (r -+ �q) and �P v �q V �r are equivalent. 5. (Depends on the Exercise Set in Section 1.2)

6. 7.

8.

9.

a) Given a truth table, explain how to use disjunctive nor­ mal form to construct a compound proposition with this truth table. b) Explain why part (a) shows that the operators /\, v, and � are functionally complete. c) Is there an operator such that the set containing just this operator is functionally complete? What are the universal and existential quantifications of a predicate P (x)? What are their negations? a) What is the difference between the quantification 3xVy P (x , y) and Vy3x P (x , y), where P (x , y) is a predicate? b) Give an example of a predicate P (x , y) such that 3xVy P (x , y) and Vy3x P (x , y) have different truth values. Describe what is meant by a valid argument in proposi­ tional logic and show that the argument "If the earth is flat, then you can sail off the edge of the earth," "You can­ not sail off the edge of the earth," therefore, "The earth is not flat" is a valid argument. Use rules of inference to show that if the premises "All zebras have stripes" and "Mark is a zebra" are true, then the conclusion "Mark has stripes" is true.

1 - 1 06

10. a) Describe what is meant by a direct proof, a proof by contraposition, and a proof by contradiction of a con­ ditional statement P -+ q . b) Give a direct proof, a proof by contraposition and a proof by contradiction of the statement: "If n is even, then n + 4 is even." 1 1 . a) Describe a way to prove the biconditional P # q . b) Prove the statement: "The integer 3 n + 2 i s odd if and only if the integer 9n + 5 is even, where n is an integer." 12. To prove that the statements PI , P2 , P3 , and P4 are equivalent, is it sufficient to show that the conditional statements P4 -+ P2 , P3 -+ PI , and PI -+ P2 are valid? If not, provide another set of conditional statements that can be used to show that the four statements are equivalent. 13. a) Suppose that a statement of the form Vx P (x) is false. How can this be proved? b) Show that the statement "For every positive integer n, n 2 ::: 2 n " i s false. 14. What is the difference between a constructive and nonconstructive existence proof? Give an example of each. 1 5. What are the elements of a proof that there is a unique element x such that P (x), where P(x) is a propositional function? 1 6. Explain how a proof by cases can be used to prove a result about absolute values, such as the fact that Ixy l = Ix l ly l for all real numbers x and y .

Su pp lementary Exercises 1. Let P be the proposition "I will do every exercise in this book" and q be the proposition "I will get an ''I>:' in this course." Express each of these as a combination of P and q.

a) I will get an "Pl' in this course only if ! do every exer­ cise in this book. b) I will get an "Pl' in this course and I will do every exercise in this book: c) Either I will not get an "Pl' in this course or I will not do every exercise in this book. For me to get an "A" in this course it is necessary and sufficient that I do every exercise in this book. 2. Find the truth table of the compound proposition (p v

d)

q) -+ (p /\ �r).

3. Show that these compound propositions are tautologies.

a) (�q /\ (p -+ q» -+ � P b) « p v q) /\ �p) -+ q 4. Give the converse, the contrapositive, and the inverse of these conditional statements. a) If it rains today, then I will drive to work. b) If lx l = x, then x ::: O. c) If n is greater than 3, then n 2 is greater than 9.

5 . Given a conditional statement P -+ q , find the converse of its inverse, the converse of its converse, and the con­ verse of its contrapositive. 6. Given a conditional statement p -+ q , find the inverse of its inverse, the inverse of its converse, and the inverse of its contrapositive. 7. Find a compound proposition involving the propositional variables p, q , r, and s that is true when exactly three of these propositional variables are true and is false otherwise.

8. Show that these statements are inconsistent: "If Sergei takes the job offer then he will get a signing bonus." "If Sergei takes the job offer, then he will receive a higher salary." "If Sergei gets a signing bonus, then he will not receive a higher salary." "Sergei takes the job offer." 9. Show that these statements are inconsistent: "If Miranda does not take a course in discrete mathematics, then she will not graduate." "If Miranda does not graduate, then she is not qualified for the job." "If Miranda reads this book, then she is qualified for the job." "Miranda does not take a course in discrete mathematics but she reads this book."

I - I 1! 7

Supplementary Exercises

ing the propositional function G(x , y), which represents "x is the grandmother of y."

10. Suppose that you meet three people, A, B , and C , o n the

island of knights and knaves described in Example 1 8 in Section 1 . 1 . What are A, B , and C if A says "I am a knave and B is a knight" and B says "Exactly one of the three of us is a knight"? 1 1 . (Adapted from [Sm78]) Suppose that on an island there are three types of people, knights, knaves, and normals. Knights always tell the truth, knaves always lie, and nor­ mals sometimes lie and sometimes tell the truth. Detec­ tives questioned three inhabitants of the island-Amy, Brenda, and Claire-as part of the investigation of a crime. The detectives knew that one ofthe three commit­ ted the crime, but not which one. They also knew that the criminal was a knight, and that the other two were not. Ad­ ditionally, the detectives recorded these statements: Amy: "I am innocent." Brenda: "What Amy says is true." Claire: "Brenda is not a normal." After analyzing their informa­ tion, the detectives positively identified the guilty party. Who was it? 12. Show that if is a proposition, where is the condi­ tional statement "If is true, then unicorns live," then "Unicorns live" is true. Show that it follows that can­ not be a proposition. (This paradox is known as Lob s

S S

S

S

paradox.)

13. Show that the argument with premises "The tooth fairy is

a real person" and "The truth fairy is not a real person" and conclusion "You can find gold at the end of the rainbow" is a valid argument. Does this show that the conclusion is true? 14. Let P (x) be the statement "student x knows calculus" and let Q(y) be the statement "class y contains a student who knows calculus." Express each ofthese as quantifications of P (x) and Q(y). a) Some students know calculus. b) Not every student knows calculus. c) Every class has a student in it who knows calculus. d) Every student in every class knows calculus. e) There is at least one class with no students who know calculus. 15. Let P (m , n) be the statement "m divides n ," where the

domain for both variables consists of all positive inte­ gers. (By "m divides n" we mean that n = for some integer Determine the truth values of each of these statements. b) P (2, 4) a) P(4, 5) d) 3m "In P (m , n) c) "1m "In P (m , n) e) 3n "1m P (m , n) 1) Vn P ( I , n) 16. Find a domain for the quantifiers in 3x 3y(x =1= y 1\ Vz «z =1= x) 1\ (z =1= y» such that this statement is true. 1 7. Find a domain for the quantifiers in 3x 3y(x =1= y 1\ Vz « z = x) V (z = y» ) such that this statement is false. 18. Use existential and universal quantifiers to express the statement "No one has more than three grandmothers" us-

k.)

km

107

19. Use existential and universal quantifiers to express the

20.

21.

22. 23.

24. 25. 26.

27.

28.

29.

30.

31.

statement "Everyone has exactly two biological parents" using the propositional function P (x , y), which repre­ sents "x is the biological parent of y." The quantifier 3n denotes "there exists exactly n ," so that 3n x P (x) means there exist exactly n values in the do­ main such that P (x) is true. Determine the true value of these statement where the domain consists of all real numbers. b) 3 \ x(lx l = 0) a) 30 x(x z = - 1 ) d) 3 3 x(x = Ix I ) c) 3 z x(x z = 2) Express each o f these statements using existential and universal quantifiers and propositional logic where 3 n is defined in Exercise 20. b) 3 \ x P (x) a) 3 0x P (x) d ) 3 3 X P (X) c) 3z x P (x) Let P (x , y) be a propositional function. Show that 3x Vy P (x , y) --+ Vy 3x P (x , y) is a tautology. Let P (x) and Q(x) be propositional functions. Show that 3x ( P (x) --+ Q(x» and "Ix P (x) --+ 3x Q(x) always have the same truth value. If Vy 3x P (x , y) is true, does it necessarily follow that 3x Vy P (x , y) is true? If "Ix 3y P (x , y) is true, does it necessarily follow that 3x Vy P (x , y) is true? Find the negations of these statements. a) If it snows today, then I will go skiing tomorrow. b) Every person in this class understands mathematical induction. c) Some students in this class do not like discrete math­ ematics. d) In every mathematics class there is some student who falls asleep during lectures. Express this statement using quantifiers: "Every student in this class has taken some course in every department in the school of mathematical sciences." Express this statement using quantifiers: "There is a build­ ing on the campus of some college in the United States in which every room is painted white." Express the statement "There is exactly one student in this class who has taken exactly one mathematics class at this school" using the uniqueness quantifier. Then express this statement using quantifiers, without using the uniqueness quantifier. Describe a rule of inference that can be used to prove that there are exactly two elements x and y in a domain such that P (x) and P (y) are true. Express this rule of inference as a statement in English. Use rules of inference to show that if the premises Vx( P (x) --+ Q(x» , Vx(Q(x) --+ R (x» , and -.R(a), where a is in the domain, are true, then the conclusion -'P (a) is true.

108

x 3 is irrational, then x is irrational. x is irrational and x 0, then .jX is irra­ tional. Prove that given a nonnegative integer n, there is a unique nonnegative integer m such that m 2 � n (m + 1 )2 . 10 Prove that there exists an integer m such that m 2 Is your proof constructive or nonconstructive?

32. Prove that if 33. Prove that if 34. 35.

/ - / 08

1 / The Foundations: Logic and Proofs






1 000

.

36. Prove that there is a positive integer that can be written ·

as the sum of squares of positive integers in two differ­ ent ways. (Use a computer or calculator to speed up your work.)

37. Disprove the statement that every positive integer is the

sum of the cubes of eight nonnegative integers.

38. Disprove the statement that every positive integer is the

sum of at most two squares and a cube of nonnegative integers.

39. Disprove the statement that every positive integer is the

sum of 36 fifth powers of nonnegative integers.

40. Assuming the truth of the theorem that states that ...;n is

n

irrational whenever is a positive integer that is not a perfect square, prove that ..fi + .../3 is irrational.

Computer Proj ects Write programs with the specified input and output.

1. Given the truth values of the propositions p and q, find the

truth values of the conjunction, disjunction, exclusive or, conditional statement, and biconditional of these proposi­ tions. 2. Given two bit strings of length find the bitwise AND, bitwise OR, and bitwise XOR of these strings. 3. Given the truth values of the propositions p and q in fuzzy logic, find the truth value of the disjunction and the con-

n,

junction of p and q (see Exercises 40 and 4 1 of Section 1 . 1 ). *4. Given positive integers and interactively play the game of Chomp. * 5. Given a portion of a checkerboard, look for tilings of this checkerboard with various types ofpolyominoes, including dominoes, the two types oftriominoes, and larger polyomi­ noes.

m n,

Computations and Explorations Use a computational program or programs you have written to do these exercises.

1 . Look for positive integers that are not the sum of the cubes

of nine different positive integers.

2. Look for positive integers greater than 79 that are not the

sum of the fourth powers of 1 8 positive integers.

3. Find as many positive integers as you can that can be writ-

ten as the sum of cubes of positive integers, in two different ways, sharing this property with 1 729. *4. Try to find winning strategies for the game of Chomp for different initial configurations of cookies. *5. Look for tilings of checkerboards and parts of checker­ boards with polynominoes.

Writing Proj ects Respond to these with essays using outside sources.

1 . Discuss logical paradoxes, including the paradox of Epi­

menides the Cretan, Jourdain's card paradox, and the bar­ ber paradox, and how they are resolved. 2. Describe how fuzzy logic is being applied to practical ap­ plications. Consult one or more of the recent books on fuzzy logic written for general audiences. 3. Describe the basic rules of WFF 'N PROOF, developed by Layman Allen. Give examples of some of the games included in WFF 'N

of Modern Logic, PROOF.

The Game

4. Read some of the writings of Lewis Carroll on symbolic

logic. Describe in detail some of the models he used to represent logical arguments and the rules of inference he used in these arguments. 5. Extend the discussion of Prolog given in Section 1 .3, ex­ plaining in more depth how Prolog employs resolution. 6. Discuss some of the techniques used in computational logic, including Skolem's rule. 7. ''Automated theorem proving" is the task of using com­ puters to mechanically prove theorems. Discuss the goals

1 - 1 09

and applications of automated theorem proving and the progress made in developing automated theorem provers. 8. Describe how DNA computing has been used to solve instances of the satisfiability problem. 9. Discuss what is known about winning strategies in the game of Chomp.

Writing Projects

109

10. Describe various aspects of proof strategy discussed by

George P6lya in his writings on reasoning, including [P062], [P07 1 ] , and [P090] . 1 1 . Describe a few problems and results about tilings with polyominoes, as described in [G094] and [Ma9 1 ] , for example.

C H A P T E R

Basic Structures : Sets, Functions, Sequences, and Sums 2.1 Sets 2.2 Set Operations 2.3 Functions 2.4 Sequences and Summations

2.1

M

uch of discrete mathematics i s devoted to the study of discrete structures, used to represent discrete obj ects. Many important discrete structures are built using sets, which are collections of obj ects. Among the discrete structures built from sets are combinations, unordered collections of obj ects used extensively in counting; relations, sets of ordered pairs that represent relationships between obj ects; graphs, sets of vertices and edges that connect vertices; and finite state machines, used to model computing machines . These are some of the topics we will study in later chapters. The concept of a function is extremely important in discrete mathematics. A function assigns to each element of a set exactly one element of a set. Functions play important roles throughout discrete mathematics. They are used to represent the computational complexity of algorithms, to study the size of sets, to count obj ects, and in a myriad of other ways. Useful structures such as sequences and strings are special types of functions. In this chapter, we will introduce the notion of a sequence, which represents ordered lists of elements. We will introduce some important types of sequences, and we will address the problem of identifying a pattern for the terms of a sequence from its first few terms. Using the notion of a sequence, we will define what it means for a set to be countable, namely, that we can list all the elements of the set in a sequence. In our study of discrete mathematics, we will often add consecutive terms of a sequence of numbers. Because adding terms from a sequence, as well as other indexed sets of numbers, is such a common occurrence, a special notation has been developed for adding such terms . In this section, we will introduce the notation used to express summations. We will develop formulae for certain types of summations. Such summations appear throughout the study of discrete mathematics, as, for instance, when we analyze the number of steps a procedure uses to sort a list of numbers into increasing order.

Sets Introduction In this section, we study the fundamental discrete structure on which all other discrete structures are built, namely, the set. Sets are used to group obj ects together. Often, the obj ects in a set have similar properties. For instance, all the students who are currently enrolled in your school make up a set. Likewise, all the students currently taking a course in discrete mathematics at any school make up a set. In addition, those students enrolled in your school who are taking a course in discrete mathematics form a set that can be obtained by taking the elements common to the first two collections. The language of sets is a means to study such collections in an organized fashion. We now provide a definition of a set. This definition is an intuitive definition, which is not part of a formal theory of sets.

DEFINITION 1

Lijfill �m 2- /

A

set is an unordered collection of obj ects.

Note that the term object has been used without specifying what an obj ect is. This description of a set as a collection of obj ects, based on the intuitive notion of an obj ect, was first stated by the German mathematician Georg Cantor in 1 89 5 . The theory that results from this intuitive 111

112

2 / Basic Structures: Sets, Functions, Sequences, and Sums

Assessmenl



DEFINITION 2

2-2

definition of a set, and the use of the intuitive notion that any property whatever there is a set consisting of exactly the objects with this property, leads to paradoxes, or logical inconsistencies. This was shown by the English philosopher Bertrand Russell in 1 902 (see Exercise 38 for a description of one of these paradoxes). These logical inconsistencies can be avoided by building set theory beginning with axioms. We will use Cantor's original version of set theory, known as naive set theory, without developing an axiomatic version of set theory, because all sets considered in this book can be treated consistently using Cantor's original theory. The objects in a set are called the elements, or members, of the set. A set is said to contain its elements. We will now introduce notation used to describe membership in sets. We write a E A to denote that a is an element of the set A . The notation a f/ A denotes that a is not an element of the set A. Note that lowercase letters are usually used to denote elements of sets. There are several ways to describe a set. One way is to list all the members of a set, when this is possible. We use a notation where all members of the set are listed between braces. For example, the notation {a , b, c , d} represents the set with the four elements a , b, c, and d.

EXAMPLE 1

The set V of all vowels in the English alphabet can be written as V = {a , e, i,

EXAMPLE 2

The set 0 of odd positive integers less than 1 0 can be expressed by 0

EXAMPLE 3

Although sets are usually used to group together elements with common properties, there is nothing that prevents a set from having seemingly unrelated elements. For instance, {a , 2, Fred, New Jersey} is the set containing the four elements a, 2, Fred, and New Jersey. ....

,=

0,

u}.

....

{ l , 3 , 5 , 7, 9}.

Sometimes the brace notation is used to describe a set without listing all its members. Some members of the set are listed, and then ellipses ( . . . ) are used when the general pattern of the elements is obvious.

EXAMPLE 4 Extra � Examples �

The set of positive integers less than 1 00 can be denoted by { I , 2, 3 , . . . , 99} . Another way to describe a set is to use set builder notation. We characterize all those elements in the set by stating the property or properties they must have to be members. For instance, the set 0 of all odd positive integers less than 1 0 can be written as o

= {x I x is an odd positive integer less than 1 O} ,

or, specifying the universe a s the set o f positive integers, as o

= {x

E

Z+ I x is odd and x




to r while ::: begin

n

C2

, Cr : values of denominations of coins, where Cr ; a positive integer)

• • •

> ... >

n:

Ci

add a coin with value Ci to the change

end

n := n

-

Cj

We have described a greedy algorithm for making change using quarters, dimes, nickels, and pennies. We will show that this algorithm leads to an optimal solution in the sense that it uses the fewest coins possible. Before we embark on our proof, we show that there are sets of coins for which the greedy algorithm (Algorithm 6) does not necessarily produce change using the fewest coins possible. For example, if we have only quarters, dimes, and pennies (and no nickels) to use, the greedy algorithm would make change for 30 cents using six coins-a quarter and five pennies-whereas we could have used three coins, namely, three dimes.

LEMMA l

If n is a positive integer, then n cents in change using quarters, dimes, nickels, and pennies using the fewest coins possible has at most two dimes, at most one nickel, at most four pennies, and cannot have two dimes and a nickel. The amount of change in dimes, nickels, and pennies cannot exceed 24 cents.

176

/

3 The Fundamentals: Algorithms, the Integers, and Matrices

3- / 0

Proof: We use a proof by contradiction. We will show that if we had more than the specified number of coins of each type, we could replace them using fewer coins that have the same value. We note that if we had three dimes we could replace them with a quarter and a nickel, if we had two nickels we could replace them with a dime, if we had five pennies we could replace them with a nickel, and if we had two dimes and a nickel we could replace them with a quarter. Because we can have at most two dimes, one nickel, and four pennies, but we cannot have two dimes and a nickel, it follows that 24 cents is the most money we can have in dimes, nickels,

k. [This i s read as "f(x) is big-oh of g(x )."]

3-/5

3.2 The Growth of Functions

Assessment



unks



EXAMPLE l

Extra � Examples Iiiiiiiij

181

The constants C and k in the definition of big-O notation are called witnesses to the relationship I(x) is O (g(x» . To establish that I(x) is O (g(x» we need only one pair of witnesses to this relationship. That is, to show that I(x) is O (g(x» , we need find only one pair of constants C and k, the witnesses, such that I /(x) 1 � C lg(x) 1 whenever x > k. Note that when there is one pair of witnesses to the relationship I(x) is O (g(x» , there are irifinitely many pairs of witnesses. To see this, note that if C and k are one pair of witnesses, then any pair C' and k', where C < C' and k < k', is also a pair of witnesses, because I /(x) 1 � C jg(x) 1 � C' lg(x) 1 whenever x > k' > k. A useful approach for finding a pair of witnesses is to first select a value of k for which the size of I /(x) 1 can be readily estimated when x > k and to see whether we can use this estimate to find a value of C for which I /(x) 1 < C Ig(x) 1 for x > k. This approach is illustrated in Example I . Show that I(x) = x 2 + 2x + 1 is O (x 2 ).

Solution: We observe that we can readily estimate the size of I(x) when x > I because x and 1 < x 2 when x > 1 . It follows that


1 , as shown in Figure 1 . Consequently, we can take C = 4 and k = 1 as witnesses to show that I(x) is O (x 2 ). That is, I(x) = x 2 + 2x + 1 < 4x 2 whenever x > 1 . (Note that it is not necessary to use absolute values here because all functions in these equalities are positive when x is positive.) Alternatively, we can estimate the size of I(x) when x > 2. When x > 2, we have 2x � x 2 and 1 � x 2 • Consequently, if x > 2, we have

It follows that C = 3 and k = 2 are also witnesses to the relation I(x) is O (x 2 ).

4

3

The part of the graph of/(x) x 2 + 2x + 1 that satisfies/ex) < 4x 2 is shown in color. =

2

2 FIGURE 1

The Function x2 + 2x + 1 is O(x2 ).

182

3/

The Fundamentals: Algorithms, the Integers, and Matrices

3- 1 6

Observe that in the relationship "f(x) is O (x 2 )," x 2 can be replaced by any function with larger values than x 2 • For example, f(x ) is O (x 3 ), f(x) is O (x 2 + X + 7), and so on. It is also true that x 2 is O (x 2 + 2x + 1 ), because x 2 < x 2 + 2x + 1 whenever x > 1 . This .... means that C = 1 and k = 1 are witnesses to the relationship x 2 is O (x 2 + 2x + 1 ). Note that in Example 1 we have two functions, f(x) = x 2 + 2x + 1 and g(x) = x 2 , such that f(x) is O (g(x)) and g(x) is O (f(x))-the latter fact following from the inequality x 2 ::: x 2 + 2x + 1 , which holds for all nonnegative real numbers x . We say that two functions f(x) and g(x) that satisfy both of these big-O relationships are of the same order. We will return to this notion later in this section.

= O (g(x)). However, the equals sign in this notation does not represent a genuine equality. Rather, this notation tells us that an inequality holds relating the values of the functions f and g for sufficiently large numbers in the domains of these functions. However, it is acceptable to write f(x) E O (g(x)) because O (g(x)) represents the set of functions that are O (g(x)).

Remark: The fact that f(x) is O (g(x)) is sometimes written f(x)

Big-O notation has been used in mathematics for more than a century. In computer science it is widely used in the analysis of algorithms, as will be seen in Section 3.3. The German mathematician Paul Bachmann first introduced big- O notation in 1 892 in an important book on number theory. The big- O symbol is sometimes called a Landau symbol after the German mathematician Edmund Landau, who used this notation throughout his work. The use ofbig- O notation in computer science was popularized by Donald Knuth, who also introduced the big-Q and big-e notations defined later in this section. When f(x) is o (g(x)), and h(x) is a function that has larger absolute values than g(x) does for sufficiently large values of x, it follows that f(x) is O (h(x)). In other words, the function g(x) in the relationship f(x) is o (g(x )) can be replaced by a function with larger absolute values. To see this, note that if if x

>

k,

Ig(x ) 1 for all x

>

k, then

>

k.

I f(x) 1 ::: C lg(x) 1 and if I h (x ) 1

>

I f(x) 1 ::: C l h (x ) 1

ifx

Hence, f(x) is O (h (x)). When big- O notation is used, the function g in the relationship f(x ) is O (g(x)) is chosen to be as small as possible (sometimes from a set of reference functions, such as functions of the form x n , where n is a positive integer).

unllS � PAUL GUSTAV HEINRICH BACHMANN ( 1 837- 1 920) Paul Bachmann, the son ofa Lutheran pastor, shared his father's pious lifestyle and love of music. His mathematical talent was discovered by one of his teachers, even though he had difficulties with some of his early mathematical studies. After recuperating from tuberculosis in Switzerland, Bachmann studied mathematics, first at the University of Berlin and later at Gottingen, where he attended lectures presented by the famous number theorist Dirichlet. He received his doctorate under the German number theorist Kummer in 1 862; his thesis was on group theory. Bachmann was a professor at Breslau and later at Munster. After he retired from his professorship, he continued his mathematical writing, played the piano, and served as a music critic for newspapers. Bachmann's mathematical writings include a five-volume survey of results and methods in number theory, a two-volume work on elementary number theory, a book on irrational numbers, and a book on the famous conjecture known as Fermat's Last Theorem. He introduced big-O notation in his 1 892 book Analytische Zahlentheorie.

3-1 7

3 .2 The Growth of Functions

183

The part of the graph ofj(x) that satisfies j(x) < Cg(x) is shown in color.

k

FIGURE 2

The Function I(x) is O(g(x» .

In subsequent discussions, we will almost always deal with functions that take on only positive values. All references to absolute values can be dropped when working with big- O estimates for such functions. Figure 2 illustrates the relationship I(x) is O (g(x » . Example 2 illustrates how big- O notation is used to estimate the growth of functions.

EXAMPLE 2

Show that 7x 2 is O (x 3 ).

Solution: Note that when x > 7, we have 7x 2 < X 3 • (We can obtain this inequality by multiplying both sides of x > 7 by x 2 .) Consequently, we can take e = 1 and k = 7 as witnesses to establish the relationship 7x 2 is O (x 3 ). Alternatively, when x > 1 , we have 7x 2 < 7x 3 , so that e = 7 and .... k = 1 are also witnesses to the relationship 7x 2 is O (x 3 ). Example 3 illustrates how to show that a big- O relationship does not hold.

EXAMPLE 3

Show that n 2 is not O (n).

Solution: To show that n 2 is not O (n), we must show that no pair of constants e and k exist such that n 2 � en whenever n > k. To see that there are no such constants, observe that when n > 0 we can divide both sides of the inequality n 2 � en by n to obtain the equivalent inequality n :::: e . We now see that no matter what e and k are, the inequality n � e cannot hold for all n with n > k. In particular, once we set a value of k, we see that when n is larger than the .... maximum of k and e , it is not true that n � e even though n > k.

unks � EDMUND LANDAU ( 1 877- 1 938) Edmund Landau, the son of a Berlin gynecologist, attended high school and university in Berlin. He received his doctorate in 1 899, under the direction of Frobenius. Landau first taught at the University of Berlin and then moved to Gottingen, where he was a full professor until the Nazis forced him to stop teaching. Landau's main contributions to mathematics were in the field of analytic number theory. In particular, he established several important results concerning the distribution of primes. He authored a three-volume exposition on number theory as well as other books on number theory and mathematical analysis.

184

/

3 The Fundamentals: Algorithms, the Integers, and Matrices

EXAMPLE

4

3- / 8

Example 2 shows that 7x 2 is O (x 3 ). Is it also true that x 3 is O (7x 2 )?

Solution: To determine whether x 3 is 0 (7x 2 ), we need to determine whether there are constants C and k such that x 3 ::::: C (7x 2 ) whenever x > k. The inequality x 3 ::::: C(7x 2 ) is equivalent to the inequality x ::::: 7C , which follows by dividing the original inequality by the positive quantity x 2 . Note that no C exists for which x ::::: 7C for all x > k no matter what k is, because x can be made arbitrarily large. It follows that no witnesses C and k exist for this proposed big-O

185

1 we have

I f(x) 1 = l an x n + an_ I x n - 1 + . . . + alx + ao l ::::: Ian Ix n + l an_ I l x n - 1 + . . . + l al l x + l ao l = x n (Ian I + l an - I I /x + . . + l a l l /x n - I + l ao l /x n ) ::::: x n (Ian l + l an - I I + . . . + l al l + l ao ! ) . .

This shows that

where C = Ian I + l an - I I + . . . + l ao I whenever x + . . . + l ao l and k = 1 show that f(x) is O (x n ).

>

1 . Hence, the witnesses C = I an I + l an - I I

k) , and

I fz(x) 1 :s C2 Ig2 (X) 1 when x

>

k2 . To estimate the sum o f fl (x) and fz(x), note that

l UI + fz)(x ) 1 = I fl (x) + fz(x ) 1 :s I fl (x) 1 + I fz(x) 1

using the triangle inequality la + h i ::: l a l + I h I -

When x i s greater than both k l and k2 , it follows from the inequalities for I fl (x ) 1 and I fz(x ) 1 that I fl (x) 1 + I fz(x) 1 :s C l lgl (x) 1 + C2 Ig2 (X) 1 :s Cl lg(x) 1 + C 2 Ig(x ) 1

= (C I + C2 )lg(x) 1 = C lg(x) I ,

where C = C I + C2 and g(x) = max( lgl (x) l , Ig2 (X) I ). [Here max(a , b) denotes the maximum, or larger, of a and b.] This inequality shows that l UI + fz)(x) I :s C lg(x ) 1 whenever x > k, where k = max(k l , k2 ). We state this useful result as Theorem 2 .

188

3/

The Fundamentals: Algorithms, the Integers, and Matrices

THEOREM 2

3-22

Suppose that II (x) is O (gl (x)) and h ex) is 0 (g2(X)). Then (II + h)(x) is O (max(lgl (x) l , Ig2(X) I )) · We often have big- 0 estimates for II and 12 in terms of the same function g. In this situation, Theorem 2 can be used to show that (II + h)(x) is also O (g(x)), because max(g(x), g(x)) = g(x). This result is stated in Corollary 1 .

COROLLARY 1

Suppose that II (x) and h(X) are both O (g(x )). Then (II + h)(x ) is O (g(x)). In a similar way big- O estimates can be derived for the product of the functions II and h. When x is greater than max(kl , k2 ) it follows that 1 (ll h)(x ) 1 = 1 /1 (x) l l h(x) 1 :::: C 1 Igl (X) I C2 Ig2 (X) 1 :::: C I C2 1 (gl g2)(x) 1 :::: C I (gl g2 )(x ) l , where C = C I C2 . From this inequality, it follows that II (x)h(x) is 0 (g l g2 ), because there are constants C and k, namely, C = C I C2 and k = max(kl , k2), such that I (ld2 )(X) I :::: C lgl (X)g2 (X) 1 whenever x > k. This result is stated in Theorem 3.

THEOREM 3

Suppose that II (x) is O (g I (X)) and 12(x) is 0 (g2(X)). Then (ll h)(x) is 0 (gl (X)g2(X)). The goal in using big-O notation to estimate functions is to choose a function g(x) that grows relatively slowly so that I(x) is 0 (g(x )). Examples 8 and 9 illustrate how to use Theorems 2 and 3 to do this. The type of analysis given in these examples is often used in the analysis of the time used to solve problems using computer programs.

EXAMPLE 8

Give a big- O estimate for I(n) = 3n log(n !) + (n 2 + 3) log n , where n is a positive integer.

Solution: First, the product 3n log(n !) will be estimated. From Example 6 we know that log(n !) is O (n log n). Using this estimate and the fact that 3n is O (n), Theorem 3 gives the estimate that 3n 10g(n !) is 0 (n 2 10g n). Next, the product (n 2 + 3) log n will be estimated. Because (n 2 + 3) < 2n 2 when n > 2, it follows that n 2 + 3 is 0 (n 2 ). Thus, from Theorem 3 it follows that (n 2 + 3) log n is 0 (n 2 10g n). Using Theorem 2 to combine the two big-O estimates for the products shows that I(n) = .... 3n 10g(n ! ) + (n 2 + 3) log n is 0 (n 2 10g n). EXAMPLE 9

Give a big- O estimate for I(x) = (x + 1 ) log(x 2 + 1 ) + 3x 2 .

Solution: First, a big- O estimate for (x + 1 ) 10g(x 2 + 1 ) will be found. Note that (x + 1 ) is o (x). Furthermore, x 2 + 1 :::: 2X 2 when x > 1 . Hence, log(x 2 + 1 ) :::: log(2x 2 ) = log 2 + log x 2 = log 2 + 2 10g x :::: 3 10g x , if x

>

2. This shows that log(x 2 + 1 ) i s O (log x).

3.2 The Growth of Functions

3-23

1 89

From Theorem 3 it follows that (x + 1 ) log(x 2 + 1 ) is O (x 10g x). Because 3x 2 is O (x 2 ), Theorem 2 tells us that f(x) is O (max(x log x, x 2 » . Because x log x ::::: x 2 , for x > 1 , it follows .... that f(x) is O (x 2 ).

Big-Omega and Big-Theta Notation Big- O notation is used extensively to describe the growth of functions, but it has limitations. In particular, when f(x) is O (g(x» , we have an upper bound, in terms of g(x), for the size of f(x) for large values ofx . However, big-O notation does not provide a lower bound for the size of f(x) for large x . For this, we use big-Omega (big-Q) notation. When we want to give both an upper and a lower bound on the size of a function f(x), relative to a reference function g(x), we use big­ Theta (big-8) notation. Both big-Omega and big-Theta notation were introduced by Donald Knuth in the 1 970s. His motivation for introducing these notations was the common misuse of big- O notation when both an upper and a lower bound on the size of a function are needed. We now define big-Omega notation and illustrate its use. After doing so, we will do the same for big-Theta notation. There is a strong connection between big- O and big-Omega notation. In particular, f(x) is Q(g(x» if and only if g(x) is o (f(x» . We leave the verification of this fact as a straightforward exercise for the reader.

DEFINITION 2

Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is Q(g(x» if there are positive constants C and k such that I f(x ) 1 ::: C lg(x ) 1

whenever x

EXAMPLE 10

>

k. [This is read as "f(x) is big Omega o f g(x)."] -

The function f(x) = 8x 3 + 5x 2 + 7 is Q(g(x» , where g(x) is the function g(x) = x 3 • This is easy to see because f(x) = 8x 3 + 5x 2 + 7 ::: 8x 3 for all positive real numbers x . This is equivalent to saying that g(x) = x 3 is O (8x 3 + 5x 2 + 7), which can be established directly by .... turning the inequality around. Often, it is important to know the order of growth of a function in terms of some relatively simple reference function such as x n when n is a positive integer or eX , where e > 1 . Knowing the order of growth requires that we have both an upper bound and a lower bound for the size of the function. That is, given a function f(x), we want a reference function g(x) such that f(x) is O (g(x» and f(x) is Q(g(x» . Big-Theta notation, defined as follows, is used to express both of these relationships, providing both an upper and a lower bound on the size of a function.

DEFINITION 3

Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is 8(g(x» if f(x) is O (g(x» and f(x) is Q(g(x» . When f(x) is 8(g(x» , we say that "f is big-Theta of g(x)" and we al so say that f(x) is of order g(x).

When f(x) is 8(g(x» , it is also the case that g(x) is 8(f(x » . Also note that f(x) is 8(g(x» if and only if f(x) is o (g(x» and g(x) is O (f(x» (see Exercise 25).

190

3/

The Fundamentals: Algorithms, the Integers, and Matrices

3-24

Usually, when big-Theta notation is used, the function g(x) in 8 (g(x » is a relatively simple reference function, such as x n , c;X , log x , and so on, while f(x) can be relatively complicated. EXAMPLE 11

We showed (in Example 5) that the sum of the first n positive integers is O(n 2 ). Is this sum of order n 2 ?

Solution: Let fen) = 1 + 2 + 3 + . . . + n . Because we already know that fen) is O(n 2 ), to show that f(n) is of order n 2 we need to find a positive constant C such that f(n) > Cn 2 for sufficiently large integers n . To obtain a lower bound for this sum, we can ignore the first half of the terms. Summing only the terms greater than rn /21 , we find that 1 + 2 + . . . + n � rn /2 1 + ( rn /21 + I ) + . . . + n � rn/21 + rn/2 1 + . . . + rn/21 = (n - rn/21 + I ) rn/21 �

(n /2)(n /2)

= n 2 /4 .

This shows that fen) is Q(n 2 ). We conclude that fen) is of order n 2 , or in symbols, fen) is .... 8(n 2 ) . We can show that f(x) is 8 (g(x» if we can find positive real numbers C 1 and C2 and a positive real number k such that

C 1 Ig(x ) 1 :s I f(x) 1 :s C2 Ig(x)1 whenever x EXAMPLE 12

>

k. This shows that f(x) is O (g(x» and f(x) is Q(g(x» .

Show that 3x 2 + 8x log x is 8 (x 2 ) .

Solution: Because 0 :s 8x log x :s 8x 2 , it follows that 3x 2 + 8x log x :s I Ix 2 for x > I . Conse­ quently, 3x 2 + 8x log x is O (x 2 ) . Clearly, x 2 is O (3x 2 + 8x log x). Consequently, 3x 2 + 8x log x .... is 8 (x 2 ) . One useful fact is that the leading term of a polynomial determines its order. For example, if f(x) = 3x5 + X 4 + 1 7x 3 + 2, then f(x) is of order x 5 • This is stated in Theorem 4, whose proof is left as an exercise at the end of this section.

THEOREM

4

EXAMPLE 13

n

n

Let f(x) = anx + an _ l X - 1 + . . . + a l X + ao, where aO , a l , n an i- O. Then f(x) is of order x .

. . . , an

are real numbers with

The polynomials 3x8 + I Ox7 + 22 1x 2 + 1444, x 19 - 1 8x 4 - 1 0, 1 1 2, and _x 99 + 40,OOlx 98 + .... I OO, 003x are of orders x 8 , x 19 , and x 99 , respectively. Unfortunately, as Knuth observed, big- O notation is often used by careless writers and speakers as if it had the same meaning as big-Theta notation. Keep this in mind when you see big- O notation used. The recent trend has been to use big-Theta notation whenever both upper and lower bounds on the size of a function are needed.

3.2 The Growth of Functions

3-25

191

Exercises In Exercises 1-14, to establish a big- O relationship, find wit­ nesses C and such that I f(x)1 ::: C lg(x)1 whenever x >

k

1. Determine whether each of these functions is O (x). a) f(x) = 10 b) f(x) = 3x + 7 c) f(x) = x 2 + x + 1 d) f(x) = S log x e) f(x) = LxJ t) f(x) = fx/21

k.

2. Determine whether each of these functions is 0 (x 2 ). a) f(x) = 1 7x + 1 1 b) f(x) = x 2 + 1 000 c) f(x) = x log x d) f(x) = x4/2 e) f(x ) = 2x t) f(x) = LxJ . fx l 3. Use the definition of "f(x) is O (g(x» " to show that 4. 5. 6. 7.

8.

9. 10. 11. 12. 13. 14.

15. 16. 17.

18. 19.

x4 + 9x3 + 4x + 7 is 0 (X4). Use the definition of "f(x) is O (g(x» " to show that 2x + 1 7 is O W ). Show that (x 2 + I )/(x + I ) is O (x). Show that (x3 + 2x)/(2x + I) is 0 (x 2 ). Find the least integer n such that f(x) is O (x n ) for each of these functions. a) f(x) = 2x3 + x 2 10g x b) f(x) = 3x3 + (log x)4 c) f(x) = (x4 + x 2 + 1 )/(x3 + I ) d) f(x) = (x4 + S log x)/(x4 + 1 ) Find the least integer n such that f(x) i s O (x n ) for each of these functions. a) f(x) = 2x 2 + x3 10g x b) f(x) = 3x 5 + (log x)4 c) f(x) = (x4 + x 2 + 1 )/(x4 + 1 ) d) f(x) = (x3 + S log x)/(x4 + I ) Show that x 2 + 4x + 1 7 i s 0 (x3 ) but that x3 i s not 0 (x 2 + 4x + 1 7). Show that x3 is 0 (X4) but that X4 is not 0 (x3 ). Show that 3x4 + I is 0 (x4/2) and x4/2 is 0 (3x4 + I ). Show that x log x is 0 (x 2 ) but that x 2 is not O (x log x). Show that 2 n is 0 (3 n ) but that 3 n is not 0 (2n ). Is it true that x3 is o (g(x » , ifg is the given function? [For example, if g(x) = x + I , this question asks whether x3 is O(x + I ).] a) g(x) = x 2 b) g(x) = x3 d) g(x) = x 2 + x4 c) g(x) = x 2 + x3 e) g(x) = ]X t) g(x) = x3 /2 Explain what it means for a function to be 0 ( 1 ). Show that if f(x) is O (x), then f(x) is 0 (x 2 ). Suppose that f(x), g(x), and h (x) are functions such that f(x) is O (g(x» and g(x) is O(h(x» . Show that f(x) is O(h(x» . Let be a positive integer. Show that I k + 2 k + . . . + n k is O(n k + l ). Give as good a big-O estimate as possible for each of these functions. b) (n log n + n 2 )(n3 + 2) a) (n 2 + 8)(n + I )

k

c) (n ! + 2 n )(n3 + log(n 2 + I » 20. Give a big-O estimate for each o f these functions. For the

function g in your estimate f(x) is O (g), use a simple function g of smallest order. a) (n3 + n 2 10g n)(log n + I ) + ( 1 7 10g n + 1 9)(n3 + 2) b) (2n + n 2 )(n3 + 3 n ) c) (n n + n2n + s n )(n ! + s n ) 2 1 . Give a big-O estimate for each ofthese functions. For the function g in your estimate that f(x) is O (g(x» , use a simple function g of the smallest order. a) n log(n 2 + I ) + n 2 10g n b) (n log n + 1 )2 + (log n + 1 )(n 2 + I ) c) n 2n + n n 2 22. For each function in Exercise I , determine whether that function is Q(x) and whether it is 8(x). 23. For each function in Exercise 2, determine whether that function is Q(x 2 ) and whether it is 8(x 2 ). 24. a) Show that 3x + 7 is 8(x). b) Show that 2x 2 + x - 7 is 8(x 2 ). c) Show that Lx + 1 /2J is 8(x). d) Show that log(X 2 + I) is 8(log2 x). e) Show that 10g I O x is 8(log2 x). 25. Show that f(x) is 8(g(x» if and only if f(x) is O (g(x» and g(x) is O (f(x» . 26. Show that if f(x) and g(x) are functions from the set of real numbers to the set of real numbers, then f(x) is O (g(x» if and only if g(x) is Q(f(x» . 27. Show that if f(x) and g(x) are functions from the set of real numbers to the set of real numbers, then f(x) is 8(g(x» if and only if there are positive constants C 1 , and C2 such that C I Ig(x ) 1 ::: I f(x ) 1 ::: C 2 Ig(x)1 whenever x > 28. a) Show that 3x 2 + x + I is 8(3x 2 ) by directly finding the constants C 1 , and C 2 in Exercise 27. b) Express the relationship in part (a) using a picture showing the functions 3x 2 + x + I , C I . 3x 2 , and C2 · 3x 2 , and the constant on the x-axis, where C 1 , C 2 , and are the constants you found in part (a) to show that 3x 2 + x + I is 8(3x 2 ). 29. Express the relationship f(x) is 8(g(x» using a picture. Show the graphs of the functions f(x), C I Ig(x ) l , and C2 Ig(x ) l , as well as the constant on the x -axis. 30. Explain what it means for a function to be Q( 1 ). 3 1 . Explain what it means for a function to be 8( 1 ). 32. Give a big-O estimate of the product of the first n odd positive integers. 33. Show that if f and g are real-valued functions such that f(x) is O (g(x» , then for every positive integer fk (x) is O (gk (x» . [Note that fk (x) = f(x l .] 34. Show that for all real numbers a and with a > I and > I , if f(x) is o (logb x), then f(x) is O (loga x).

k,

k.

k,

k

k

k

b

b

k,

192

3 / The Fundamentals: Algorithms, the Integers, and Matrices

3-26

c) x 2 is o(2X ). d) x 2 + X + 1 is not o(x 2 ).

35. Suppose that f(x) is o (g(x » where f and g are increas­

ing and unbounded functions. Show that log I f(x)1 is O (log I g(x ) 1 ). 36. Suppose that f(x) is O (g(x» . Does it follow that 2f(x ) is . o (2g(x» ? 37. Let fl (x) and hex) be functions from the set of real num­ bers to the set of positive real numbers. Show that if fl (x) and f2 (X) are both 8(g(x » , where g(x) is a function from the set of real numbers to the set of positive real numbers, then fl (x) + hex) is 8(g(x» . Is this still true if fl (x) and f2 (X) can take negative values? 38. Suppose that f(x), g(x), and h (x ) are functions such that f(x) is 8(g(x» and g(x) is 8(h (x» . Show that f(x) is 8(h (x» . 39. If fl (x) and hex) are functions from the set of posi­ tive integers to the set of positive real numbers and fl (x) and f2 (x) are both 8(g(x» , is (fl - f2 )(X) also 8(g(x» ? Either prove that it is or give a counterexample. 40. Show that if fl (x) and f2 (X) are functions from the set of positive integers to the set of real numbers and fl (x) is 8(gl (x» and hex) is 8(g2 (X» , then (fJ /2 )(X) is 8(gl g2 (X» . 41. Find functions f and g from the set of positive integers to the set of real numbers such that fen) is not O (g(n» and g(n ) is not O (f(n» simultaneously. 42. Express the relationship f(x) is Q(g(x» using a picture. Show the graphs of the functions f(x) and Cg(x), as well as the constant k on the real axis. 43. Show that if fl (x) is 8(gl (x» , hex) is 8(g2 (X» , and f2 (X) i= 0 and g2 (x) i= 0 for all real numbers x > 0, then (fJ /h )(x) is 8« gJ /g2 )(X» . n I n 44. Show that if f(x) = anx + an_lx - + . . . + al x + ao, where ao , ai , . . . , an - I , and an are real numbers and an i= 0, then f(x) is 8(x n ). Big- O , big-Theta, and big-Omega notation can be extended to functions in more than one variable. For example, the state­ ment f(x , y) is O (g(x , y» means that there exist constants C , kl , and k2 such that I f(x , y ) 1 :::: C lg(x , y ) 1 whenever x > kl and y > k2 . 45. Define the statement f(x , y) is 8(g(x , y» . 46. Define the statement f(x , y) is Q(g(x , y» . 47. Show that (x 2 + xy + X log y)3 is O (x 6y3 ). 48. Show that x 5 y3 + x4y4 + x 3y 5 is Q(x3y 3). 49. Show that Lxy J is O (xy). 50. Show that fxYl is Q(xy). The following problems deal with another type of asymptotic notation, called Iittle-o notation. Because little-o notation is based on the concept of limits, a knowledge of calculus is needed for these problems. We say that f(x) is o(g(x» [read f(x) is "little-oh" of g(x)], when f(x) lim = o. oo ---> g(x ) x 51. (Requires calculus) Show that b) x log x is O(X 2 ). a) x 2 is o(x3 ).

52.

(Requires calculus) a) Show that if f(x ) and g(x) are functions such that f(x)

53.

54.

*55. *56. 57.

58.

59. 60.

is o(g(x» and c is a constant, then cf(x) is o(g(x» , where (cf)(x) = cf(x). b) Show that if fl (X), h ex), and g(x) are functions such that fl (x) is o(g(x» and h ex) is o(g(x» , then (fl + h)(x) is o(g(x» , where (fl + h)(x) = fl (x) + hex). (Requires calculus) Represent pictorially that x log x is O(X 2 ) by graphing x log x , x 2 , and x log x/x 2 . Explain how this picture shows that x log x is O(X 2 ). (Requires calculus) Express the relationship f(x) is o(g(x» using a picture. Show the graphs of f(x), g(x), and f(x)/g(x). (Requires calculus) Suppose that f(x) is o(g(x» . Does it follow that 2f(x ) is o(2g(x» ? (Requires calculus) Suppose that f(x ) is o(g(x» . Does it follow that log I f(x)1 is o(log Ig(x ) I )? (Requires calculus) The two parts ofthis exercise describe the relationship between little-o and big- O notation. a) Show that if f(x) and g(x) are functions such that f(x) is o(g(x» , then f(x) is O (g(x» . b) Show that if f(x) and g(x) are functions such that f(x) is O (g(x» , then it does not necessarily follow that f(x) is o(g(x» . (Requires calculus) Show that if f(x) is a polynomial of degree n and g(x) is a polynomial of degree m where m > n , then f(x) is o(g(x» . (Requires calculus) Show that if fl (x) is O (g(x» and hex) is o(g(x» , then fl (x ) + hex) is O (g(x» . (Requires calculus) Let Hn be the nth harmonic number 1 1 1 Hn = I + + + " ' + ' 2" 3 ;;

Show that Hn is o (log n). [Hint: First establish the inequality n n L -:1 < -1 dx X I j =2 J by showing that the sum of the areas of the rectangles of height I /} with base from } 1 to } , for } = 2, 3 , . . . , n, is less than the area under the curve y = 1 /x from 2 to n.] *61 . Show that n log n is O (log n ! ). � 62. Determine whether log n ! is 8(n log n). Justify your answer. *63. Show that log n! is greater than (n log n)/4 for n > 4. [Hint: Begin with the inequality n ! > n(n - 1 ) ( n - 2) · · · fn /2l] Let f(x) and g(x) be functions from the set ofreal numbers to the set of real numbers. We say that the functions f and g are asymptotic and write f(x) g(x) iflimx---> oo f(x)/g(x) = 1 . 64. (Requires calculus) For each of these pairs of functions, determine whether f and g are asymptotic.

I

-



3. 3

3-2 7

+ + + ++

+

+

a) f(x) = x2 3x 7, g(x) = x2 b) f(x) = x2 log x , g(x) = x3 c) f(x ) = X4 log(3x8 7), g(x) = (x2 1 7x 3)2 f(x) = (x3 x2 X 1 )4 , g(x ) = (x4 x3 x2 X 1 )3 .

d)

3.3

+ + ++ ++ +

10

65.

Complexity of Algorithms

193

(Requires calculus) +

For each o f these pairs o f functions, determine whether f and g are asymptotic. a) f(x) = log(X2 1), g(x) = log x b) f(x) = 2x+3 , g(x) = 2x+ 7 c) f(x) = 2 1' , g(x) = 2X 2 f(x) = 2x2 +x+ l , g(x) = 2x2 +2x

d)

Com p lexity o f Algorithms

Introduction When does an algorithm provide a satisfactory solution to a problem? First, it must always produce the correct answer. How this can be demonstrated will be discussed in Chapter 4. Second, it should be efficient. The efficiency of algorithms will be discussed in this section. How can the efficiency of an algorithm be analyzed? One measure of efficiency is the time used by a computer to solve a problem using the algorithm, when input values are of a specified size. A second measure is the amount of computer memory required to implement the algorithm when input values are of a specified size. Questions such as these involve the computational complexity ofthe algorithm. An analysis of the time required to solve a problem of a particular size involves the time complexity of the algorithm. An analysis of the computer memory required involves the space complexity of the algorithm. Considerations of the time and space complexity of an algorithm are essential when algorithms are implemented. It is obviously important to know whether an algorithm will produce an answer in a microsecond, a minute, or a billion years. Likewise, the required memory must be available to solve a problem, so that space complexity must be taken into account. Considerations of space complexity are tied in with the particular data structures used to implement the algorithm. Because data structures are not dealt with in detail in this book, space complexity will not be considered. We will restrict our attention to time complexity.

Time Complexity The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation. Time complexity is described in terms of the number of operations required instead of actual computer time because of the difference in time needed for different computers to perform basic operations. Moreover, it is quite complicated to break all operations down to the basic bit operations that a computer uses. Furthermore, the fastest computers in existence can perform basic bit operations (for instance, adding, multiplying, comparing, or exchanging two bits) in 1 0- 9 second ( 1 nanosecond), but personal computers may require 1 0- 6 second ( 1 microsecond), which is 1 000 times as long, to do the same operations. We illustrate how to analyze the time complexity of an algorithm by considering Algorithm 1 of Section 3 . 1 , which finds the maximum of a finite set of integers. EXAMPLE 1

Describe the time complexity of Algorithm 1 of Section 3 . 1 for finding the maximum element in a set.

Solution: The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used.

194

3/

The Fundamentals: Algorithms, the Integers, and Matrices

. 3-28

To find the maximum element of a set with n elements, listed in an arbitrary order, the temporary maximum is first set equal to the initial term in the list. Then, after a comparison has been done to determine that the end of the list has not yet been reached, the temporary maximum and second term are compared, updating the temporary maximum to the value of the second term if it is larger. This procedure is continued, using two additional comparisons for each term of the list--one to determine that the end of the list has not been reached and another to determine whether to update the temporary maximum. Because two comparisons are used for each of the second through the nth elements and one more comparison is used to exit the loop when i = n + 1 , exactly 2(n - 1 ) + 1 = 2 n - 1 comparisons are used whenever this algorithm is applied. Hence, the algorithm for finding the maximum of a set of n elements has ... time complexity 8(n), measured in terms of the number of comparisons used. Next, we will analyze the time complexity of searching algorithms. EXAMPLE 2

Describe the time complexity of the linear search algorithm.

Solution: The number of comparisons used by the algorithm will be taken as the measure of the time complexity. At each step of the loop in the algorithm, two comparisons are performed­ one to see whether the end of the list has been reached and one to compare the element x with a term of the list. Finally, one more comparison is made outside the loop. Consequently, if x = aj , 2i + 1 comparisons are used. The most comparisons, 2n + 2, are required when the element is not in the list. In this case, 2n comparisons are used to determine that x is not aj , for i = 1 , 2, . . . , n , an additional comparison is used to exit the loop, and one comparison is made outside the loop. So when x is not in the list, a total of 2 n + 2 comparisons are used. Hence, a ... linear search requires at most O (n) comparisons. WORST-CASE COMPLEXITY The type of complexity analysis done in Example 2 is a worst­ case analysis. By the worst-case performance of an algorithm, we mean the largest number of

operations needed to solve the given problem using this algorithm on input of specified size. Worst-case analysis tells us how many operations an algorithm requires to guarantee that it will produce a solution. EXAMPLE 3

Describe the time complexity of the binary search algorithm in terms of the number of compar­ isons used (and ignoring the time required to compute m = [(i + j) / 2] in each iteration of the loop in the algorithm).

Solution: For simplicity, assume there are n = 2 k elements in the list ai , a2 , . . . , an , where k is a nonnegative integer. Note that k = log n . (If n , the number of elements in the list, is not a power of 2, the list can be considered part of a larger list with 2 k +1 elements, where 2 k < n < 2k + l . Here 2 k + 1 is the smallest power of 2 larger than n .) At each stage of the algorithm, i and j, the locations of the first term and the last term of the restricted list at that stage, are compared to see whether the restricted list has more than one term. If i < j, a comparison is done to determine whether x is greater than the middle term of the restricted list. At the first stage the search is restricted to a list with 2 k - 1 terms. So far, two comparisons have been used. This procedure is continued, using two comparisons at each stage to restrict the search to a list with half as many terms. In other words, two comparisons are used at the first stage of the algorithm when the list has 2 k elements, two more when the search has been reduced to a list with 2 k -1 elements, two more when the search has been reduced to a list with 2k - 2 elements, and so on, until two comparisons are used when the search has been reduced to a list with 2 1 = 2 elements. Finally, when one term is left in the list, one comparison tells us that there are no additional terms left, and one more comparison is used to determine if this term is x.

3-29

3.3

Complexity of Algorithms

195

Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. (If n is not a power of 2, the original list is expanded to a list with 2 k + 1 terms, where k = L log n J , and the search requires at most 2 flog n 1 + 2 comparisons.) Consequently, a binary search requires at most 8(log n) comparisons. From this analysis it follows that the binary search algorithm is more efficient, in the worst case, than a .... linear search. AVERAGE-CASE COMPLEXITY Another important type of complexity analysis, besides worst-case analysis, is called average-case analysis. The average number of operations used to solve the problem over all inputs of a given size is found in this type of analysis. Average-case time complexity analysis is usually much more complicated than worst-case analysis. However, the average-case analysis for the linear search algorithm can be done without difficulty, as shown in Example 4. EXAMPLE

4

Describe the average-case performance of the linear search algorithm, assuming that the element is in the list.

x

Solution: There are n types of possible inputs when x is known to be in the list. If x is the first term of the list, three comparisons are needed, one to determine whether the end of the list has been reached, one to compare x and the first term, and one outside the loop. If x is the second term of the list, two more comparisons are needed, so that a total of five comparisons are used. In general, if x is the ith term of the list, two comparisons will be used at each of the i steps of the loop, and one outside the loop, so that a total of 2i + 1 comparisons are needed. Hence, the average number of comparisons used equals 3 + 5 + 7 + . . . + (2n + 1 ) n

2( 1 + 2 + 3 + . . . + n) + n n

In Section 4. 1 we will show that

1 +2+3+···+n =

n (n + 1 ) . 2

Hence, the average number of comparisons used by the linear search algorithm (when x is known to be in the list) is

2[n(n + 1 )/2] + I = n + 2, n

-----

which is 8 (n ). Remark: In the analysis in Example 4 it has been assumed that x is in the list being searched

and it is equally likely that x is in any position. It is also possible to do an average-case analysis of this algorithm when x may not be in the list (see Exercise 1 3 at the end of this section).

Remark: Although we have counted the comparisons needed to determine whether we have

reached the end of a loop, these comparisons are often not counted. From this point on we will ignore such comparisons.

WORST-CASE COMPLEXITY OF TWO SORTING ALGORITHMS We analyze the worst-case complexity of the bubble sort and the insertion sort in Examples 5 and 6. EXAMPLE

5

What is the worst-case complexity of the bubble sort in terms of the number of comparisons made?

196

3/

The Fundamentals: Algorithms, the Integers, and Matrices

3-30

Solution: The bubble sort (described in Example 4 in Section 3 . 1 ) sorts a list by performing a sequence of passes through the list. During each pass the bubble sort successively compares adjacent elements, interchanging them if necessary. When the ith pass begins, the i - I largest elements are guaranteed to be in the correct positions. During this pass, n - i comparisons are used. Consequently, the total number of comparisons used by the bubble sort to order a list of n elements is (n - l )n 2 using a summation formula that we will prove in Section 4 . 1 . Note that the bubble sort always uses this many comparisons, because it continues even if the list becomes completely sorted at some intermediate step. Consequently, the bubble sort uses (n - l )n/2 comparisons, so it has .... 8(n 2 ) worst-case complexity in terms of the number of comparisons used. (n - 1 ) + (n - 2) + . . . + 2 + 1 =

EXAMPLE 6

What is the worst-case complexity of the insertion sort in terms of the number of comparisons made?

Solution: The insertion sort (described in Section 3 . 1 ) inserts the jth element into the correct position among the first j - 1 elements that have already been put into the correct order. It does this by using a linear search technique, successively comparing the jth element with successive terms until a term that is greater than or equal to it is found or it compares aj with itself and stops because aj is not less than itself. Consequently, in the worst case, j comparisons are required to insert the jth element into the correct position. Therefore, the total number of comparisons used by the insertion sort to sort a list of n elements is 2+3+" '+n =

n (n + 1 ) - 1, 2

using the summation formula for the sum of consecutive integers that we will prove in Section 4. 1 and noting that the first term, 1 , is missing in this sum. Note that the insertion sort may use considerably fewer comparisons if the smaller elements started out at the end of the list. We .... conclude that the insertion sort has worst-case complexity 8(n 2 ).

Understanding the Complexity of Algorithms Table 1 displays some common terminology used to describe the time complexity of algorithms. For example, an algorithm that finds the largest of the first 1 00 terms of a list of n elements by TABLE 1 Commonly Used Terminology for the Complexity of Algorithms. Complexity

Terminology

8( 1 )

Constant complexity

8(log n ) 8(n)

Logarithmic complexity

8(n log n ) 8(n b )

n log n complexity

8(bn ), where b 8(n !)

Linear complexity Polynomial complexity >

1

Exponential complexity Factorial complexity

3.3

unks �

Complexity of Algorithms

197

applying Algorithm 1 to the sequence of the first 1 00 terms, where n is an integer with n ::: 1 00, has constant complexity because it uses 99 comparisons no matter what n is (as the reader can verify). The linear search algorithm has linear (worst-case or average-case) complexity and the binary search algorithm has logarithmic (worst-case) complexity. Many important algorithms have n log n complexity, such as the merge sort, which we will introduce in Chapter 4 . An algorithm has polynomial complexity if it has complexity 8(n h ), where b is an integer with b ::: 1 . For example, the bubble sort algorithm is a polynomial-time algorithm because it uses 8(n 2 ) comparisons in the worst case. An algorithm has exponential complexity if it has time complexity 8(bn ), where b > 1 . The algorithm that determines whether a compound proposition in n variables is satisfiable by checking all possible assignments of truth variables is an algorithm with exponential complexity because it uses 8(2n ) operations. Finally, an algorithm has factorial complexity if it has 8(n ! ) time complexity. The algorithm that finds all orders that a travelling salesman could use to visit n cities has factorial complexity; we will discuss this algorithm in Chapter 9. A problem that is solvable using an algorithm with polynomial worst-case complexity is called tractable, because the expectation is that the algorithm will produce the solution to the problem for reasonably sized input in a relatively short time. However, if the polynomial in the big-8 estimate has high degree (such as degree 1 00) or if the coefficients are extremely large, the algorithm may take an extremely long time to solve the problem. Consequently, that a problem can be solved using an algorithm with polynomial worst-case time complexity is no guarantee that the problem can be solved in a reasonable amount of time for even relatively small input values. Fortunately, in practice, the degree and coefficients of polynomials in such estimates are often small. The situation is much worse for problems · that cannot be solved using an algorithm with worst-case polynomial time complexity. Such problems are called intractable. Usually, but not always, an extremely large amount of time is required to solve the problem for the worst cases of even small input values. In practice, however, there are situations where an algorithm with a certain worst-case time complexity may be able to solve a problem much more quickly for most cases than for its worst case. When we are willing to allow that some, perhaps small, number of cases may not be solved in a reasonable amount of time, the average-case time complexity is a better measure of how long an algorithm takes to solve a problem. Many problems important in industry are thought to be intractable but can be practically solved for essentially all sets of input that arise in daily life. Another way that intractable problems are handled when they arise in prac­ tical applications is that instead oflooking for exact solutions of a problem, approximate solutions are sought. It may be the case that fast algorithms exist for finding such approximate solutions, perhaps even with a guarantee that they do not differ by very much from an exact solution. Some problems even exist for which it can be shown that no algorithm exists for solving them. Such problems are called unsolvable (as opposed to solvable problems that can be solved using an algorithm). The first proof that there are unsolvable problems was provided by the great English mathematician and computer scientist Alan Turing when he showed that the halting problem is unsolvable. Recall that we proved that the halting problem is unsolvable in Section 3 . 1 . (A biography of Alan Turing and a description of some of his other work can be found in Chapter 1 2.) The study of the complexity of algorithms goes far beyond what we can describe here. Note, however, that many solvable problems are believed to have the property that no algorithm with polynomial worst-case time complexity solves them, but that a solution, if known, can be checked in polynomial time. Problems for which a solution can be checked in polynomial time are said to belong to the class NP (tractable problems are said to belong to class P).* There is also an important class of problems, called NP-complete problems, with the property that if any of these problems can be solved by a polynomial worst-case time algorithm, then all problems in the class NP can be solved by polynomial worst-case time algorithms. ' N P stands

for nondeterministic polynomial time.

198

3 / The Fundamentals: Algorithms, the Integers, and Matrices

3-32

The satisfiability problem is an example of an NP-complete problem-we can quickly verify that an assignment of truth values to the variables of a compound proposition makes it true, but no polynomial time algorithm has been discovered for finding such an assignment of truth values. [For example, an exhaustive search of all possible truth values requires E>(2n ) bit operations where n is the number of variables in the compound proposition.] Furthermore, if a polynomial time algorithm for solving the satisfiability problem were known, there would be polynomial time algorithms for all problems known to be in this class of problems (and there are many important problems in this class). Despite extensive research, no polynomial worst-case time algorithm has been found for any problem in this class. It is generally accepted, although not proven, that no NP-complete problem can be solved in polynomial time. For more information about the complexity of algorithms, consult the references, including [CoLeRiStO l ] , for this section listed at the end of this book. (Also, for a more formal discussion of computational complexity in terms of Turing machines, see Section 12.5.) Note that a big-E> estimate of the time complexity of an algorithm expresses how the time required to solve the problem can change as the input grows in size. In practice, the best estimate (that is, with the smallest reference function) that can be shown is used. However, big-E> esti­ mates of time complexity cannot be directly translated into the actual amount of computer time used. One reason is that a big-E> estimate f(n) is E>(g(n» , where f(n) is the time complexity of an algorithm and g(n) is a reference function, means that CJg(n) � f(n) � C2 g(n) when n > k, where C J , C2 , and k are constants. So without knowing the constants C J , C2 , and k in the inequality, this estimate cannot be used to determine a lower bound and an upper bound on the number of operations used in the worst case. As remarked before, the time required for an oper­ ation depends on the type of operation and the computer being used. Often, instead of a big-E> estimate on the worst-case time complexity of an algorithm, we have only a big-O estimate. Note that a big-O estimate on the time complexity of an algorithm provides an upper, but not a lower, bound on the worst-case time required for the algorithm as a function of the input size. Never­ theless, for simplicity, we will often use big- O estimates when describing the time complexity of algorithms, with the understanding that big-E> estimates would provide more information. However, the time required for an algorithm to solve a problem of a specified size can be determined if all operations can be reduced to the bit operations used by the computer. Table 2 displays the time needed to solve problems of various sizes with an algorithm using the indicated number of bit operations. Times of more than 1 0 1 00 years are indicated with an asterisk. (In Section 3 . 6 the number of bit operations used to add and multiply two integers will be discussed.) In the construction of this table, each bit operation is assumed to take 1 0-9 second, which is the time required for a bit operation using the fastest computers today. In the future, these times will decrease as faster computers are developed. It is important to know how long a computer will need to solve a problem. For instance, if an algorithm requires 1 0 hours, it may be worthwhile to spend the computer time (and money)

TABLE 2 The Computer Time Used by Algorithms. Bit Operations Used

Problem Size log n

n

n

n2

n log n

3

10 1 02

3 7

1 0-9 1 0-9

1 0-8

S

1 0-7

S

S

1 0-8 1 0-7

X

X

S

7

X

1 0-7

S

1 0-5

S

1 03

1 .0

X

1 0-8

S

1 0-6

S

1

X

1 0-5

S

1 0-3

S

x

S

S

n!

2n 1 0-6 S 1 0 1 3 yr

4X

3 *

*

*

*

*

1 04

1 .3

X

1 0-8 s

1 0-5

S

1

X

1 0-4

S

1 0- 1

1 05 1 06

1 .7

X

1 0-8 s

1 0-4 1 0-3

S

2 2

X

1 0-3

S

10 s

*

*

1 7 min

*

*

2

X

1 0-8

S

S

X

1 0-2 s

S

X

1 0-3

S

3.3 Complexity of Algorithms

3-33

199

required to solve this problem. But, if an algorithm requires 1 0 billion years to solve a problem, it would be unreasonable to use resources to implement this algorithm. One of the most interesting phenomena of modem technology is the tremendous increase in the speed and memory space of computers. Another important factor that decreases the time needed to solve problems on computers is parallel processing, which is the technique of performing sequences of operations simultaneously. Efficient algorithms, including most algorithms with polynomial time complexity, bene­ fit most from significant technology improvements. However, these technology improvements offer little help in overcoming the complexity of algorithms of exponential or factorial time complexity. Because of the increased speed of computation, increases in computer memory, and the use of algorithms that take advantage of parallel processing, many problems that were considered impossible to solve five years ago are now routinely solved, and certainly five years from now this statement will still be true.

Exercises procedure

1. How many comparisons are used by the algorithm given

2.

3.

4.

in Exercise 1 6 of Section 3 . 1 to find the smallest natural number in a sequence of natural numbers? Write the algorithm that puts the first four terms of a list of arbitrary length in increasing order. Show that this al­ gorithm has time complexity 0 ( 1 ) in terms ofthe number of comparisons used. Suppose that an element is known to be among the first four elements in a list of 32 elements. Would a linear search or a binary search locate this element more rapidly? Determine the number of multiplications used to find starting with x and successively squaring (to find X4 and so on). Is this a more efficient way to find than by multiplying x by itself the appropriate number of times? Give a big-O estimate for the number of comparisons used by the algorithm that determines the number of 1 s in a bit string by examining each bit of the string to determine whether it is a 1 bit (see Exercise of Section 3 . 1 ). a) Show that this algorithm determines the number of 1 bits in the bit string S :

n

x 2'

5.

*6.

k 2 X x2 ,

,

25

count :S= i=0 0bit count ( S : bit string) := count + 1 count S := S (S - 1 ) {count i s the number o f 1 s in S } procedure

while begin end

power y :=i ao:=1 I n power c y {y:= =y :+=a power aicn +power n an_l Cn -1 + . . . + alc + ao l where the final value of y is the value of the polynomial at x = c. Evaluate 3x 2 + x + 1 at x = 2 by working through each step ofthe algorithm showing the values assigned for := begin

*

end

a)

at each assignment step.

b) Exactly how many multiplications and additions are

n =c

used to evaluate a polynomial of degree at x ? (Do not count additions used to increment the loop variable.) 8. There is a more efficient algorithm (in terms of the num­ ber of multiplications and additions used) for evaluating polynomials than the conventional algorithm described in the previous exercise. It is called Horner's method. This pseudocode shows how to use this method to find the value ' of +...+ + at x + procedure real numbers)

alx ao = c

an xn an _lX n - 1 alX ao = c. Horner(c, ao, aI, a2 , . . . , an : y :=i a:=n 1 n + an-i n -1 + . . . + alc + ao l {y y=:=ancyn 2+c an_lC Evaluate 3x + x + 1 at x = 2 by working through for

to

*

-

anxn an_lXn-1

to

*

/\

Here S I i s the bit string obtained by changing the rightmost 1 bit of S to a 0 and all the 0 bits to the right of this to 1 s. [Recall that S /\ (S - 1 ) is the bitwise AND of S and S - 1 .] b) How many bitwise AND operations are needed to find the number of 1 bits in a string S? 7. The conventional algorithm for evaluating a polynomial + +...+ + at x can be ex­ pressed in pseudocode by

polynomial(c, ao, aI, . . . , an : real

numbers)

a)

each step ofthe algorithm showing the values assigned at each assignment step. b) Exactly how many multiplications and additions are used by this algorithm to evaluate a polynomial of degree at x ? (Do not count additions used to increment the loop variable.) 9. What is the largest for which one can solve in one

n =c n

200

3 / The Fundamentals: Algorithms, the Integers, and Matrices

second a problem using an algorithm that requires fen ) bit operations, where each bit operation i s carried out in 1 0 - 9 second, with these values for f(n)? a) log n b) n c) n log n 1) n ! e) 2 n d) n 2 10. How much time does an algorithm take to solve a problem of size n if this algorithm uses 2n 2 2 n bit operations, each requiring 1 0 - 9 second, with these values of n ? a) 10 b) 20 c ) 50 d) 1 00 1 1 . How much time does an algorithm using 2 50 bit operations need if each bit operation takes these amounts of time? a) 1 0 - 6 second b) 1 0- 9 second c) 1 0 - 1 2 second 12. Determine the least number of comparisons, or best-case performance, a) required to find the maximum of a sequence of n in­ tegers, using Algorithm 1 of Section 3 . 1 . b) used to locate an element in a list of n terms with a linear search. c) used to locate an element in a list of n terms using a binary search. 13. Analyze the average-case performance ofthe linear search algorithm, if exactly half the time element x is not in the list and if x is in the list it is equally likely to be in any position. 14. An algorithm is called optimal for the solution of a prob­ lem with respect to a specified operation if there is no algorithm for solving this problem using fewer operations. a) Show that Algorithm 1 in Section 3 . 1 is an optimal al­ gorithm with respect to the number of comparisons of integers. Comparisons used for bookkeeping in the loop are not of concern here.) b) Is the linear search algorithm optimal with respect to the number of comparisons of integers (not including comparisons used for bookkeeping in the loop)? 1 5. Describe the worst-case time complexity, measured in terms of comparisons, of the ternary search algorithm described in Exercise 27 of Section 3 . 1 . 16. Describe the worst-case time complexity, measured in terms of comparisons, of the search algorithm described in Exercise 28 of Section 3 . 1 . 17. Analyze the worst-case time complexity o f the algorithm you devised in Exercise 29 of Section 3 . 1 for locating a mode in a list of nondecreasing integers. 18. Analyze the worst-case time complexity of the algorithm you devised in Exercise 30 of Section 3 . 1 for locating all modes in a list of nondecreasing integers.

3-34

19. Analyze the worst-case time complexity of the algorithm

20.

+

(Note:

21.

22.

23.

24.

25.

26.

27.

28.

you devised in Exercise 3 1 of Section 3 . 1 for finding the first term of a sequence of integers equal to some previous term. Analyze the worst-case time complexity of the algorithm you devised in Exercise 32 of Section 3 . 1 for finding all terms of a sequence that are greater than the sum of all previous terms. Analyze the worst-case time complexity of the algorithm you devised in Exercise 33 of Section 3 . 1 for finding the first term of a sequence less than the immediately preced­ ing term. Determine the worst-case complexity in terms of com­ parisons of the algorithm from Exercise 5 in Section 3 . 1 for determining all values that occur more than once in a sorted list of integers. Determine the worst-case complexity in terms of compar­ isons of the algorithm from Exercise 9 in Section 3 . 1 for determining whether a string is a palindrome. How many comparisons does the selection sort (see preamble to Exercise 4 1 in Section 3 . 1 ) use to sort n items? Use your answer to give a big-O estimate of the complex­ ity ofthe selection sort in terms of number of comparisons for the selection sort. Find a big-O estimate for the worst-case complexity in terms of number of comparisons used and the number of terms swapped by the binary insertion sort described in the preamble to Exercise 47 in Section 3 . 1 . Show that the greedy algorithm for making change for n cents using quarters, dimes, nickels, and pennies has O(n) complexity measured in terms of comparisons needed. Describe how the number of comparisons used in the worst case changes when these algorithms are used to search for an element of a list when the size of the list doubles from n to 2n, where n is a positive integer. a) linear search b) binary search Describe how the number of comparisons used in the worst case changes when the size of the list to be sorted doubles from n to 2n , where n is a positive integer when these sorting algorithms are used. b) insertion sort a) bubble sort c) selection sort (described in the preamble to Exercise 4 1 in Section 3 . 1 ) d) binary insertion sort (described in the preamble to Exercise 47 in Section 3 . 1 )

3.4 The Integers and Division Introduction The part o f mathematics involving the integers and their properties belongs to the branch of mathematics called number theory. This section is the beginning of a four-section introduction

3.4 The Integers

3-35

and Division

201

to number theory. We will develop the basic concepts of number theory used throughout com­ puter science. In this section we will review some basic concepts of number theory, including divisibility and modular arithmetic. In Section 3.5 we will cover prime numbers and begin our discussion of greatest common divisors. In Section 3 . 6 we will describe several important algo­ rithms from number theory, tying together the material in Sections 3 . 1 and 3 .3 on algorithms and their complexity with the notions introduced in this section and in Section 3 . 5 . For example, we will introduce algorithms for finding the greatest common divisor of two positive integers and for performing computer arithmetic using binary expansions. Finally, in Section 3.7, we will con­ tinue our study ofnumber theory by introducing some important results and their applications to computer arithmetic and cryptology, the study of secret messages. We will rely on proof methods we studied in Chapter 1 to develop basic theorems in number theory. This will give you an oppor­ tunity to use what you have learned about proof methods and proof strategies in a new setting. The ideas that we will develop in this section are based on the notion of divisibility. Divi­ sion of an integer by a positive integer produces a quotient and a remainder. Working with these remainders leads to modular arithmetic, which is used throughout computer science. We will discuss three applications of modular arithmetic in this section: generating pseudorandom num­ bers, assigning computer memory locations to files, and encrypting and decrypting messages.

Division When one integer is divided by a second, nonzero integer, the quotient may or may not be an inte­ ger. For example, 1 2/3 = 4 is an integer, whereas 1 1 /4 = 2.75 is not. This leads to Definition 1 .

DEFINITION 1

If a and b are integers with a #- 0, we say that a divides b if there is an integer c such that = ac. When a divides b we say that a is a factor of b and that b is a multiple of a . The notation a I b denotes that a divides b. We write a 1 b when a does not divide b. b

Remark: We can express a I b using quantifiers as 3c(ac

is the set of integers.

= b), where the universe of discourse

In Figure 1 a number line indicates which integers are divisible by the positive integer d. EXAMPLE 1

Determine whether 3 1 7 and whether 3 I 1 2 .

Solution: It follows that 3 17, because 7/3 is not an integer. On the other hand, 3 I 1 2 because j . 44. From the definition o f the matrix product, devise an algo­ rithm for computing the product of two upper triangular matrices that ignores those products in the computation that are automatically equal to zero. 45. Give a pseudocode description of the algorithm in Exer­ cise 44 for multiplying two upper triangular matrices. 46. How many multiplications of entries are used by the al­ gorithm found in Exercise 44 for multiplying two n x n upper triangular matrices? 47. Show that ifA and B are invertible matrices and AB exists, then (AB) -I = B - 1 A- I . 48. What is the best order to form the product ABCD if A, B, C, and D are matrices with dimensions 30 x 1 0, l Ox 40, 40x 50, and 50 x 30, respectively? Assume that the number of multiplications of entries used to mul­ tiply a p x q matrix and a q x r matrix is pqr. 49. Let A be an n x n matrix and let 0 be the n x n matrix all of whose entries are zero. Show that the following are true. 42.

3-95

Computer Projects a) A 1 , find ab modm.

17. Given a positive integer, find the Cantor expansion of this integer (see the preamble to Exercise 46 of Section 3.6). 18. Given a positive integer n, a modulus m, multiplier a, increment c, and seed xo, with 0 .:s a < m, 0 .:s c < m, and 0 .:s Xo < m, generate the sequence of n pseudo­ random numbers using the linear congruential generator Xn + 1 = (axn + c) modm. 19. Given positive integers a and b, find integers s and t such that sa + tb = gcd(a, b). 20. Given n linear congruences modulo pairwise relatively prime moduli, find the simultaneous solution ofthese con­ gruences modulo the product of these moduli. 2 1 . Given an m x k matrix A and a k x n matrix B, find AB.

Given a square matrix A and a positive integer n, find An . 23. Given a square matrix, determine whether it is symmetric. 24. Given an n l x n 2 matrix A, an n 2 x n 3 matrix B, an n 3 x n 4 matriJi. C, and an n 4 x n 5 matrix D, all with inte­ ger entries, determine the most efficient order to multiply these matrices (in terms of the number of multiplications and additions of integers). 25. Given twom x n Boolean matrices, find their meet and join. 26. Given anm x k Boolean matrix A and a k x n Boolean matrix B, find the Boolean product of A and B. 27. Given a square Boolean matrix A and a positive integer n, find A[n ) . 22.

Computations and Explorations Use a computational program or programs you have written to do these exercises.

1.

We know that nb is O(dn ) when b and d are positive num­ bers with d :::: 2 . Give values ofthe constants C and k such that nb .:s C dn whenever x > k for each of these sets of values: b = 10, d = 2; b = 20, d = 3; b = 1 000, d = 7.

2.

Compute the change for different values of n with coins of different denominations using the greedy al­ gorithm and determine whether the smallest number of coins was used. Can you find conditions so that the

262

3.

4. 5.

6. 7.

3 / The Fundamentals:

Algorithms, the Integers, and Matrices

greedy algorithm is guaranteed to use the fewest coins possible? Using a generator of random orderings of the integers I , 2, . . . , n, find the number of comparisons used by the bubble sort, insertion sort, binary insertion sort, and selection sort to sort these integers. Determine whether 2P - I is prime for each ofthe primes not exceeding 1 00. Test a range of large Mersenne numbers 2P - I to de­ termine whether they are prime. (You may want to use software from the GIMPS project.) Look for polynomials in one variables whose values at long runs of consecutive integers are all primes. Find as many primes of the form n 2 + I where n is a

3-96

8. 9.

10.

11.

positive integer as you can. It is not known whether there are infinitely many such primes. Find 10 different primes each with 100 digits. How many primes are there less than 1 ,000,000, less than 10,000,000, and less than 1 00,000,000? Can you propose an estimate for the number of primes less than x where x is a positive integer? Find a prime factor of each of 10 different 20-digit odd in­ tegers, selected at random. Keep track of how long it takes to find a factor of each ofthese integers. Do the same thing for 1 0 different 30-digit odd integers, 1 0 different 40-digit odd integers, and so on, continuing as long as possible. Find all pseudoprimes to the base 2, that is, composite integers n such that 2n -1 == I (mod n ) , where n does not exceed 10,000.

Writing Projects Respond to these questions with essays using outside sources.

1. 2.

3.

4. 5.

6.

7.

8.

9.

1 0.

Examine the history of the word algorithm and describe the use of this word in early writings. Look up Bachmann's original introduction of big-O no­ tation. Explain how he and others have used this notation. Explain how sorting algorithms can be classified into a taxonomy based on the underlying principle on which they are based. Describe the radix sort algorithm. Describe what is meant by a parallel algorithm. Explain how the pseudocode used in this book can be extended to handle parallel algorithms. Explain how the complexity of parallel algorithms can be measured. Give some examples to illustrate this concept, showing how a parallel algorithm can work more quickly than one that does not operate in parallel. Describe the Lucas-Lehmer test for determining whether a Mersenne number is prime. Discuss the progress of the GIMPS project in finding Mersenne primes using this test. Explain how probabilistic primality tests are used in prac­ tice to produce extremely large numbers that are almost certainly prime. Do such tests have any potential draw­ backs? The question of whether there are infinitely many Carmichael numbers was solved recently after being open for more than 75 years. Describe the ingredients that went into the proof that there are infinitely many such numbers. Summarize the current status of factoring algorithms in terms oftheir complexity and the size of numbers that can currently be factored. When do you think that it will be feasible to factor 200-digit numbers?

11.

12.

13.

14.

15.

16. 17. 18. 19.

Describe the algorithms that are actually used in modern computers to add, subtract, multiply, and divide positive integers. Describe the history of the Chinese Remainder Theorem. Describe some of the relevant problems posed in Chi­ nese and Hindu writings and how the Chinese Remainder Theorem applies to them. When are the numbers of a sequence truly random num­ bers, and not pseudorandom? What shortcomings have been observed in simulations and experiments in which pseudorandom numbers have been used? What are the properties that pseudorandom numbers can have that ran­ dom numbers should not have? Describe how public key cryptography is being applied. Are the ways it is applied secure given the status of fac­ toring algorithms? Will information kept secure using public key cryptography become insecure in the future? Describe how public key cryptography can be used to send signed secret messages so that the recipient is rela­ tively sure the message was sent by the person claiming to have sent it. Show how a congruence can be used to tell the day ofthe week for any given date. Describe some of the algorithms used to efficiently mul­ tiply large integers. Describe some of the algorithms used to efficiently mul­ tiply large matrices. Describe the Rabin public key cryptosystem, explaining how to encrypt and how to decrypt messages and why it is suitable for use as a public key cryptosystem.

C HAPTER

Induction and Recursion 4.1 Mathematical

Induction

4.2 Strong

Induction and Well-Ordering

4.3 Recursive

Definitions and Structural Induction

4.4 Recursive

Algorithms

4.5 Program

Correctness

M any mathematical statements assert that a property is true for all positive integers. 3-

n: n!n ::: nn, n n

n

Examples of such statements are that for every positive integer is divisible by 3; a set with elements has 2n subsets; and the sum of the first positive integers is + 1 )/2. A major goal of this chapter, and the book, is to give the student a thorough understanding of mathematical induction, which is used to prove results of this kind. Proofs using mathematical induction have two parts. First, they show that the statement holds for the positive integer 1 . Second, they show that if the statement holds for a positive integer then it must also hold for the next larger integer. Mathematical induction is based on the rule of inference that tells us that if and � + 1 » are true for the domain of positive integers, then is true. Mathematical induction can be used to prove a tremendous variety of results. Understanding how to read and construct proofs by mathematical induction is a key goal of learning discrete mathematics. In Chapters 2 and 3 we explicitly defined sets and functions. That is, we described sets by listing their elements or by giving some property that characterizes these elements. We gave formulae for the values of functions. There is another important way to define such objects, based on mathematical induction. To define functions, some initial terms are specified, and a rule is given for finding subsequent values from values already known. Sets can be defined by listing some of their elements and giving rules for constructing elements from those already known to be in the set. Such definitions, called are used throughout discrete mathematics and computer science. Once we have defined a set recursively, we can use a proof method called structural induction to prove results about this set. When a procedure is specified for solving a problem, this procedure must solve the problem correctly. Just testing to see that the correct result is obtained for a set of input values does not show that the procedure always works correctly. The correctness of a procedure can be guaranteed only by proving that it always yields the correct result. The final section of this chapter contains an introduction to the techniques of program verification. This is a formal technique to verify that procedures are correct. Program verification serves as the basis for attempts under way to prove in a mechanical fashion that programs are correct.

n(n

V P(n)

P(I) Vk(P(k) P(k

recursive definitions,

4.1

always

Mathematical Induction Introduction Suppose that we have an infinite ladder, as shown in Figure 1 , and we want to know whether we can reach every step on this ladder. We know two things: 1 . We can reach the first rung of the ladder. 2. If we can reach a particular rung of the ladder, then we can reach the next rung. Can we conclude that we can reach every rung? By ( 1 ), we know that we can reach the first rung of the ladder. Moreover, because we can reach the first rung, by (2), we can also reach the second rung; it is the next rung after the first rung. Applying (2) again, because we can reach the second rung, we can also reach the third rung. Continuing in this way, we can show that we can reach the fourth rung, the fifth rung, and so on. For example, after 1 00 uses of (2), we know that we can reach the 1 0 1 st rung. But can we conclude that we are able to reach every rung of this

4- /

263

264

4 / Induction and Recursion

FIGURE 1

4-2

Climbing an Infinite Ladder.

infinite ladder? The answer is yes, something we can verify using an important proof technique called mathematical induction. That is, we can show that P (n) is true for every positive integer n , where P(n) is the statement that we can reach the nth rung of the ladder. Mathematical induction is an extremely important proof technique that can be used to prove assertions of this type. As we will see in this section and in subsequent sections of this chapter and later chapters, mathematical induction is used extensively to prove results about a large variety of discrete objects. For example, it is used to prove results about the complexity of algorithms, the correctness of certain types of computer programs, theorems about graphs and trees, as well as a wide range of identities and inequalities. In this section, we will describe how mathematical induction can be used and why it is a valid proof technique. It is extremely important to note that mathematical induction can be used only to prove results obtained in some other way. It is not a tool for discovering formulae or theorems.

Mathematical Induction

Assessmenl �

In general, mathematical induction* can be used to prove statements that assert that P(n) is true for all positive integers n, where P (n) is a propositional function. A proof by mathe­ matical induction has two parts, a basis step, where we show that P ( l ) is true, and an in­ ductive step, where we show that for all positive integers k, if P(k) is true, then P(k + 1 ) i s true. · Unfortunately. using the terminology "mathematical induction" clashes with the terminology used to describe different types of reasoning. In logic, deductive reasoning uses rules of inference to draw conclusions from premises, whereas inductive reasoning makes conclusions only supported, but not ensured, by evidence. Mathematical proofs, including arguments that use mathematical induction, are deductive, not inductive.

4-3

4. 1 Mathematical Induction

265

PRINCIPLE OF MATHEMATICAL INDUCTION To prove that P en ) is true for all pos­ itive integers n , where P (n ) is a propositional function, we complete two steps: BASIS STEP:

We verify that p e l ) is true.

INDUCTIVE S TEP:

all positive integers k.

We show that the conditional statement P (k)

-+

P (k + 1 ) is true for

To complete the inductive step of a proof using the principle of mathematical induction, we assume that P(k) is true for an arbitrary positive integer k and show that under this assumption, P(k + 1 ) must also be true. The assumption that P(k) is true is called the inductive hypothesis. Once we complete both steps in a proof by mathematical induction, we have shown that P en) is true for all positive integers, that is, we have shown that Yn P (n ) is true where the quantification is over the set of positive integers. In the inductive step, we show that Yk( P(k) -+ P (k + 1 )) is true, where again, the domain is the set of positive integers. Expressed as a rule of inference, this proof technique can be stated as [P(1)

A

Y k(P(k)

-+

P (k + 1 )) ]

-+

Y n P (n ) ,

when the domain is the set of positive integers. Because mathematical induction is such an important technique, it is worthwhile to explain in detail the steps of a proof using this technique. The first thing we do to prove that P en ) is true for all positive integers n is to show that p e l ) is true. This amounts to showing that the particular statement obtained when n is replaced by 1 in Pen ) is true. Then we must show that P (k) -+ P (k + 1 ) is true for every positive integer k. To prove that this conditional statement is true for every positive integer k, we need to show that P(k + 1 ) cannot be false when P (k) is true. This can be accomplished by assuming that P(k) is true and showing that P (k + 1 ) must also be true.

under this hypothesis not ifi! is assumed

Remark: In a proofby mathematical induction it is

assumed that P (k) is true for all positive integers ! It is only shown that that P (k) is true, then P (k + 1 ) is also true. Thus, a proof by mathematical induction is not a case of begging the question, or circular reasoning.

When we use mathematical induction to prove a theorem, we first show that P ( 1 ) is true. Then we know that P (2) is true, because P ( 1 ) implies P (2). Further, we know that P (3) is true, because P (2) implies P (3). Continuing along these lines, we see that P en ) is true for every positive integer n . WAYS TO REMEMBER HOW MATHEMATICAL INDUCTION WORKS Thinking of the infinite ladder and the rules for reaching steps can help you remember how mathematical induction works. Note that statements ( 1 ) and (2) for the infinite ladder are exactly the basis step and inductive step, respectively, of the proof that P en ) is true for all positive integers n , where P(n) is the statement that we can reach the nth rung of the ladder. Consequently, we can invoke mathematical induction to conclude that we can reach every rung. Besides the infinite ladder, several other useful illustrations of mathematical induction can help you remember how this principle works. One ofthese involves a line of people: person one,

unkS �

H I STORICAL NOTE The first known use of mathematical induction is in the work of the sixteenth-century mathematician Francesco Maurolico ( 1 494 - 1 575). Maurolico wrote extensively on the works of classical mathematics and made many contributions to geometry and optics. In his book Arithmeticorum Libri Duo. Maurolico presented a variety of properties of the integers together with proofs of these properties. To prove some of these properties he devised the method of mathematical induction. His first use of mathematical induction in this book was to prove that the sum of the first n odd positive integers equals n2•

266

4 I Induction and Recursion

4-4

,.�""" 14 Pt",,,, /J Ptrsan 12

Person

1 Person

FIGURE 2

Persan l l Per�on / 0 Person 9 Person 8 Person 7 Person 6 Person 5 Person 4

2 Person 3

People Telling Secrets.

person two, and so on. A secret is told to person one, and each person tells the secret to the next person in line, if the former person hears it. Let P en ) be the proposition that person n knows the secret. Then P ( 1 ) is true, because the secret is told to person one; P (2) is true, because person one tells person two the secret; P (3) is true, because person two tells person three the secret; and so on. By the principle of mathematical induction, every person in line learns the secret. This is illustrated in Figure 2. (Of course, it has been assumed that each person relays the secret in an unchanged manner to the next person, which is usually not true in real life.) Another way to illustrate the principle of mathematical induction is to consider an infinite row of dominoes, labeled 1 , 2 , 3 , . . . , n , . . . , where each domino is standing up. Let P en ) be the proposition that domino n is knocked over. If the first domino is knocked over-i.e., if P ( 1 ) i s true-and if, whenever the kth domino i s knocked over, i t also knocks the (k + l )st domino over-i.e., if P (k) -+ P (k + 1 ) is true for all positive integers k-then all the dominoes are knocked over. This is illustrated in Figure 3 .

Examples o f Proofs b y Mathematical Induction Many theorems state that P en ) is true for all positive integers n , where p en) is a propositional function, such as the statement that 1 + 2 + . . . + n = n (n + 1 )/2 for all positive integers n or the statement that n ::::: 2 n for all positive integers n . Mathematical induction is a technique

FIGURE 3

Illustrating How Mathematical Induction Works Using Dominoes.

4-5

4. 1

Mathematical Induction

267

lin P(n),

unks �

for proving theorems of this kind. In other words, mathematical induction can be used to prove statements of the form where the domain is the set of positive integers. Mathematical induction can be used to prove an extremely wide variety of theorems, each of which is a statement of this form. We will use a variety of examples to illustrate how theorems are proved using mathematical induction. The theorems we will prove include summation formulae, inequalities, identities for combinations of sets, divisibility results, theorems about algorithms, and some other creative results. In later sections, we will employ mathematical induction to prove many other types of results, including the correctness of computer programs and algorithms. Mathematical induction can be used to prove a wide variety of theorems, not just summation formulae, inequalities, and other types of examples we illustrate here. Note that there are many opportunities for error in induction proofs. We will describe some incorrect proofs by mathematical induction at the end of this section and in the exercises. PROVING SUMMATION FORMULAE We begin by using mathematical induction to prove several different summation formulae. As we will see, mathematical induction is particularly well suited for proving that such formulae are valid. However, summation formulae can be proven in other ways. This is not surprising because there are often different ways to prove a theorem. The major disadvantage of this use of mathematical induction is that you cannot use it to derive a summation formula. That is, you must already have the formula before you attempt to prove it by mathematical induction. We begin by using mathematical induction to prove a formula for the sum of the smallest positive integers.

EXAMPLE 1

Ex1ra � ExamPles lliiiiiiil

n

n 1 + 2 + · · · + n = n(n 2+ 1) . Solution: P(n) n n(n + 1)/2 . P(n) n = 1, 2, P(I) P(k) P(k + 1) k = 1, 2, P(1) . 1 = 1(1 2+ 1) . P(k) k. 1 + 2 + · · · + k = k(k 2+ 1) . P(k + 1) 1 + 2 + . . . + k + (k + 1) = (k + 1) [(k2+ 1) + 1 ] = (k + 1)(k2 + 2) k+ 1 P(k), 1 + 2 + . . . + k + (k + 1) = k(k 2+ 1) + (k + 1) k(k + 1) + 2(k + 1) 2 = (k + 1)(k2 + 2) Show that if

is a positive integer, then

Let be the proposition that the sum of the first positive integers is We must do two things to prove that is true for 3, . Namely, we must show that is true and that the conditional statement implies is true for 3, . . . . .

BASIS STEP:

IS

.

true, because

For the inductive hypothesis we assume that That is, we assume that

INDUCTIVE S TEP:

positive integer

.

Under this assumption, it must be shown that

is also true. When we add

holds for an arbitrary

is true, namely, that

to both sides of the equation in

we obtain

268

4 / Induction and Recursion

4-6

This last equation shows that P (k + 1) is true under the assumption that P(k) is true. This completes the inductive step. We have completed the basis step and the inductive step, so by mathematical induction we know that P is true for all positive integers That is, we have proven that 1 + 2 + . . . + =

>

BASIS S TEP:

First, note that

a 2 = /3 , a2 = (3 + .J5)/2 3 = 14 , so P(3) and P(4) are true.
a k - I . Because a is a solution of x 2 - x - I = (as the quadratic formula shows), it follows that a 2 = a + 1 . Therefore, a k - I = a 2 a k -3 = (a + 1 )a k -3 = a . a k -3 + 1 . a k -3 = a k - 2 + a k -3 .

3 ::::: :::::

INDUCTIVE S TEP:

4.



By the inductive hypothesis, if k ::: r.

J k- I

>

a k -3 ,

4, it follows that

Therefore, we have r.

J k+ 1

=

r.

Jk

It follows that

+

P(

r.

J k- I

>

a k -2 + a k -3

=

ak- I .

k + I ) is true. This completes the proof.

4, P ( I ) follows from the assumption that P(j) is true for 3 ::::: Hence, the inductive step does not show that P(3) P(4). Therefore, we had to show that P(4) is true separately.

Remark: The inductive step shows that whenever k ::: ::::: j

k+

k.

-+

We can now show that the Euclidean algorithm uses 0 (log b) divisions to find the greatest common divisor of the positive integers a and b, where a ::: b.

THEOREM !

LAME ' S THEOREM

Let

a

and b be positive integers with

a :::

b. Then the

number of divisions used by the Euclidean algorithm to find gcd(a , b) is less than or equal to five times the number of decimal digits in b.

Proof: Recall that when the Euclidean algorithm is applied to find gcd(a , b) with a ::: b, this sequence of equations (where a

=

ro

0 ::::: r2 0 ::::: r3

unkS �

n

and

b


2, where a = ( 1 + "f5)/2. Therefore, it follows that b > a n - I . Furthermore, because 10g 1 0 a '" 0. 2 08 > 1 /5, we see that 10g IO b

>

(fl - 1 ) log 1 0 a

>

(n

..."

1 )/5 .

kk

Hence, n - 1 < 5 . log 1 0 b. Now suppose that b has decimal d�gits. Then b < 1 0k and �d because is an integer, it follows that n :::: 5k. It follows that n - 1 < 10g IO b < b is less than or equal to 5(logl O ,b + 1). Because 5(lOgl (l b + 1 ) is O (log b), we see that o (log b) divisions are used by the Euclidean algorithm to find gcd(a , b) whenever a > b.

Recursively Defined Sets and Structures

Assessmenl �

We have explored how functions can be defined recursively. We now turn our attention to how sets can be defined recursively. Just as in the recursive definition of functions, recursive definitions of sets have two parts, a b�sis step and a recursive step. In the basis step, an initial collection of element!> is specifi�. In the recursive step, rules for forming new elements in the set from those already known. tp b� in the set are provided. Recursive definitions may also include an exclusion rule, which sp�ifies that a recursively defined set contains nothing other than those elements specified in the basis step or generated by applications of the recursive step. In our discussions, we will always tacitly assume that the exclusion rule holds and no element belongs to a recursively defined set unless it is in the initial collection specified in the basis step or can

unks � 1 8 13,

1 8 1 7.

GABRIEL LAME ( 1 795- 1 870) Gabriel Lame entered the Ecole Polytechnique in graduating in He continued his education at the Ecole des Mines, graduating in In Lame went to Russia, where he was appointed director of the Schools of Highways and Trans­ portation in St. Petersburg. Not only did he teach, but he also planned roads and bridges while in Russia. He returned to Paris in where he helped found an engineering firm. However, he soon left the finn , accepting the chair of physics at the Ecole Polytechnique, which he held until While holding this position, he was active outside academia as an engineering consultant, serving as chief engineer of mines and participating in the building of railways. Lame contributed original work to number theory, applied mathematics, and thennodynamics. His best­ known work involves the introduction of curvilinear coordinates. His work on number theory includes proving Fennat's Last Theorem for n = as well as providing the upper bound for the number of divisions used by the Euclidean algorithm given in this text. In the opinion of Gauss, one of the most important mathematicians of all time, Lame was the foremost French mathematician of his time. However, French mathematicians considered him too practical, whereas French scientists considered him too theoretical.

1 820.

1820

1 832,

7,

1 844.

4-38

300 4 / Induction and Recursion

be generated using the recursive step one or more times. Later we will see how we can use a technique known as structural induction to prove results about recursively defined sets. Examples 7, 8, 1 0, and 1 1 illustrate the recursive definition of sets. In each example, we show those elements generated by the first few applications of the recursive step. EXAMPL E 7

Consider the subset S of the set of integers defined by BASIS S TEP: 3 E S . RECURSIVE S TEP:

Extra � Examples �

If X E S and y E S, then x + y E S .

3

12

The new elements found to be in S are by the basis step, 3 + 3 = 6 at the first application of the recursive step, 3 + 6 = 6 + 3 = 9 and 6 + 6 = at the second application of the recursive � step, and so on. We will show later that S is the set of all positive multiples of 3 .

2.124

Recursive definitions play an important role in the study of strings. (See Chapter for an introduction to the theory of formal languages, for example.) Recall from Section that a string over an alphabet � is a finite sequence of symbols from � . We can define �* , the set of strings over � , recursively, as Definition shows.

2

DEFINITION 2

The set � * o f strings over the alphabet � can be defined recursively by BASIS S TEP:

A

E

� * (where A is the empty string containing no symbols).

RECURSIVE STEP: If w

E � * and x E � , then wx E � * .

The basis step of the recursive definition of strings says that the empty string belongs to � * . The recursive step states that new strings are produced by adding a symbol from � to the end of strings in � * . At each application of the recursive step, strings containing one additional symbol are generated. EXAMPLE 8

A,

1

If � = {O, I }, the strings found to be in � * , the set of all bit strings, are specified to be in � * in the basis step, ° and formed during the first application of the recursive step, 00, 0 1 , 1 0, � and 1 1 formed during the second application of the recursive step, and so on. Recursive definitions can be used to define operations or functions on the elements of recursively defined sets. This is illustrated in Definition 3 of the concatenation of two strings and Example 9 concerning the length of a string.

DEFINITION 3

Two strings can be combined via the operation of concatenation. Let � be a set of symbols . and � * the set of strings formed from symbols in � . We can define the concatenation oftwo strings, denoted by . , recursively as follows. BASIS STEP:

lf w E � * , then w . A

RECURSIVE STEP:

If w \

E

=

w, where A is the empty string

� * and W2

E

.

� * and x E � , then W I · (W2X)

=

(WI · W2)x .

The concatenation of the strings W I and W2 is often written as W I W2 rather than W I W2 . By repeated application of the recursive definition, it follows that the concatenation of two strings •

4-39

4.3

abra

WI

cadabra

and W2 consists of the symbols in concatenation of W I = and W2 = EXAMPLE 9

Length of a String

Recursive Definitions and Structural Induction 301

WI

E :E*

=

l

W2 .

For instance, the

Give a recursive definition of (w), the length of the string w .

Solution: The length o f a string can b e defined by leA) 0; l(wx) lew) + 1 if w and x =

abracadabra.

followed by the symbols in is W I W2 =

E :E .

Another important use of recursive definitions is to define well-formed formulae of various types. This is illustrated in Examples 1 0 and 1 1 . EXAMPLE 10

Well-Formed Formulae for Compound Statement Forms We can define the set of well­ formed formulae for compound statement forms involving T, F, propositional variables, and operators from the set {-., /\, v , -+ , *+ } . BASIS S TEP:

T, F, and s , where s i s a propositional variable, are well-formed formulae.

If E and F are well-formed formulae, then (-.E), (E /\ F), (E F), and (E *+ F) are well-formed formulae.

RECURSIVE S TEP:

(E

-+

v

F),

For example, by the basis step we know that T, F, p, and q are well-formed formulae, where p and q are propositional variables. From an initial application of the recursive step, we know that (p v q), (p -+ F), (F -+ q), and (q /\ F) are well-formed formulae. A second application of the recursive step shows that « p v q) -+ (q /\ F)), (q v (p v q )), and « p -+ F) -+ T) are well-formed formulae. We leave it to the reader to show that P -' /\ q , pq/\, and -. /\ pq are well-formed formulae, by showing that none can be obtained using the basis step and one or .... more applications of the recursive step.

not

EXAMPLE 11

We can define the set of well-formed formulae consisting of variables, numerals, and operators from the set { + , - , *, /, t } (where * denotes multiplication and t denotes exponentiation) recursively.

Well-Formed Formulae of Operators and Operands

BASIS S TEP:

x is a well-formed formula if x is a numeral or variable.

If F and G are well-formed formulae, then (F + (F / G), and (F t G) are well-formed formulae.

RECURSIVE S TEP:

G), (F - G), (F * G),

For example, by the basis step we see that x, y, 0, and 3 are well-formed formulae (as is any variable or numeral). Well-formed formulae generated by applying the recursive step once include (x + 3), (3 + y), (x - y), (3 - 0), (x * 3), (3 * y), (3/0), (x /y), (3 t x), and (O t 3). Applying the recursive step twice shows that formulae such as « x + 3) + 3) and (x - (3 * y)) are well-formed formulae. [Note that (3/0) is a well-formed formula because we are concerned only with syntax matters here.] We leave it to the reader to show that each of the formulae x3 +, y * + x , and * x /y is a well-formed formula by showing that none of them can be .... obtained using the basis step and one or more applications of the recursive step.

not

We will study trees extensively in Chapter 1 0. A tree is a special type of a graph; a graph is made up of vertices and edges connecting some pairs of vertices. We will study graphs in Chapter 9. We will briefly introduce them here to illustrate how they can be defined recursively.

4 / Induction and Recursion

302

DEFINITION 4

4-40

The set of rooted trees, where a rooted tree consists of a set of vertices containing a distin­

guished vertex called the root, and edges connecting these vertices, can be defined recursively by these steps: BASIS S TEP: A

single vertex r is a rooted tree.

REC URSIVE S TEP: Suppose that T1 , T2 , . . . , Tn are disjoint rooted trees with roots rl , r2 , . . . , rn , respectively. Then the graph formed by starting with a root r , which is not in any of the rooted trees T1 , T2 , . . , Tn , and adding an edge from r to each of the vertices rl , r2 , . . , rn , is also a rooted tree . . .

.

2

In Figure we illustrate some ofthe rooted trees formed starting with the basis step and applying the recursive step one time and two times. Note that infinitely many rooted trees are formed at each application of the recursive definition. Binary trees are a special type of rooted trees. We will provide recursive definitions of two types of binary trees-full binary trees and extended binary trees. In the recursive step of the definition of each type of binary tree, two binary trees are combined to form a new tree with one of these trees designated the left subtree and the other the right subtree. In extended binary trees, the left subtree or the right subtree can be empty, but in full binary trees this is not possible. Binary trees an� one of the most important types of structures in computer science. In Chapter l O we will see how they can be used in searching and sorting algorithms, in algorithms for compressing data, and in many other applications. We first define extended binary trees.

D EFINITION 5

The set of extended binary trees can be defined recursively by these steps: BASIS S TEP:

The empty set is an extended binary tree.

RECURSIVE STEP: If Tl and T2 are disjoint extended binary trees, there is an extended binary tree, denoted by Tl . T2 , consisting of a root r together with edges connecting the root to each of the roots of the left subtree Tl and the right subtree T2 when these trees are nonempty.

Basis step

Step

I

Step

2

FIGURE 2



Building Up Rooted Trees.

4.3 Recursive Definitions and Structural Induction

4-41

303

Basis step

Step

I



Step

2

/\ 1 \

Step

3

FIGURE 3

Building Up Extended Binary Trees.

3

Figure shows how extended binary trees are built up by applying the recursive step from one to three times. We now show how to define the set of full binary trees. Note that the difference between this recursive definition and that of extended binary trees lies entirely in the basis step.

DEFINITION 6

The set of full binary trees can be defined recursively by these steps: BASIS STEP: There is a full binary tree consisting only of a single vertex

r

r.

RECURSIVE STEP: I f TJ and T2 are disjoint full binary trees, there i s a full binary tree, denoted by TJ T2, consisting of a root together with edges connecting the root to each of the roots of the left subtree TJ and the right subtree T2 . •

Figure times.

4 shows how full binary trees are built up by applying the recursive step one and two

Structural Induction To prove results about recursively defined sets we generally use some form of mathematical induction. Example 1 2 illustrates the connection between recursively defined sets and mathe­ matical induction. EXAMPLE 12

3

Show that the set S defined in Example 7 by specifying that E S and that if X E S and y E S, then x + Y E S, is the set of all positive integers that are multiples of

3.

4 / Induction and Recursion

304

Basis step

Step

1

Step

2

FIGURE 4

4-42

_

Building Up Full Binary Trees.

Solution: Let be the set of all positive integers divisible by 3 . To prove that we" must show that is a subset of and that is a subset of To prove that is a subset of we must show that every positive integer divisible by 3 is in We will use mathematical induction to prove this. Let Pen) be the statement that 3n belongs to The basis step holds because by the first part of the recursive definition of 3 . 1 3 is in To establish the inductive step, assume that P(k) is true, namely, that 3k is in Because 3k is in and because 3 is in it follows from the second part of the recursive definition of that 3k 3 3(k 1 ) is also in To prove that is a subset of we use the recursive definition of First, the basis step of the definition specifies that 3 is in Because 3 3 . 1 , all elements specified to be in in this step are divisible by 3 and are therefore in To finish the proof, we must show that all integers in generated using the second part of the recursive definition are in This consists of showing that x y is in whenever x and y are elements of also assumed to be in Now if x and y are both in it follows that 3 I x and 3 I y. By part (i) of Theorem 1 of Section it follows that 3 I x y, completing the proof. A

A

S

A

S

S,

S

S

S.

A.

A

=

S,

S,

S.

S. S.

=

S

A,

S.

A.

S

+

=

=

S,

+

S.

S.

S

A.

+

S

A

A, +

A.

3 .4,

x

N by ao,O

=

0 and

0

m + n (n + )/ 2 for all (m , n) E N

x

N, that is, for all pairs of nonnegative

1

So/ution: We can prove that am , n = m + n (n + )/ 2 using a generalized version ofmathematical induction. The basis step requires that we show that this formula is valid when (m , n) = (0, 0) . The induction step requires that we show that if the formula holds for all pairs smaller than (m , n) in the lexicographic ordering of N x N, then it also holds for (m , n). BASIS S TEP: Let (m , n) = (0, 0 ) . Then by the basis case of the recursive definition of am , n we have ao,O = Furthermore, when m = n = 0, m + n (n + )/ 2 = 0 + (0 . )/ 2 = This completes the basis step.

O.

1

1

1

O.

INDUCTIVE S TEP: Suppose that am ', n ' = m' + n'(n' + )/ 2 whenever (m ', n') is less than (m , n) in the lexicographic ordering ofN x N. By the recursive definition, if n = 0, then am , n = am- I , n + Because (m 1 , n) is smaller than (m , n ), the inductive hypothesis tells us that am- I , n = m + n (n + )/ 2 , so that am , n = m + n (n + 1 )/ 2 + 1 = m + n (n + )/ 2 ,

1. 1 -

1

-

-

1

1

4 / Induction and Recursion

308

4-46

giving us the desired equality. Now suppose that n > 0, so am , n = am , n - l + n. Because (m , n - 1 ) is smallerthan (m , n), the inductive hypothesis tells us that am , n - l = m + (n - 1 )n/2, so am , n = m + (n - 1 )n /2 + n = m + (n 2 - n + 2n)/2 = m + n(n + 1 )/2. This finishes the 0 am ' n = aam- l ' nl + m,n- + l 1'f n > 0 , then am, n = m + n for all (m , n) E N X N . 46.

Use generalized induction as was done in Example 15 to show that if am , n is defined recursively by al , l = 5 and

{

2 if n = l and m > 1 am ' n = aam-1 ' nl + m,n- + 2 1'f n > 1 , then am,n = 2(m + n) + 1 for all (m , n) E Z+ X Z+ .

*47.

4-48

4 / Induction and Recursion

A partition of a positive integer n is a way to write n as a sum of positive integers where the order of terms in the sum does not matter. For instance, 7 = 3 + 2 + 1 + 1 is a partition of7. Let Pm equal the number of different par­ titions of m , and let Pm , n be the number of different ways to express m as the sum of positive integers not exceed­ ing n . a) Show that Pm,m = Pm . b) Show that the following recursive definition for Pm , n is correct: 1 if m = 1 1 if n = 1 if m < n Pm,n = Pm,m 1 + Pm,m- l if m = n > 1 Pm,n- l + Pm-n,n if m > n > 1 .

1

c)

Find the number of partitions of 5 and of 6 using this recursive definition.

Consider an inductive definition of a version of Ackermann's

� function. This function was named after Wilhelm Ackermann,

..... a German mathematician who was a student ofthe great math­ ematician David Hilbert. Ackermann's function plays an im­ portant role in the theory of recursive functions and in the

study of the complexity of certain algorithms involving set unions. (There are several different variants of this function. All are called Ackermann's function and have similar proper­ ties even though their values do not always agree.) 2n if m = 0 if m > 1 and n = 0 o if m :::: 1 and n = 1 A(m , n) = 2 A(m - 1 , A(m , n - 1» if m :::: l and n :::: 2

1

Exercises 48-55 involve this version ofAckermann's function. 48. Find these values of Ackermann's function. a) A(I , O) b) A(O, 1) c) A( I , I ) d ) A(2, 2) 49. Show that A(m , 2) = 4 whenever m :::: 1 . SO. Show that A ( 1 , n ) = 2n whenever n :::: 1 . 51. Find these values of Ackermann's function. a) A(2, 3) *b) A(3 , 3) *52. Find A(3 , 4). * *53. Prove that A(m , n + 1) > A(m , n) whenever m and n are nonnegative integers. *54. Prove that A(m + 1 , n) :::: A(m , n) whenever m and n are nonnegative integers. 55. Prove that A(i , j) :::: j whenever i and j are nonnegative integers. 56. Use mathematical induction to prove that a function F de­ fined by specifying F(O) and a rule for obtaining F(n + 1) from F(n) is well defined. 57. Use strong induction to prove that a function F defined by specifying F(O) and a rule for obtaining F(n + 1) from the values F(k) for k = 0, 1 , 2, . . . , n i s well defined. 58. Show that each of these proposed recursive definitions of a function on the set of positive integers does not produce a well-defined function. a) F(n) = 1 + F ( ln/2J ) for n :::: 1 and F(I) = 1 . b) F(n) = 1 + F(n - 3 ) for n :::: 2, F(I) = 2, and F(2) = 3 . c ) F(n) = 1 + F(n/2) for n :::: 2, F(I) = I, and F(2) = 2 . d) F(n) = 1 + F(n/2) if n is even and n :::: 2, F(n) = 1 - F(n - 1 ) if n is odd, and F(I) = 1 . e) F(n) = 1 + F(n/2) if n is even and n :::: 2 , F(n) = F(3n - 1 ) if n is odd and n :::: 3, and F(I) = 1 . 59. Show that each of these proposed recursive definitions of a function on the set of positive integers does not produce a well-defined function. a) F(n) = 1 + F( l(n + 1)/2J ) for n :::: 1 and F(I) = 1 . b) F(n) = 1 + F(n - 2) for n :::: 2 and F(I) = O. c) F(n) = 1 + F(n/3) for n :::: 3 , F(I) = I, F(2) = 2, and F(3) = 3. d ) F(n) = 1 + F(n/2) if n is even and n :::: 2, F(n) = 1 + F(n - 2) if n is odd, and F(I) = 1 . e) F(n) = 1 + F(F(n - 1 » if n :::: 2 and F(I) = 2.

4.4 Recursive Algorithms

4-49

Exercises 60-62 deal with iterations ofthe logarithm function. Let log n denote the logarithm of n to the base 2, as usual. The function log(k) n is defined recursively by Iog(k) n

_

-

I

n if k = 0 log(log(k - l ) n) if log(k - l ) n is defined and positive undefined otherwise.

The iterated logarithm is the function log* n whose value at n is the smallest nonnegative integer k such that log(k) n ::: I . 60. Find each of these values: a) log(2 ) 1 6 b) 10g(3 ) 256 c) log(3 ) 2 655 3 6 65536 d) log(4) 22 . 61. Find the value of log* n for each of these values of n : a) 2 b) 4 c) 8 d) 1 6 f) 65536 e) 256 g) 22048 62. Find the largest integer n such that log* n = 5. Detennine the number of decimal digits in this number.

4.4

311

Exercises 63-65 deal with values of iterated functions. Sup­ pose that f(n) is a function from the set of real numbers, or positive real numbers, or some other set of real numbers, to the set of real numbers such that f(n ) is monotonically increasing [that is, f(n) < f(m) when n < m) and f(n) < n for all n in the domain of f.] The function f(k) (n) is defined recursively by n if k = 0 f(k) (n) = f(f(k - l ) (n» if k > O. Furthennore, let c be a positive real number. The iterated function f; is the number of iterations of f required to reduce its argument to c or less, so f;(n) is the smallest nonnegative integer k such that fk (n) ::: c . 63. Let f(n) = n a , where a is a positive integer. Find a fonnula for f(k) (n). What is the value of fo(n) when n is a positive integer? k 64. Let f(n) = nj2. Find a fonnula for f( ) (n). What is the value of fj(n) when n is a positive integer? 65. Let f(n) = ,.;n. Find a fonnula for j a, can be reduced to finding the greatest common divisor of a pair of smaller integers, namely, b mod a and a, because gcd(b mod a , a) = gcd(a , b). When such a reduction can be done, the solution to the original problem can be found with a sequence of reductions, until the problem has been reduced to some initial case for which the solution is known. For instance, for finding the greatest common divisor, the reduction continues until the smaller of the two numbers is zero, because gcd(a , 0) = a when a > o. We will see that algorithms that successively reduce a problem to the same problem with smaller input are used to solve a wide variety of problems.

DEFINITION 1

unks � EXAMPLE 1

Extra � Examples lliiiiiit

recursive

An algorithm is called if it solves a problem by reducing it to same problem with smaller input.

an instance of the

We will describe a variety of different recursive algorithms in this section. Give a recursive algorithm for computing n !, where n is a nonnegative integer.

Solution: We can build a recursive algorithm that finds n ! , where n is a nonnegative integer, based on the recursive definition ofn ! , which specifies that n ! n . (n - I ) ! when n is a positive integer, and that o ! 1 . To find n ! for a particular integer, we use the recursive n times, each time replacing a value of the factorial function with the value of the factorial function at =

=

step

312

41

4-50

Induction and Recursion

1.

the next smaller integer. At this last step, we insert the value of O ! . The recursive algorithm we obtain is displayed as Algorithm To help understand how this algorithm works, we trace the steps used by the algorithm to compute 4 ! . First, we first use the recursive step to write 4 ! = 4 · 3 ! . We then use the recursive step repeatedly to write 3 ! = 3 . 2 ! , 2 ! = 2 . I ! , and I ! = . O ! . Inserting the value of O! = and working back through the steps, we see that I ! = = 2! = 2 . I ! = 2 , 3 ! = 3 . 2! = .... 3 · 2 = 6, and 4! = 4 · 3 ! = 4 · 6 = 24.

1 . 11 1,

1,

ALGORITHM 1 A Recursive Algorithm for Computing n!.

procedure factorial( n: nonnegative integer) if n = 0 thenfactorial(n) := 1

elsefactorial(n) := n ·factorial(n

-

1)

Example 2 shows how a recursive algorithm can be constructed to evaluate a function from its recursive definition. EXAMPLE 2

Give a recursive algorithm for computing a n , where a is a nonzero real number and nonnegative integer.

n is a

Solution: We can base a recursive algorithm on the recursive definition of states that for n 0 and the initial condition 1. To find an+ 1

=

a . an

aO

>

a n . This definition a n , successively

=

use the recursive step to reduce the exponent until it becomes zero. We give this procedure in .... Algorithm 2.

ALGORITHM 2 A Recursive Algorithm for Computing a n .

procedure power(a : nonzero real number, if n = 0 then power(a , n) : = I

else power(a , n) := a · power(a, n

EXAMPLE 3

-

n:

nonnegative integer)

I)

Devise a recursive algorithm for computing bn mod m , where b, � and I ::: b < m .

n 0, Solution: We can base a recursive algorithm on the fact that m � 2,

bn mod m

=

(b .(bn - 1 mod m )) mod m ,

n, and

m are integers with

1.

which follows by Corollary 2 in Section 3 .4, and the initial condition b O mod m = We leave this as Exercise 1 2 for the reader at the end of the section. However, we can devise a much more efficient recursive algorithm based on the observation that b n mod m

=

(b n l2 mod m i mod m

=

« b Ln / 2 J mod m i mod m · b mod m ) mod m

n is even and when n is odd, which we describe in pseudocode as Algorithm 3. when

b n mod m

4.4 We use a trace of Algorithm 3 with input b 2, n 5, and m 3 to illustrate how it works. First, because n 5 is odd we use the "else" clause to see that mpower(2, 5, 3) (mpower(2, 2, 3 f mpower(2, 3 . 2 3) 3 . We next use the "else if" clause to see that mpower(2, 2, 3) 1 , 3)2 3 . Using the "else" clause again, we see that 3 ·2 3) 3 . Finally, using the "if" clause, mpower(2, 1 , 3) (mpower(2, 0, 3)2 we see that mpower(2, 0, 3) 1 . Working backwards, it follows that mpower(2, 1 , 3) (12 3 . 2 3) 3 2, so mpower(2, 2, 3) 22 3 1 , and finally 2 mpower(2, 5 , 3) ( 1 3 · 2 3) 3 2.

4-5 1

Recursive Algorithms

=

=

mod

=

mod

mod =

mod

=

=

mod mod

=

mod mod mod mod

mod

=

=

mod

=

=

mod

=

313

mod

=

=



ALG ORITHM 3 Recursive Modular Exponentiation.

procedure mpower(b, n , m : integers with m :=:: 2, n :=:: 0) if n = 0 then mpower(b, n , m) = 1 else if n is even then mpower(b, n , m) = mpower(b, n 12 , m)2 mod m else mpower(b, n , m) = (mpower(b, Ln I2J , m)2 mod m . {mpower(b, n , m) = bn mod m }

b mod m) mod m

Next we give a recursive algorithm for finding greatest common divisors. EXAMPLE 4

Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b.

Solution:

We can base a recursive algorithm on the reduction gcd(a , b) = gcd(b mod a , a) and the condition gcd(O, b) = b when b > 0. This produces the procedure in Algorithm 4, which is a recursive version of the Euclidean algorithm. We illustrate the workings of Algorithm 4 with a trace when the input is a = 5, b = 8. With this input, the algorithm uses the "else" clause to find that gcd(5, 8) = gcd(8 mod 5 , 5) = gcd(3 , 5). It uses this clause again to find that gcd(3 , 5) = gcd(5 mod 3 , 3) = gcd(2, 3), then to get gcd(2, 3) = gcd(3 mod 2, 2) = gcd( 1 , 2), then to get gcd( 1 , 2) = gcd(2 mod 1 , 1 ) = gcd(O, 1 ). Finally, to find gcd(O, 1 ) it uses the first step with a = ° to find that gcd(O, 1 ) = 1 . � Consequently, the algorithm finds that gcd(5 , 8) = 1 .

ALGORITHM 4 A Recursive Algorithm for Computing

procedure gcd(a, b: nonnegative integers with a if a = 0 then gcd(a, b) := b else gcd(a , b) := gcd(b mod a, a)


am and j > m) then binary search(x , m + 1 , j) else location : = 0 procedure

m

:=

-

4.4

4-53

315

Recursive Algorithms

Proving Recursive Algorithms Correct Mathematical induction, and its variant strong induction, can be used to prove that a recursive algorithm is correct, that is, that it produces the desired output for all possible input values. Examples 7 and 8 illustrate how mathematical induction or strong induction can be used to prove that recursive algorithms are correct. First, we will show that Algorithm is correct.

2

EXAMPLE 7

2, which computes powers of real numbers, is correct. Solution: We use mathematical induction on the exponent n. If n 0 , the first step of the algorithm tells us that power (a , 0) correct because a O 1 for every nonzero real number a. This completes the basis step.1 . This is The inductive hypothesis is the statement that power (a , k) a k for all a =I 0 for the nonnegative integer k. That is, the inductive hypothesis is the statement that the algorithm correctly computes a k . To complete the inductive step, we show that ifthe inductive hypothesis is true, then the algorithm correctly computes a k+ l . Because k + 1 is a positive integer, when the algorithm computes a k+ l , the algorithm sets power (a, k 1) a · power (a, k). By the inductive hypothesis, we have power (a, k) a k , so power (a, k 1) a . power (a, k) a . a k a k + l . This completes the inductive step. We have completed the basis step and the inductive step, so we can conclude that Algorithm .... 2 always computes a n correctly when a =I 0 and n is a nonnegative integer. Prove that Algorithm

=

BASIS STEP:

=

=

=

INDUCTIVE S TEP:

=

+

=

=

+

=

=

Generally, we need to use strong induction to prove that recursive algorithms are correct, rather thanjust mathematical induction. Example 8 illustrates this; it shows how strong induction can be used to prove that Algorithm is correct.

3

EXAMPLE S

Extra � ExamPles llliiii-'

3, which computes modular powers, is correct. Solution: We use strong induction on the exponent n. Let b be an integer and m an integer with m 2 . When n 0, the algorithm sets mpower( b, n, m) equal to 1 . This is correct because bO m 1 . The basis step is complete. For the inductive hypothesis we assume that mpower(b, j, m) bj m for all integers 0 j k whenever b is a positive integer and m is an integer with m 2. To complete the inductive step, we show that if the inductive hypothesis is correct, then mpower(b, k, m) bk Because the recursive algorithm handles odd and even values of k differently, we split the inductive step into two cases. When k is even, we have mpower(b, k, m) mpower(b, kj2, m )2 m (bk/2 m)2 m bk m, 2 k where we have used the inductive hypothesis to replace mpower(b, kj2, m ) by b / m. When k is odd, we have mpower(b, k, m) «mpower(b, Lkj2j, m))2 m ·b m) m Prove that Algorithm

:::

BASIS S TEP:

mod

=

=

=

IND UCTIVE S TEP:

:::::

=


Consequently, this algorithm requires far less computation than does the recursive algorithm. We have shown that a recursive algorithm may require far more computation than an iterative one when a recursively defined function is evaluated. It is sometimes preferable to use a recursive procedure even if it is less efficient than the iterative procedure. In particular, this is true when the recursive approach is easily implemented and the iterative approach is not. (Also, machines designed to handle recursion may be available that eliminate the advantage of using iteration.)

n 1 -

IIIn I -

II I n In

n 1

n 1.

-

The Merge Sort

UIlIs E:J EXAMPLE 9

We now describe a recursive sorting algorithm called the merge sort algorithm. We will demonstrate how the merge sort algorithm works with an example before describing it in generality. Sort the list 8,

2, 4, 6, 9, 7, l O, 1, 5, 3 using the merge sort.

Solution: A merge sort begins by splitting the list into individual elements by successively splitting lists in two. The progression of sublists for this example is represented with the balanced binary tree of height 4 shown in the upper half of Figure 2 . Sorting is done by successively merging pairs of lists. At the first stage, pairs of individual elements are merged into lists of length two in increasing order. Then successive merges of pairs of lists are performed until the entire list is put into increasing order. The succession of merged lists in increasing order is represented by the balanced binary tree of height shown .... in the lower half of Figure (note that this tree is displayed "upside down").

2

4

In general, a merge sort proceeds by iteratively splitting lists into two sublists of equal length (or where one sublist has one more element than the other) until each sublist contains one

318

4 / Induction and Recursion

4-56

8 2 4 6 9

7

10 1 5 3

/ �

8 2 4 6 9

7

10 1 5 3

/ �5 3 / �6 9 10 1 / \4 6/ \9 10/ \ 5/ \3 8 2 / \10 / \2 8 8 2 4

7

7

7

8

2

10

7

\2 8/ 4 6 9 \ 10/ 5 3 \2 4 /8 \6 9/ \ /10 \3 5/ / / � � 1 3 5 10 2 4 6 8 9 � / 1 2 3 4 5 6 8 9 10 7

7

7

7

FIGURE 2

DeDlO�

The Merge Sort of 8, 2, 4, 6, 9, 7, 10, 1, 5, 3.

element. This succession of sublists can be represented by a balanced binary tree. The procedure continues by successively merging pairs of lists, where both lists are in increasing order, into a larger list with elements in increasing order, until the original list is put into increasing order. The succession of merged lists can be represented by a balanced binary tree. We can also describe the merge sort recursively. To do a merge sort, we split a list into two sublists of equal, or approximately equal, size, sorting each sublist using the merge sort algorithm, and then merging the two lists. The recursive version of the merge sort is given in Algorithm This algorithm uses the subroutine merge, which is described in Algorithm 1 0.

9.

ALGORITHM 9 A Recursive Merge Sort.

procedure mergesort(L if n > I then

= a\ , . . . , an }

m := Ln/2J L \ := a \ , a2 , . . . , am L 2 := am + \ , am+2 , · · · , an L := merge(mergesort(L \ }, mergesort(L 2 }}

{L is now sorted into elements in nondecreasing order}

4.4 Recursive Algorithms

4-5 7

319

TABLE 1 Merging the Two Sorted Lists 2, 3, 5, 6 and 1 , 4. First List

Second List

2356 2356 356 56 56

14 4 4 4

Merged List

1 12 123 1234 123456

Comparison

1 1 , and n is a positive integer, then n can be expressed uniquely in the form n = ak bk + ak _ I bk - 1 + . . . + al b + ao. A lattice point in the plane is a point (x , y) where both x and y are integers. Use mathematical induction to show that at least n + 1 straight lines are needed to ensure that every lattice point (x , y) with x :::: 0, y :::: 0, and x + y :'S n lies on one of these lines. (Requires calculus) Use mathematical induction and the product rule to show that if n is a positive integer and

Supplementary Exercises

4-69

f, (x), fz (x), . . . , fn (x), are all differentiable functions,

then

31.

32.

33.

*34.

(f, (x)fz (x) · · · fn (x))' f, (x) fz (x) · · · fn (x) f{ (x) f� (x) . . . f� (x) + + + f, (x) fz (x) fn (x) · (Requires material in Section 3. 7) Suppose that B MAM- ' , where A and B are n x n matrices and M is

=

=

=

invertible. Show that Bk MAk M- ' for all positive in­ tegers k. Use mathematical induction to show that if you draw lines in the plane you only need two colors to color the regions formed so that no two regions that have an edge in com­ mon have a common color. Show that n ! can be represented as the sum of n of its distinct positive divisors whenever n � 3 . [Hint: Use in­ ductive loading. First try to prove this result using mathe­ matical induction. By examining where your proof fails, find a stronger statement that you can easily prove using mathematical induction.] Use mathematical induction to prove that ifx" X2 , , Xn are positive real numbers with n � 2, then • • •

33 1

o < p /q < I as the sum of distinct unit fractions. At each step of the algorithm, we find the smallest positive integer n such that 1 / n can be added to the sum without exceed­ ing p /q . For example, to express 517 we first start the sum with 1 /2. Because 5/7 - 1 /2 3 / 1 4 we add 1/5 to the sum because 5 is the smallest positive integer k such that 1 / k < 3 / 1 4. Because 3 / 1 4 - 1 /5 1 /70, the a1go­ rithm terminates, showing that 5j7 1 /2 + 1 /5 + 1 170. Let T (p) be the statement that this algorithm terminates for all rational numbers p /q with 0 < p /q < 1 . We will prove that the algorithm always terminates by showing that T(p) holds for all positive integers p. a) Show that the basis step T ( l ) holds. b) Suppose that T(k) holds for positive integers k with k < p. That is, assume that the algorithm terminates for all rational numbers k/ r, where 1 � k < p. Show that if we start with p /q and the fraction 1 / n is se­ lected in the first step of the algorithm, then p /q p' /q ' + l / n , where p' np - q and q' nq . After considering the case where p/q l / n , use the in­ ductive hypothesis to show that the greedy algorithm terminates when it begins with p' / q' and complete the inductive step. The McCarthy 91 functioQ. (defined by John McCarthy, one of the founders of artificial intelligence) is defined using the rule n - 10 if n > 1 00 M(n ) M(M (n + 1 1)) if n � 1 00

= ==

=

=

= =

={

35.

*36.

37.

*38.

39.

40.

Use mathematical induction to prove that ifn people stand in a line, where n is a positive integer, and ifthe first per­ son in the line is a woman and the last person in line is a man, then somewhere in the line there is a woman directly in front of a man. Suppose that for every pair of cities in a country there is a direct one-way road connecting them in one direction or the other. Use mathematical induction to show that there is a city that can be reached from every other city either directly or via exactly one other city. Use mathematical induction to show that when n circles divide the plane into regions, these regions can be col­ ored with two different colors such that no regions with a common boundary are colored the same. Suppose that among a group of cars on a circular track there is enough fuel for one car to complete a lap. Use mathematical induction to show that there is a car in the group that can complete a lap by obtaining gas from other cars as it travels around the track. Show that if n is a positive integer, then

'tt

(2j - 1 )

(j;; ) = n(n + 1/k

1 )/2.

A unit or Egyptian fraction is a fraction of the form l/n, where n is a positive integer. In this exercise, we will use strong induction to show that a greedy algorithm can be used to express every rational number p /q with

for all positive integers n . 41. By successively using the defining rule for M(n), find a) M(102). b) M( 1 0 1 ). c) M(99). d) M(97). e) M(87). t) M(76). * >il:t2. Show that the function M(n) is a well-defined function from the set of positive integers to the set of positive inte­ gers. [Hint: Prove that M(n ) 9 1 for all positive integers n with n � 1 0 1 .] 43. Is this proofthat 3 1 1 1 - +-+...+ (n - l )n 2 n 1 ·2 2·3 whenever n is a positive integer, correct? Justify your answer. Basis step: The result is true when n 1 because 1 3 1 1 ·2 2 Inductive step : Assume that the result is true for n . Then 1 1 1 1 + + -+...+ 1 ·2 2·3 (n - 1 )n n(n + 1) 1_ � + n n+1 2 n 1 3 2 n+1 Hence, the result is true for n + 1 if it is true for n . This completes the proof.

=

-

=

---

= .!. (.!. = _

-

--

_

_

)

332

44.

*45.

*46.

*47.

48.

49.

*50.

51.

4-70

4 / Induction and Recursion

Suppose that A I . A 2 , . . . , A n are a collection of sets. Suppose that R2 = A l EB A 2 and Rk = Rk- I EB A k for k = 3 , 4, . . . , n . Use mathematical induction to prove that x E Rn if and only if x belongs to an odd number of the sets A I , A 2 , . . . , A n . (Recall that S EB T is the symmetric difference of the sets S and T defined in Section 2 .2 .) Show that n circles divide the plane into n 2 - n + 2 re­ gions if every two circles intersect in exactly two points and no three circles contain a common point. Show that n planes divide three-dimensional space into (n 3 + 5n + 6)/6 regions if any three of these planes have a point in common and no four contain a common point. Use the well-ordering property to show that .../i is irra­ tional. [Hint: Assume that .../i is rational. Show that the set of positive integers of the form b .../i has a least el­ ement a. Then show that a .../i - a is a smaller positive integer of this form.] A set is well ordered if every nonempty subset of this set has a least element. Determine whether each of the following sets is well ordered. a) the set of integers b) the set of integers greater than - 100 c) the set of positive rationals d) the set of positive rationals with denominator less than 1 00 a) Show that if aI , a2 , . . . , an are positive integers, then gcd(al ' a2 , . . . , an - I , an ) = gcd(a l ' a2 , . . . , an - 2 , gcd(an _ I ' an ». b) Use part (a), together with the Euclidean algorithm, to develop a recursive algorithm for computing the great­ est common divisor of a set of n positive integers. Describe a recursive algorithm for writing the greatest common divisor of n positive integers as a linear combi­ nation of these integers. Find an explicit formula for f(n) iff( l ) = 1 and f(n) =

unkS�

fen - 1 ) + 2n - 1 for n ::: 2 . Prove your result using mathematical induction. * *52. Give a recursive definition of the set of bit strings that contain twice as many Os as 1 s. 53. Let S be the set of bit strings defined recursively by A E S and Ox E S, x I E S if X E S, where A is the empty string. a) Find all strings in S of length not exceeding five. b) Give an explicit description of the elements of S. 54. Let S be the set of strings defined recursively by abc E S, bac E S, and acb E S, where a, b, and c are fixed letters; and for all X E S, abcx E S; abxc E S, axbc E S, and xabc E S, where x is a variable representing a string of letters. a) Find all elements of S of length eight or less. b) Show that every element of S has a length divisible by three. The set B of all balanced strings of parentheses is defined recursively by A E B, where A is the empty string; (x) E B, xy E B ifx , Y E B. 55. Show that (00) is a balanced string of parentheses and (0» is not a balanced string of parentheses. 56. Find all balanced strings of parentheses with exactly six symbols. 57. Find all balanced strings of parentheses with four or fewer symbols. 58. Use induction to show that if x is a balanced string of parentheses, then the number of left parentheses equals the number of right parentheses in x . Define the function N on the set o f strings o f parentheses by N(A) = 0, N (O = 1 , N O) = - I , N(uv)

=

N(u) + N(v),

where A is the empty string, and u and v are strings. It can be shown that N is well defined.

JOHN McCARTHY (BORN 1 927) John McCarthy was born in Boston. He grew up in Boston and in Los Angeles. He studied mathematics as both an undergraduate and a graduate student, receiving his B.S. in 1 948 from the California Institute of Technology and his Ph.D. in 1 95 1 from Princeton. After graduating from Princeton, McCarthy held positions at Princeton, Stanford, Dartmouth, and M.1. T. He held a position at Stanford from 1 962 until 1 994, and is now an emeritus professor there. At Stanford, he was the director of the Artificial Intelligence Laboratory, held a named chair in the School of Engineering, and was a senior fellow in the Hoover Institution. McCarthy was a pioneer in the study of artificial intelligence, a term he coined in 1 955. He worked on problems related to the reasoning and information needs required for intelligent computer behavior. Mc­ Carthy was among the first computer scientists to design time-sharing computer systems. He developed LISp, a programming language for computing using symbolic expressions. He played an important role in using logic to verify the correctness of computer programs. McCarthy has also worked on the social implications of computer technology. He is currently working on the problem of how people and computers make conjectures through assumptions that complications are absent from situations. McCarthy is an advocate of the sustainability of human progress and is an optimist about the future of humanity. He has also begun writing science fiction stories. Some of his recent writing explores the possibility that the world is a computer program written by some higher force. Among the awards McCarthy has won are the Turing Award from the Association for Computing Machinery, the Research Excellence Award of the International Conference on Artificial Intelligence, the Kyoto Prize, and the National Medal of Science.

4-71

59.

Computer Projects Find

*67.

a) N « » . c) N «( )«

b) N 0» 0)(0 . ». d) N « ) « ( » )« » ). **60. Show that a string w of parentheses is balanced if and only if N(w) = 0 and N(u) ::: 0 whenever u is a prefix of w, that is, w = u v . *61. Give a recursive algorithm for finding all bal­ anced strings of parentheses containing n or fewer symbols. 62. Give a recursive algorithm for finding gcd(a, b), where a and b are nonnegative integers, based on these facts: gcd(a , b) = gcd(b, a) if a > b, gcd(O, b) = b, gcd(a , b) = 2 gcd(aI2, b12) if a and b are even, gcd(a, b) = gcd(aI2, b) if a is even and b is odd, and gcd(a, b) = gcd(a, b - a) . 63. Verify the program segment if x > y then x := y with respect to the initial assertion T and the final asser­ tion x :::s y . *64. Develop a rule of inference for verifying recursive pro­ grams and use it to verify the recursive program for com­ puting factorials given in Section 65. Devise a recursive algorithm that counts the number of times the integer 0 occurs in a list of integers. Exercises 66-73 deal with some unusual sequences, infor­ mally called self-generating sequences, produced by simple recurrence relations or rules. In particular, Exercises 66-7 1 deal with the sequence {a(n)} defined by a(n) = n - a(a(n 1» for n ::: 1 and a(O) = o. (This sequence, as well as those in Exercises 70 and 7 1 , are defined in Douglas Hofstader's fascinating book GOdel, Escher, Bach ([Ho99]). 66. Find the first 10 terms of the sequence {a(n)} defined in the preamble to this exercise.

4.4.

* *68.

*69.

70.

Prove that this sequence is well defined. That is, show that a(n) is uniquely defined for all nonnegative integers n . Prove that a(n) = L(n + 1 )t-tJ where t-t = (- 1 + ./5)/2. [Hint: First show for all n > 0 that (t-tn - Lt-tnJ) + (t-t 2 n - Lt-t 2 nJ) = 1 . Then show for all real numbers a with 0 :::s a < 1 and a =1 1 - t-t that L( 1 + t-t)( I - a)J + La + t-tJ = 1 , considering the cases 0 :::s a < 1 - t-t and 1 - t-t < a < 1 separately.] Use the formula from Exercise 68 to show that a(n) = a(n - 1 ) if t-tn - Lt-tnJ < 1 - t-t and a(n) = a(n - 1 ) + 1 otherwise. Find the first 10 terms of each of the following self­ generating sequences: a) a(n) = n - a(a(a(n - 1 » ) for n ::: 1 with a(O) = 0 b) a(n) = n - a(a(a(a(n - 1 » » for n ::: 1 with a(O) =

. c) 71.

333

o a(n) = a(n - a(n - 1 » + a(n - a(n - 2» for n with a(l) = 1 and a(2) = 1

:::

3

Find the first 1 0 terms of both the sequences m (n) and fen) defined by the following pair of interwoven recurrence relations: m en ) = n - f(m(n - 1 », fen) = n - m(f(n - 1 » for n ::: 1 with f(O) = 1 and m(O) = o.

Golomb's self-generating sequence is the unique nonde­ creasing sequence of positive integers a I , a2 , a 3 , . . . , which has the property that it contains exactly ak occurrences of k for each positive integer k. 72. Find the first 20 terms of Golomb's self-generating se­ quence. *73. Show that if f(n) is the largest integer m such that am = n, where am is the mth term of Golomb's self-generating sequence, then fen) = L � = I ak and f(f(n» = L � = I kak .

Computer Projects Write programs with these input and output.

* *1.

* *2.

* *3.

4. 5.

Given a 2n x 2n checkerboard with one square miss­ ing, construct a tiling of this checkerboard using right triominoes. Generate all well-formed formulae for expressions in­ volving the variables x, y, and z and the operators {+, *, I, -} with n or fewer symbols. Generate all well-formed formulae for propositions with n or fewer symbols where each symbol is T, F, one of the propositional variables p and q , or an operator from

6. *7.

Given a real number a and a nonnegative integer n, find a 2n using recursion. Given a real number a and a nonnegative integer n, find a n using the binary expansion of n and a recursive algorithm k for computing a 2 Given two integers not both zero, find their greatest com­ mon divisor using recursion. Given a list of integers and an element x, locate x in this list using a recursive implementation of a linear search. Given a list of integers and an element x, locate x in this list using a recursive implementation of a binary search. Given a nonnegative integer n, find the nth Fibonacci number using iteration. •

8. 9.

{--', v , 1\ , -+ , �}.

10.

Given a string, find its reversal. Given a real number a and a nonnegative integer n, find a n using recursion.

11.

334

12. 13.

4 / Induction and Recursion Given a nonnegative integer n, find the nth Fibonacci number using recursion. Given a positive integer, find the number of parti­ tions of this integer. (See Exercise 47 of Section 4.3.)

4- 72

14.

15.

Given positive integers m and n, find A(m , n), the value of Ackermann's function at the pair (m , n). (See the pream­ ble to Exercise 48 of Section 4.3.) Given a list of n integers, sort these integers using the merge sort.

Computations and Explorations Use a computational program or programs you have written to do these exercises.

1.

2.

3.

4.

What are the largest values of n for which n ! has fewer than 1 00 decimal digits and fewer than 1 000 decimal digits? Determine which Fibonacci numbers are divisible by 5, which are divisible by 7, and which are divisible by I I . Prove that your conjectures are correct. Construct tilings using right triominoes ofvarious 1 6 x 1 6, 32 x 32, and 64 x 64 checkerboards with one square missing. Explore which m x n checkerboards can be completely

* *5. * *6. 7. 8.

covered by right triominoes. Can you make a conjecture that answers this question? Implement an algorithm for determining whether a point is in the interior or exterior of the simple polygon. Implement an algorithm for triangulating a simple polygon . Which values of Ackermann's function are small enough that you are able to compute them? Compare either the number of operations or the time needed to compute Fibonacci numbers recursively versus that needed to compute them iteratively.

Writing Projects Respond to these with essays using outside sources.

1.

2.

3.

Describe the origins of mathematical induction. Who were the first people to use it and to which problems did they apply it? Explain how to prove the Jordan curve theorem for sim­ ple polygons and describe an algorithm for determining whether a point is in the interior or exterior of a simple polygon. Describe how the triangulation of simple polygons is used in some key algorithms in computational geometry.

4. 5.

6.

7.

Describe a variety of different applications of the Fibonacci numbers to the biological and the physical sciences . Discuss the uses ofAckermann's function both in the theory of recursive definitions and in the analysis ofthe complex­ ity of algorithms for set unions . Discuss some of the various methodologies used to es­ tablish the correctness of programs and compare them to Hoare's methods described in Section 4 . 5 . Explain how the ideas and concepts ofprogram correctness can be extended to prove that operating systems are secure .

C H A P T E R

Counting 5.1 The Basics of

Counting

5.2 The Pigeonhole

Principle

5.3 Permutations

and Combinations

5.4 Binomial

Coefficients

5.5 Generalized

Permutations and Combinations

5.6 Generating

Permutations and Combinations

5.1

� ombinatorics, the study of arrangements of objects, is an important part of discrete math­ \l...,. ematics. This subject was studied as long ago as the seventeenth century, when combi­

natorial questions arose in the study of gambling games. Enumeration, the counting of objects with certain properties, is an important part of combinatorics. We must count objects to solve many different types of problems. For instance, counting is used to determine the complexity of algorithms. Counting is also required to determine whether there are enough telephone num­ bers or Internet protocol addresses to meet demand. Furthermore, counting techniques are used extensively when probabilities of events are computed. The basic rules of counting, which we will study in Section 5 . 1 , can solve a tremendous variety of problems. For instance, we can use these rules to enumerate the different telephone numbers possible in the United States, the allowable passwords on a computer system, and the different orders in which the runners in a race can finish. Another important combinatorial tool is the pigeonhole principle, which we will study in Section 5 .2. This states that when objects are placed in boxes and there are more objects than boxes, then there is a box containing at least two objects. For instance, we can use this principle to show that among a set of 1 5 or more students, at least were born on the same day of the week. We can phrase many counting problems in terms of ordered or unordered arrangements of the objects of a set. These arrangements, called permutations and combinations, are used in many counting problems. For instance, suppose the 1 00 top finishers on a competitive exam taken by 2000 students are invited to a banquet. We can count the possible sets of 1 00 students that will be invited, as well as the ways in which the top 1 0 prizes can be awarded. Another problem in combinatorics involves generating all the arrangements of a specified kind. This is often important in computer simulations. We will devise algorithms to generate arrangements of various types.

3

The Basics of Counting Introduction

Suppose that a password on a computer system consists of six, seven, or eight characters. Each of these characters must be a digit or a letter of the alphabet. Each password must contain at least one digit. How many such passwords are there? The techniques needed to answer this question and a wide variety of other counting problems will be introduced in this section. Counting problems arise throughout mathematics and computer science. For example, we must count the successful outcomes of experiments and all the possible outcomes of these experiments to determine probabilities of discrete events. We need to count the number of operations used by an algorithm to study its time complexity. We will introduce the basic techniques of counting in this section. These methods serve as the foundation for almost all counting techniques. Basic Counting Principles

We will present two basic counting principles, the product rule and the sum rule. Then we will show how they can be used to solve many different counting problems. The product rule applies when a procedure is made up of separate tasks. 5- 1

335

336

5I

5-2

Counting

THE PRODUCT RULE Suppose that a procedure can be broken down into a sequence of two tasks. If there are n 1 ways to do the first task and for each of these ways of doing the first task, there are n 2 ways to do the second task, then there are n l n 2 ways to do the procedure.

Extra � Examples � EXAMPLE 1

Examples

1-10 show how the product rule is used.

12 Solution: The procedure of assigning offices to these two employees consists of assigning an office to Sanchez, which can be done in 12 ways, then assigning an office to Patel different from the office assigned to Sanchez, which can be done in 11 ways. By the product rule, there are ... 12 · 11 132 ways to assign offices to these two employees. A new company with just two employees, Sanchez and Patel, rents a floor of a building with offices. How many ways are there to assign different offices to these two employees?

=

EXAMPLE 2

100. Solution: The procedure of labeling a chair consists of two tasks, namely, assigning one of the 26thatletters and then assigning one of the 100 possible integers to the seat. The product rule shows there are 26 . 100 2600 different ways that a chair can be labeled. Therefore, the largest ... number of chairs that can be labeled differently is 2600. There are 32 microcomputers in a computer center. Each microcomputer has 24 ports. How many different ports to a microcomputer in the center are there? Solution: The procedure of choosing a port consists of two tasks, first picking a microcomputer and then picking a port on this microcomputer. Because there are 32 ways to choose the micro­ computer and 24 ways to choose the port no matter which microcomputer has been selected, the ... product rule shows that there are 32 . 24 768 ports. An extended version of the product rule is often useful. Suppose that a procedure is carried out by performing the tasks T1 , T2, . . . , Tm in sequence. If each task Ti, i 1, 2, . . . , n, can be done in ni ways, regardless of how the previous tasks were done, then there are n 1 n 2 · . . . . n m ways to carry out the procedure. This version of the product rule can be proved by mathematical induction from the product rule for two tasks (see Exercise 60 at the end of the section). The chairs of an auditorium are to be labeled with a letter and a positive integer not exceeding What is the largest number of chairs that can be labeled differently?

=

EXAMPLE 3

=

=



EXAMPLE 4

How many different bit strings of length seven are there?

Solution: Each of the seven bits can be chosen in two ways, because each bit is either 0 or 1. Therefore, the product rule shows there are a total of 2 7 128 different bit strings of length seven.

EXAMPLE 5

26 choices for each letter

choices for each digit

10

EXAMPLE 6

=

...

How many different license plates are available if each plate contains a sequence of three letters followed by three digits (and no sequences of letters are prohibited, even if they are obscene)?

Solution: There are 26 choices for each of the three letters and ten choices for each of the three digits. Hence, by the product rule there are a total of 26 · 26 · 26 · 10 · 10 · 10 17,576,000 possible license plates.

n elements?

Counting Functions

=

...

How many functions are there from a set with m elements to a set with

5 . 1 The Basics of Counting

5-3

337

Solution: A function corresponds to a choice of one of the n elements in the codomain for each of the m elements in the domain. Hence, by the product rule there are n . n . . . . . n n m functions from a set with m elements to one with n elements. For example, there are 5 3 different ..... functions from a set with three elements to a set with five elements. =

EXAMPLE 7

m elements to one with n elements? How many one-to-one functions are there from a set with Solution: First note when m n there are no one-to-one functions from a set with m elements to a set with n elements. Now let m ::::: n. Suppose the elements in the domain are ai, a2 , . . . , am . There are n ways to choose the value of the function at al. Because the function is one-to-one, the value of the function at a2 can be picked in n - 1 ways (because the value used for al cannot be used again). In general, the value of the function at ak can be chosen in n - k + 1 ways. By the product rule, there are n(n - 1 )(n - 2) · . · (n - m + 1 ) one-to-one functions from a set with m elements to one with elements. For example, there are 5 . 4 . 3 60 one-to-one functions from a set with three elements to ..... a set with five elements. Counting One-to-One Functions

>

n

EXAMPLE S

unkS �

=

numbering plan.

The Telephone Numbering Plan The format of telephone numbers in North America is specified by a A telephone number consists of 1 0 digits, which are split into a three-digit area code, a three-digit office code, and a four-digit station code. Because of signaling considerations, there are certain restrictions on some of these digits. To specify the allowable format, let X denote a digit that can take any of the values 0 through 9, let denote a digit that can take any of the values 2 through 9, and let denote a digit that must be a 0 or a 1 . Two numbering plans, which will be called the old plan and the new plan, will be discussed. (The old plan, in use in the 1 960s, has been replaced by the new plan, but the recent rapid growth in demand for new numbers for mobile phones and devices make even this new plan obsolete. In this example, the letters used to represent digits follow the conventions of the As will be shown, the new plan allows the use of more numbers. In the old plan, the formats of the area code, office code, and station code are and X\'X¥, respectively, so that telephone numbers had the form -X\'XX In the new plan, the formats of these codes are and XXXX, respectively, so that telephone numbers have the form NXX-NXX-X\'XX How many different North American telephone numbers are possible under the old plan and under the new plan?

N

Y

Numbering Plan.)

NYX-NNX

NIT, NIT,

North American NYJ(, NNJ(,

Solution: By the product rule, there are 8 · 2 · 1 0 1 60 area codes with format NYX and 8 · 1 0 . 1 0 800 area codes with format NXX. Similarly, by the product rule, there are 8 . 8 . 1 0 640 office codes with format NNX. The product rule also shows that there are 1 0 · 1 0 . 1 0 · 1 0 1 0, 000 station codes with format X\'XX =

=

=

=

Consequently, applying the product rule again, it follows that under the old plan there are 1 60 · 640 · 1 0 , 000

=

1 , 024, 000, 000

different numbers available in North America. Under the new plan there are 800 · 800 · 1 0 , 000

=

6, 400, 000, 000

different numbers available.

338

5/

5-4

Counting

EXAMPLE 9

k

What is the value of after the following code has been executed?

k :=il0:= 1 nl i2 := 1 n 2 for for

to

to

for

ikm :=:= k1 + 1n m to

Solution.' value of k is zero. Each time the nested loop is traversed, I is added to k.is theLet number Ti beThetheinitial task of traversing the ith loop. Then the number of times the loop is traversed of ways to do the tasks TJ, T2 , , Tm . The number of ways to carry out the task Tj, j = I, . . . is n because the j th loop is traversed once for each integer i with 1 i n By the product rule, it follows that the nested loop is traversed n I n 2 . . . n m times..... Hence, the final value of k is nln 2 ' " n m . .s:

EXAMPLE 10

j

.s:

j.

2,

, m,

•••

j,

Counting Subsets of a Finite Set

subsets of a finite set S is 21SI •

j

Use the product rule to show that the number of different

Solution: Let S be a finite set. List the elements of S in arbitrary order. Recall from Section l S I . Namely, a subset of S is associated with the bit string with a 1 in the ith position if the ith element in the list is in the subset, and a 0 in this position otherwise. By the product rule, there are bit strings of length l S I . Hence, I P (S) I = The product rule is often phrased in terms of sets in this way: If AI, A 2 , . . . , A m are finite sets, then the number of elements in the Cartesian product of these sets is the product of the number of elements in each set. To relate this to the product rule, note that the task of choosing an element in the Cartesian product A I A 2 . . . A m is done by choosing an element in A I, an element in A 2 , . . . , and an element in A m . By the product rule it follows that 2 .2 that there is a one-to-one correspondence between subsets of S and bit strings of length 21s l •

21s1

X

X

....

x

We now introduce the sum rule.

n

n

THE SUM RULE If a task can be done either in one of I ways is the same as any of the set ways to do the task. none of the set of

Example EXAMPLE 11

I ways or in one of n2 ways, where of n 2 ways, then there are n I + n2

11 illustrates how the sum rule is used.

Suppose that either a member ofthe mathematics faculty or a student who is a mathematics major is chosen as a representative to a university committee. How many different choices are there for this representative if there are members of the mathematics faculty and mathematics majors and no one is both a faculty member and a student?

Solution:

37

37

83

83

There are ways to choose a member of the mathematics faculty and there are ways to choose a student who is a mathematics major. Choosing a member of the mathematics

5 . 1 The Basics of Counting

5-5

339

37 + 83 = 120

faculty is never the same as choosing a student who is a mathematics major because no one is both a faculty member and a student. By the sum rule it follows that there are .... possible ways to pick this representative.

n

nm nl + n 2 + . . . +12n m. 13 j

We can extend the sum rule to more than two tasks. Suppose that a task can be done in one of I ways, in one of n 2 ways, . . . , or in one of ways, where none of the set of ni ways of doing the task is the same as any of the set of nj ways, for all pairs i and with � i < � m . Then the number of ways to do the task i s This extended version of the sum rule is often useful in counting problems, as Examples and show. This version of the sum rule can be proved using mathematical induction from the sum rule for two sets. (This is Exercise at the end of the section.)

59

EXAMPLE 12

EXAMPLE 13

I

j

23, 15, 19 Solution: The student can choose a project by selecting a project from the first list, the second list, or the third list. Because no project is on more than one list, by the sum rule there are .... 23 + 15 + 19 = 57 ways to choose a project, What is the value of k after the following code has been executed? kfor: =il0:= 1 to ni for ikk2 :=:=: = kk1 to++ n11 2

A student can choose a computer project from one of three lists. The three lists contain and possible projects, respectively. No project is on more than one list. How many possible projects are there to choose from?

for ikm :=:= k1 +to 1n m

Solution: The initial value of k is zero. This block of code is made up of different loops. Each time a loop is traversed, 1 is added to k . To determine the value of k after this code has m

been executed, we need to determine how many times we traverse a loop. Note that there are ni ways to traverse the ith loop. Because we only traverse one loop at a time, the sum rule shows that the final value of which is the number of ways to traverse one of the m loops is

nl + n 2 + . . . + nm .

k,

AI, A2 , ,

....

The sum rule can be phrased in terms of sets as: If A m are disjoint finite sets, then the number of elements in the union of these sets is the sum of the numbers of elements in the sets. To relate this to our statement of the sum rule, note there are ways to choose an element from for i . . . , m . Because the sets are disjoint, when we select an element from one of the sets we do not also select an element from a different set Aj • Consequently, by the sum rule, because we cannot select an element from two of these sets at the same time, the number of ways to choose an element from one of the sets, which is the number of elements in the union, is •

Ai Ai,= 1, 2,

.



I Ai I

340

5-6

5 / Counting

This equality applies only when the sets in question are disjoint. The situation is much more complicated when these sets have elements in common. That situation will be briefly discussed later in this section and discussed in more depth in Chapter

7.

More Complex Counting Problems Many counting problems cannot be solved using just the sum rule or just the product rule. However, many complicated counting problems can be solved using both of these rules in combination. EXAMPLE 14

Extra � Examples �

In a version of the computer language BASIC, the name of a variable is a string of one or two alphanumeric characters, where uppercase and lowercase letters are not distinguished. (An character is either one of the English letters or one of the digits.) Moreover, a variable name must begin with a letter and must be different from the five strings of two characters that are reserved for programming use. How many different variable names are there in this version of BASIC?

alphanumeric

26

10

Solution: Let V equal the number of different variable names in this version of BASIC. Let VI be the number of these that are one character long and V2 be the number of these that are characters long. Then by the sum rule, V VI + V2 . Note that VI 26, because a one-character variable name must be a letter. Furthermore, by the product rule there are 26 . 36 strings oflength two that begin with a letter and end with an alphanumeric character. However, five of these are excluded, so V2 26 . 36 - 5 931. Hence, there are V VI + V2 26 + 931 957.... different names for variables in this version of BASIC. =

=

EXAMPLE 1 5

two

=

=

=

=

=

Each user on a computer system has a password, which is six to eight characters long, where each character is an uppercase letter or a digit. Each password must contain at least one digit. How many possible passwords are there?

Solution: + + P8.

6, 7, 8,

Let P be the total number of possible passwords, and let P6, P7, and Pg denote the number of possible passwords of length and respectively. By the sum rule, P = P6 P7 We will now find P6, P7 , and P8 • Finding P6 directly is difficult. To find P6 it is easier to find the number of strings of uppercase letters and digits that are six characters long, including those with no digits, and subtract from this the number of strings with no digits. By the product rule, the number of strings of six characters is 6 , and the number of strings with Hence, no digits is P6

=

36 266 • 366 - 266 2,176,782,336 - 308,915,776 1,867,866,560. =

=

Similarly, it can be shown that P7 and

=

367 - 267 78,364,164,096 - 8,031,810,176 70,332,353,920 =

=

8 368 - 268 2,821,109,907,456 - 208,827,064,576 2,612,282,842,880.

P

=

=

=

Consequently, P

=

P6

+ P7 + P8 2,684,483,063,360 . =

5- 7

Basics of Counting

5 . 1 The

Bit Number

o

Class A

0

Class B

1

0

Class C

I

1

0

Class D

1

1

1

0

Class E

1

1

1

1

FIGURE EXAMPLE 16

unkS�

1

2

3

4

netid netid

I

8

16

I

netid

o

31

24

hostid hostid

Multicast Address

I

341

I

hostid

Address

Internet Addresses (IPv4).

Counting Internet Addresses In the Internet, which is made up of interconnected physical networks of computers, each computer (or more precisely, each network connection of a com­ puter) is assigned an In Version 4 of the Internet Protocol (IPv4), now in use, an address is a string of 32 bits. It begins with a The netid is followed by a which identifies a computer as a member of a particular network. Three forms of addresses are used, with different numbers of bits used for netids and hostids. Class A addresses, used for the largest networks, consist of 0, followed by a 7 -bit netid and a 24-bit hostid. Class B addresses, used for medium-sized networks, consist of 1 0, followed by a 14-bit netid and a 1 6-bit hostid. Class C addresses, used for the smallest networks, consist of 1 1 0, followed by a 2 1 -bit netid and an 8-bit hostid. There are several restrictions on addresses because of special uses: 1 1 1 1 1 1 1 is not available as the netid of a Class A network, and the hostids consisting of all Os and all I s are not available for use in any network. A computer on the Internet has either a Class A, a Class B, or a Class C address. (Besides Class A, B, and C addresses, there are also Class D addresses, reserved for use in multicasting when multiple computers are addressed at a single time, consisting of 1 1 1 0 followed by 28 bits, and Class E addresses, reserved for future use, consisting of 1 1 1 1 0 followed by 27 bits. Neither Class D nor Class E addresses are assigned as the IP address of a computer on the Internet.) Figure 1 illustrates IPv4 addressing. (Limitations on the number of Class A and Class B netids have made IPv4 addressing inadequate; IPv6, a new version of Ip, uses 1 28-bit addresses to solve this problem.) How many different IPv4 addresses are available for computers on the Internet?

Internet address. host number (hostid),

Solution:

network number (netid).

Let x be the number of available addresses for computers on the Internet, and let x A , X B , and Xc denote the number of Class A, Class B , and Class C addresses available, respectively. By the sum rule, x = XA XB xc . To find XA , note that there are 2 7 - I = 127 Class A netids, recalling that the netid

+ +

1 1 1 1 1 1 1 is unavailable. For each netid, there are 2 24 - 2 = 1 6 , 777, 2 1 4 hostids, recalling that the hostids consisting of all Os and all I s are unavailable. Consequently, XA = 1 27 · 1 6 , 777, 2 1 4 = 2, 1 30, 706, 1 78. To find XB and Xc , note that there are 2 1 4 = 1 6 , 3 84 Class B netids and 2 2 1 = 2 , 097, 1 52 Class C netids. For each Class B netid, there are 2 1 6 - 2 = 65, 534 hostids, and for each Class C netid, there are 2 8 - 2 = 254 hostids, recalling that in each network the hostids consisting of all Os and all I s are unavailable. Consequently, XB = 1 , 073 , 709, 056 and Xc = 532, 676, 608. We conclude that the total number of IPv4 addresses available is x = XA XB Xc = � 2, 1 30, 706, 1 78 1 , 073 , 709, 056 532 , 676, 608 = 3 , 737,091 , 842.

+

+ +

+

The Inclusion-Exclusion Principle

n n n2 2

n

Suppose that a task can be done in 1 or in ways, but that some of the set of 1 ways to do the task are the same as some of the other ways to do the task. In this situation, we cannot use the sum rule to count the number of ways to do the task. Adding the number of ways to do

342

5/

5-8

Counting

the tasks in these two ways leads to an overcount, because the ways to do the task in the ways that are common are counted twice. To correctly count the number of ways to do the two tasks, we add the number of ways to do it in one way and the number of ways to do it in the other way, and then subtract the number of ways to do the task in a way that is both among the set of ways and the set of ways. This technique is called the principle of inclusion-exclusion. Sometimes, it is also called the subtraction principle for counting. Example illustrates how we can solve counting problems using this principle.

nI

EXAMPLE 1 7

Ex1ra � Examples lliiiiliil

1

- - - - - - - -

2 7 = 128 ways

o

0

- - - - - - - -

25 = 32 ways

n2

17

1

How many bit strings of length eight either start with a bit or end with the two bits OO?

Solution:

00,

11

We can construct a bit string of length eight that either starts with a bit or ends with the two bits by constructing a bit string of length eight beginning with a bit or by constructing a bit string of length eight that ends with the two bits We can construct a bit string of length eight that begins with a in = ways. This follows by the product rule, because the first bit can be chosen in only one way and each of the other seven bits can be chosen in two ways. Similarly, we can construct a bit string of length eight ending with the two bits in = ways. This follows by the product rule, because each of the first six bits can be chosen in two ways and the last two bits can be chosen in only one way. Some of the ways to construct a bit string of length eight starting with a are the same as the ways to construct a bit string of length eight that ends with the two bits There are = ways to construct such a string. This follows by the product rule, because the first bit can be chosen in only one way, each of the second through the sixth bits can be chosen in two ways, and the last two bits can be chosen in one way. Consequently, the number of bit strings of length eight that begin with a or end with a which equals the number of ways to construct a bit string of length eight that begin with a or that ends with equals 32 =

1 27 128

00, 26 64

00.

1 00.

25 32

1

l6Q

00,1

00,

128 + 64

-



I A II A I A2 A2 . AIAI I A2 1A , 2 A A , 2A I A2 1 AI A AI A2 , I . 2 I A I A21 I A I A21 I A II I A2 1 - IA I A2 1. This is the formula given in Section 2. 2 for the number of elements in the union of two sets. We present an example that illustrates how the formulation of the principle of inclusion­

We can phrase this counting principle in terms of sets. Let and be sets. There are ways to select an element from and ways to select an element from The number of ways to select an element from or from that is, the number of ways to select an element from their union, is the sum of the number of ways to select an element from 1 and the number of ways to select an element from minus the number of ways to select an element that is in both and ways to select an element in either or in Because there are U and n ways to select an element common to both sets, we have U

=

+

n

exclusion can be used to solve counting problems. EXAMPLE 18

350 220 51

147

A computer company receives applications from computer graduates for a job planning a line of new Web servers. Suppose that of these people majored in computer science, majored in business, and majored both in computer science and in business. How many of these applicants majored neither in computer science nor in business?

Solution: To find the number of these applicants who majored neither in computer science nor in business, we can subtract the number of students who majored either in computer science or in business (or both) from the total number of applicants. Let A I be the set of students who majored in computer science and A 2 the set of students who majored in business. Then A I A 2 is the set of students who majored in computer science or business (or both), and AI A 2 is U

n

the set of students who majored both in computer science and in business. By the principle of

5 . 1 The Basics of Counting

5-9

343

inclusion-exclusion, the number of students who majored either in computer science or in business (or both) equals

I A I A21 = I A II + I A2 1 - I A I A2 1 = 220 + 147 - 51 = 316. We conclude that 350 - 316 = 34 of the applicants majored neither in computer science nor in .... business. U

n

nn

n

The principle of inclusion-exclusion can be generalized to find the number of ways to do one of different tasks or, equivalently, to find the number of elements in the union of sets, whenever is a positive integer. We will study the inclusion-exclusion principle and some of its many applications in Chapter

1st bit

7.

2nd bit

Tree Diagrams 4th bit

o 1 0 1 00 1 0

:=: 0 8 0 8 :=: 0 8 ::: :=: :=: 5 8 8 8 8

FIGURE 2 Bit Strings of Length Four without Consecutive Is. EXAMPLE 19

Counting problems can be solved using tree diagrams. A tree consists of a root, a number of branches leaving the root, and possible additional branches leaving the endpoints of other branches. (We will study trees in detail in Chapter To use trees in counting, we use a branch to represent each possible choice. We represent the possible outcomes by the leaves, which are the endpoints of branches not having other branches starting at them. Note that when a tree diagram is used to solve a counting problem, the number of choices required to reach a leaf can vary (see Example for example).

10.)

20,

1

How many bit strings of length four do not have two consecutive s?

Solution: The tree diagram in Figure 2 displays all bit strings of length four without two con­ secutive 1 s. We see that there are eight bit strings of length four without two consecutive 1 s . .... EXAMPLE 20

A playoffbetween two teams consists of at most five games.

The first team that wins three games wins the playoff. In how many different ways can the playoff occur?

Winning team shown in color Game l

Game 4

..., " 0>

S

IV

FIGURE 3

..., " 0>

S

..., " 0>

S

IV

..., " 0>

S

-

..., " 0>

S

IV

..., " 0>

s

..., " 0>

a

IV

Best Three Games Out of Five Playoffs.

..., " 0>

s

..., " 0>

s

IV

..., " 0>

s

..., " 0>

s

IV

..., " 0>

S

Game 5

344

5 / Counting

5- 1 0

w

W

=

white, R

R

G

=

B

FIGURE 4

red, G

W

R

=

green, B

G

B

=

W

black

R

G

B

R

G

B

G

B

Counting Varieties of T-Shirts.

Solution: The tree diagram in Figure 3 displays all the ways the playoff can proceed, with the winner of each game shown. We see that there are 20 different ways for the playoff to occur. .... EXAMPLE 21

Suppose that "I Love New Jersey" T-shirts come in five different sizes: S, M, L, XL, and XXL. Further suppose that each size comes in four colors, white, red, green, and black, except for XL, which comes only in red, green, and black, and XXL, which comes only in green and black. How many different shirts does a souvenir shop have to stock to have at least one of each available size and color of the T-shirt?

Solution: The tree diagram in Figure 4 displays all possible size and color pairs. It follows that .... the souvenir shop owner needs to stock 17 different T-shirts. Exercises 1.

2. 3.

4.

5.

6.

There are 1 8 mathematics majors and 325 computer sci­ ence majors at a college. a) How many ways are there to pick two representatives so that one is a mathematics major and the other is a computer science major? b) How many ways are there to pick one representative who is either a mathematics major or a computer sci­ ence major? An office building contains 27 floors and has 37 offices on each floor. How many offices are in the building? A multiple-choice test contains 1 0 questions. There are four possible answers for each question. a) How many ways can a student answer the questions on the test if the student answers every question? b) How many ways can a student answer the questions on the test if the student can leave answers blank? A particular brand of shirt comes in 1 2 colors, has a male version and a female version, and comes in three sizes for each sex. How many different types of this shirt are made? Six different airlines fly from New York to Denver and seven fly from Denver to San Francisco. How many dif­ ferent pairs of airlines can you choose on which to book a trip from New York to San Francisco via Denver, when you pick an airline for the flight to Denver and an airline for the continuation flight to San Francisco? How many of these pairs involve more than one airline? There are four major autoroutes from Boston to Detroit

7. 8. 9.

10. 11. 12. 13. 14. 15. 1 6. 1 7.

1 8.

and six from Detroit to Los Angeles . How many major auto routes are there from Boston to Los Angeles via Detroit? How many different three-letter initials can people have? How many different three-letter initials with none of the letters repeated can people have? How many different three-letter initials are there that be­ gin with an A? How many bit strings are there of length eight? How many bit strings of length ten both begin and end with a I ? How many bit strings are there of length six or less? How many bit strings with length not exceeding n, where n is a positive integer, consist entirely of 1 s? How many bit strings of length n , where n is a positive integer, start and end with I s? How many strings are there of lowercase letters of length four or less? How many strings are there of four lowercase letters that have the letter x in them? How many strings of five ASCII characters contain the character @ ("at" sign) at least once? (Note: There are 128 different ASCII characters.) How many positive integers between 5 and 3 1 a) are divisible by 3? Which integers are these? b) are divisible by 4? Which integers are these? c) are divisible by 3 and by 4? Which integers are these?

5 . 1 The Basics of Counting

5- 1 1

19.

20.

21.

22.

23.

24.

25.

26.

27.

How many positive integers between 5 0 and 1 00 a) are divisible by 7? Which integers are these? b) are divisible by I I ? Which integers are these? c) are divisible by both 7 and 1 1 ? Which integers are these? How many positive integers less than 1 000 a) are divisible by 7? b) are divisible by 7 but not by I I ? c) are divisible by both 7 and I I ? d) are divisible by either 7 or I I ? e) are divisible by exactly one of 7 and I I ? t) are divisible by neither 7 nor I I? g) have distinct digits? h) have distinct digits and are even? How many positive integers between 1 00 and 999 inclusive a) are divisible by 7? b) are odd? c) have the same three decimal digits? d) are not divisible by 4? e) are divisible by 3 or 4? t) are not divisible by either 3 or 4? g) are divisible by 3 but not by 4? h) are divisible by 3 and 4? How many positive integers between 1 000 and 9999 inclusive a) are divisible by 9? b) are even? c) have distinct digits? d) are not divisible by 3? e) are divisible by 5 or 7? t) are not divisible by either 5 or 7? g) are divisible by 5 but not by 7? h) are divisible by 5 and 7? How many strings of three decimal digits a) do not contain the same digit three times? b) begin with an odd digit? c) have exactly two digits that are 4s? How many strings of four decimal digits a) do not contain the same digit twice? b) end with an even digit? c) have exactly three digits that are 9s? A committee is formed consisting of one representative from each ofthe 50 states in the United States, where the representative from a state is either the governor or one of the two senators from that state. How many ways are there to form this committee? How many license plates can be made using either three digits followed by three letters or three letters followed by three digits? How many license plates can be made using either two letters followed by four digits or two digits followed by four letters?

345

How many license plates can be made using either three letters followed by three digits or four letters followed by two digits? 29. How many license plates can be made using either two or three letters followed by either two or three digits? 30. How many strings of eight English letters are there a) if letters can be repeated? b) if no letter can be repeated? c) that start with X, if letters can be repeated? d) that start with X, if no letter can be repeated? e) that start and end with X, if letters can be repeated? t) that start with the letters BO (in that order), if letters can be repeated? g) that start and end with the letters BO (in that order), if letters can be repeated? h) that start or end with the letters BO (in that order), if letters can be repeated? 3 1 . How many strings of eight English letters are there a) that contain no vowels, if letters can be repeated? b) that contain no vowels, if letters cannot be repeated? c) that start with a vowel, if letters can be repeated? d) that start with a vowel, if letters cannot be repeated? e) that contain at least one vowel, if letters can be re­ peated? t) that contain exactly one vowel, if letters can be re­ peated? g) that start with X and contain at least one vowel, if letters can be repeated? h) that start and end with X and contain at least one vowel, if letters can be repeated? 32. How many different functions are there from a set with 1 0 elements to sets with the following numbers of elements? a) 2 b) 3 c) 4 d) 5 33. How many one-to-one functions are there from a set with five elements to sets with the following number of elements? d) 7 a) 4 c) 6 b) 5 34. How many functions are there from the set { I , 2, . . . , n}, where n i s a positive integer, to the set {O , I }? 35. How many functions are there from the set { I , 2, . . . , n}, where n is a positive integer, to the set {O, l } a) that are one-to-one? b) that assign 0 to both I and n? c) that assign I to exactly one ofthe positive integers less than n? 36. How many partial functions (see the preamble to Exercise 73 in Section 2.3) are there from a set with five elements to sets with each of these number of elements? b) 2 d) 9 a) I c) 5 37. How many partial functions (see the preamble to Exercise 73 in Section 2.3) are there from a set with m elements to a set with n elements, where m and n are positive integers? 38. How many subsets of a set with 1 00 elements have more than one element? 28.

346

5 / Counting

5- 12

A palindrome is a string whose reversal is identical to the string. How many bit strings oflength n are palindromes? 40. In how many ways can a photographer at a wedding ar­ range 6 people in a row from a group of 1 0 people, where the bride and the groom are among these 1 0 people, if a) the bride must be in the picture? b) both the bride and groom must be in the picture? c) exactly one of the bride and the groom is in the picture? 41. In how many ways can a photographer at a wedding ar­ range six people in a row, including the bride and groom, if a) the bride must be next to the groom? b) the bride is not next to the groom? c) the bride is positioned somewhere to the left of the groom? 42. How many bit strings of length seven either begin with two Os or end with three 1 s? 43. How many bit strings oflength 1 0 either begin with three Os or end with two Os? *44. How many bit strings oflength 10 contain either five con­ secutive Os or five consecutive 1 s? 1 , < , ! , +, and = a) How many different passwords are available for this computer system? b) How many ofthese passwords contain at least one oc­ currence of at least one of the six special characters? c) If it takes one nanosecond for a hacker to check whether each possible password is your password, how long would it take this hacker to try every possible password? 50. The name of a variable in the C programming language is a string that can contain uppercase letters, lowercase let­ ters, digits, or underscores. Further, the first character in the string must be a letter, either uppercase or lowercase, or an underscore. If the name of a variable is determined by its first eight characters, how many different variables 39.

.

can be named in C? (Note that the name of a variable may contain fewer than eight characters.) 51. Suppose that at some future time every telephone in the world is assigned a number that contains a coun­ try code 1 to 3 digits long, that is, of the form X, XX, or XXX, followed by a 10-digit telephone num­ ber of the form NXX-NXX-XXXX (as described in Example 8). How many different telephone numbers would be available worldwide under this numbering plan? 52. Use a tree diagram to find the number of bit strings of length four with no three consecutive Os. 53. How many ways are there to arrange the letters a , b, c, and d such that a is not followed immediately by b? 54. Use a tree diagram to find the number of ways that the World Series can occur, where the first team that wins four games out of seven wins the series. 55. Use a tree diagram to determine the number of subsets of {3, 7, 9, 1 1 , 24} with the property that the sum of the elements in the subset is less than 28. 56. a) Suppose that a store sells six varieties of soft drinks: cola, ginger ale, orange, root beer, lemonade, and cream soda. Use a tree diagram to determine the num­ ber of different types of bottles the store must stock to have all varieties available in all size bottles if all vari­ eties are available in 12-ounce bottles, all but lemon­ ade are available in 20-ounce bottles, only cola and ginger ale are available in 32-ounce bottles, and all but lemonade and cream soda are available in 64-ounce bottles? b) Answer the question in part (a) using counting rules. 57. a) Suppose that a popular style of running shoe is avail­ able for both men and women. The woman's shoe comes in sizes 6, 7, 8, and 9, and the man's shoe comes in sizes 8, 9, 1 0, 1 1 , and 12. The man's shoe comes in white and black, while the woman's shoe comes in white, red, and black. Use a tree diagram to determine the number of different shoes that a store has to stock to have at least one pair of this type of running shoe for all available sizes and colors for both men and women. b) Answer the question in part (a) using counting rules. *58. Use the product rule to show that there are 22" different truth tables for propositions in n variables. 59. Use mathematical induction to prove the sum rule for m tasks from the sum rule for two tasks. 60. Use mathematical induction to prove the product rule for m tasks from the product rule for two tasks. 61. How many diagonals does a convex polygon with n sides have? (Recall that a polygon is convex if every line seg­ ment connecting two points in the interior or boundary of the polygon lies entirely within this set and that a diagonal of a polygon is a line segment connecting two vertices that are not adjacent.) 62. Data are transmitted over the Internet in datagrams, which are structured blocks of bits. Each datagram

5- 1 3

5.2

contains header information organized into a maximum of 1 4 different fields (specifying many things, including the source and destination addresses) and a data area that contains the actual data that are transmitted. One of the 1 4 header fields is the header length field (denoted by HLEN), which is specified by the protocol to be 4 bits long and that specifies the header length in terms of32-bit blocks of bits. For example, if HLEN = 0 1 1 0, the header is made up of six 32-bit blocks. Another of the 1 4 header fields is the 1 6-bit-long total length field (denoted by TOTAL LENGTH), which specifies the length in bits of the entire datagram, including both the header fields and the data area. The length ofthe data area is the total length of the datagram minus the length of the header.

5.2

a)

b)

c) d)

The Pigeonhole Principle

347

The largest possible value of TOTAL LENGTH (which is 1 6 bits long) determines the maximum total length in octets (blocks of 8 bits) of an Internet datagram. What. is this value? The largest possible value of HLEN (which is 4 bits long) determines the maximum total header length in 32-bit blocks. What is this value? What is the maximum total header length in octets? The minimum (and most common) header length is 20 octets. What is the maximum total length in octets of the data area of an Internet datagram? How many different strings of octets in the data area can be transmitted if the header length is 20 octets and the total length is as long as possible?

The Pigeonhole Principle Introduction

unkS �

Suppose that a flock of 20 pigeons flies into a set of 1 9 pigeonholes to roost. Because there are 20pigeons pigeons but only 1 9 pigeonholes, a least one of these 1 9 pigeonholes must have at least two in it. To see why this is true, note that if each pigeonhole had at most one pigeon in it, at most 1 9 pigeons, one per hole, could be accommodated. This illustrates a general principle called the pigeonhole principle, which states that if there are more pigeons than pigeonholes, then there must be at least one pigeonhole with at least two pigeons in it (see Figure 1). Of course, this principle applies to other objects besides pigeons and pigeonholes.

THEOREM l

k

k

k 1

THE PIGEONHOLE PRINCIPLE If is a positive integer and + or more objects are placed into boxes, then there is at least one box containing two or more of the objects.

k. k

Proof: We will prove the pigeonhole principle using a proof by contraposition. Suppose that

k+

none of the boxes contains more than one object. Then the total number of objects would be I T I , then there are elements Sl and S2 in S such that f(sd = f(sz ), or in other words, f is not one-to-one. 9. What is the minimum number of students, each of whom comes from one of the 50 states, who must be enrolled in a university to guarantee that there are at least 1 00 who come from the same state? *10. Let (xi , Yi ), i = 1 , 2, 3 , 4, 5, be a set offive distinct points with integer coordinates in the xy plane. Show that the midpoint ofthe line joining at least one pair ofthese points has integer coordinates. 1.

Let (xi , Yi , zi ), i = 1 , 2, 3 , 4, 5, 6, 7, 8, 9, be a set ofnine distinct points with integer coordinates in xyz space. Show that the midpoint of at least one pair of these points has integer coordinates. 12. How many ordered pairs of integers (a , b) are needed to guarantee that there are two ordered pairs (ai , bl ) and (az , bz ) such that al mod 5 = az mod 5 and bl mod 5 = bz mod 5? 13. a) Show that if five integers are selected from the first eight positive integers, there must be a pair of these integers with a sum equal to 9. b) Is the conclusion in part (a) true if four integers are selected rather than five? 14. a) Show that if seven integers are selected from the first 1 0 positive integers, there must be at least two pairs of these integers with the sum 1 1 . b) Is the conclusion in part (a) true if six integers are selected rather than seven? 15. How many numbers must be selected from the set { I , 2, 3 , 4, 5 , 6} to guarantee that at least one pair of these numbers add up to 7? 16. How many numbers must be selected from the set { I , 3 , 5 , 7, 9, 1 1 , 1 3 , I 5 } to guarantee that at least one pair of these numbers add up to 1 6? 1 7. A company stores products in a warehouse. Storage bins in this warehouse are specified by their aisle, location in the aisle, and shelf. There are 50 aisles, 85 horizontal locations in each aisle, and 5 shelves throughout the ware­ house. What is the least number of products the company can have so that at least two products must be stored in the same bin? 18. Suppose that there are nine students in a discrete mathe­ matics class at a small college. a) Show that the class must have at least five male stu­ dents or at least five female students. b) Show that the class must have at least three male stu­ dents or at least seven female students. 19. Suppose that every student in a discrete mathematics class of 25 students is a freshman, a sophomore, or a junior. a) Show that there are at least nine freshmen, at least nine sophomores, or at least nine juniors in the class.

*11.

5 / Counting

354

Show that there are either at least three freshmen, at least 19 sophomores, or at least five juniors in the class. 20. Find an increasing subsequence of maximal length and a decreasing subsequence of maximal length in the se­ quence 22, 5, 7, 2, 23, 1 0, 1 5, 2 1 , 3, 17. 2 1 . Construct a sequence of 1 6 positive integers that has no increasing or decreasing subsequence of five terms . 22. Show that if there are 1 0 I people of different heights standing in a line, it is possible to find I I people in the order they are standing in the line with heights that are either increasing or decreasing. *23. Describe an algorithm in pseudocode for producing the largest increasing or decreasing subsequence of a se­ quence of distinct integers. 24. Show that in a group of five people (where any two peo­ ple are either friends or enemies), there are not necessarily three mutual friends or three mutual enemies. 25. Show that in a group of 1 0 people (where any two people are either friends or enemies), there are ei­ ther three mutual friends or four mutual enemies, and there are either three mutual enemies or four mutual friends . 26. Use Exercise 25 to show that among any group of 20 people (where any two people are either friends or ene­ mies), there are either four mutual friends or four mutual enemies. 27. Show that if n is a positive integer with n ::: 2, then the Ramsey number R (2 , n) equals n . 28. Show that i f m and n are positive integers with m ::: 2 and n ::: 2, then the Ramsey numbers R(m , n) and R(n , m) are equal. 29. Show that there are at least six people in California (pop­ ulation: 36 million) with the same three initials who were born on the same day of the year (but not necessar­ ily in the same year). Assume that everyone has three initials. 30. Show that if there are 1 00,000,000 wage earners in the United States who earn less than 1 ,000,000 dollars, then there are two who earned exactly the same amount of money, to the penny, last year. 3 1 . There are 38 different time periods during which classes at a university can be scheduled. If there are 677 different classes, how many different rooms will be needed? 32. A computer network consists of six computers. Each com­ puter is directly connected to at least one of the other com­ puters. Show that there are at least two computers in the network that are directly connected to the same number of other computers. 33. A computer network consists of six computers. Each com­ puter is directly connected to zero or more of the other computers. Show that there are at least two computers in the network that are directly connected to the same num­ ber of other computers. [Hint: It is impossible to have b)

5-20

a computer linked to none of the others and a computer linked to all the others.] 34. Find the least number of cables required to connect eight computers to four printers to guarantee that four comput­ ers can directly access four different printers. Justify your answer. 35. Find the least number of cables required to connect 100 computers to 20 printers to guarantee that 20 computers can directly access 20 different printers. (Here, the as­ sumptions about cables and computers are the same as in Example 9.) Justify your answer. *36. Prove that at a party where there are at least two people, there are two people who know the same number of other people there. 37. An arm wrestler is the champion for a period of75 hours . (Here, by an hour, we mean a period starting from an exact hour, such as 1 P. M . , until the next hour.) The arm wrestler had at least one match an hour, but no more than 1 25 total matches. Show that there is a period of con­ secutive hours during which the arm wrestler had exactly 24 matches. *38. Is the statement in Exercise 37 true if 24 is replaced by a) 2? b) 23? c) 25? d) 30? 39. Show that if f is a function from S to T , where S and T are finite sets and m = n S 1 / 1 T 1 1 , then there are at least m elements of S mapped to the same value of T . That is, show that there are distinct elements s " S2 , . . . , Sm of S such that f(sd = f(S2 ) = . . . = f(sm ). 40. There are 5 1 houses on a street. Each house has an address between 1 000 and 1099, inclusive. Show that at least two houses have addresses that are consecutive integers. *41 . Let x be an irrational number. Show that for some posi­ tive integer j not exceeding n, the absolute value of the difference between j x and the nearest integer to jx is less than lin . 42. Let n " n 2 , . . . , nl be positive integers. Show that if n , + n 2 + . . . + nl - t + 1 objects are placed into t boxes, then for some i, i = 1 , 2 , . . . , t, the ith box con­ tains at least ni objects. *43. A proof of Theorem 3 based on the generalized pigeon­ hole principle is outlined in this exercise. The notation used is the same as that used in the proof in the text . a) Assume that h ::::: n for k = 1 , 2 , . . . , n 2 + l . Use the generalized pigeonhole principle to show that there are n + 1 terms ak 1 ' ak2 , . . . , akn + 1 with ik 1 = ik2 = . . = ikn +1 , where I ::::: k, < k2

< . . . < kn+ , .

.

Show that akj > akj+ 1 for j = 1 , 2 , . . . , n. [Hint: As­ sume that akj < akj +1 , and show that this implies that ik > ik + 1 , which is a contradiction.] j j c) Use parts (a) and (b) to show that if there is no increasing subsequence of length n + I , then there must be a decreasing subsequence of this length. b)

5-2 1

53

5 . 3 Pennutations and Combinations

355

Permutations and Combinations Introduction Many counting problems can be solved by finding the number of ways to arrange a specified number of distinct elements of a set of a particular size, where the order of these elements matters. Many other counting problems can be solved by finding the number of ways to select a particular number of elements from a set of a particular size, where the order of the elements selected does not matter. For example, in how many ways can we select three students from a group of five students to stand in line for a picture? How many different committees of three students can be formed from a group of four students? In this section we will develop methods to answers questions such as these.

Permutations We begin by solving the first question posed in the introduction to this section, as well as related questions. EXAMPLE 1

Extra � Examples lliiiiiiil

In how many ways can we select three students from a group of five students to stand in line for a picture? In how many ways can we arrange all five of these students in a line for a picture?

Solution: First, note that the order in which we select the students matters. There are five ways to select the first student to stand at the start of the line. Once this student has been selected,

there are four ways to select the second student in the line. After the first and second students have been selected, there are three ways to select the third student in the line. By the product rule, there are 5 . 4 . 3 = 60 ways to select three students from a group of five students to stand in line for a picture. To arrange all five students in a line for a picture, we select the first student in five ways, the second in four ways, the third in three ways, the fourth in two ways, and the fifth in one way. Consequently, there are 5 · 4 · 3 · 2 · 1 = 1 20 ways to arrange all five students in a line for � a picture.

unks � EXAMPLE 2

EXAMPLE 3

Example 1 illustrates how ordered arrangements of distinct objects can be counted. This leads to some terminology. A permutation of a set of distinct objects is an ordered arrangement of these objects. We also are interested in ordered arrangements of some of the elements of a set. An ordered arrangement of r elements of a set is called an r-permutation.

S

S. The ordered arrange­ The number of r-permutations of a set with n elements is denoted by Pen, r). We can find Pen, r) using the product rule. S.

Let = { t , 2, 3 } . The ordered arrangement 3, 1 , 2 is a permutation of ment 3, 2 is a 2-permutation of



Let S {a , b, c} . The 2-permutations of S are the ordered arrangements a , b; a , c; b, a; b, c; c,that a; and c,b . Consequently, there are six 2-permutations of this set with three elements. To see there are always six 2-permutations of a set with three elements, note that there are three =

ways to choose the first element of the arrangement and two ways to choose the second element of the arrangement because it must be different from the first element. By the product rule, it � follows that (3 , 2) = 3 · 2 =

P

6.

356

5 / Counting

5-22

1

n.

We now use the product rule to find a formula for P with ::: r :::

THEOREM l

(n, r) whenever n and r are positive integers

If n is a positive integer and r is an integer with 1 :::

pen, r)

=

n (n

-

l)(n

-

r -permutations of a set with

2)· · · (n r + 1 )

r

::: n, then there are

-

n distinct elements.

Proof: We will use the product rule to prove that this formula is correct. The first element of the permutation can be chosen in ways because there are elements in the set. There are ways to choose the second element of the permutation, because there are elements left in the set after using the element picked for the first position. Similarly, there are 2 ways to choose the third element, and so on, until there are exactly - (r = -r ways to choose the rth element. Consequently, by the product rule, there are

n

n

n-1 n- n-1 n - I) n + 1

n(n - 1)(n - 2) · · · (n - r + 1) r-permutations of the set. Note that P (n, 0) 1 whenever n is a nonnegative integer because there is exactly one way to order zero elements. That is, there is exactly one list with no elements in it, namely the empty list. We now state a useful corollary of Theorem 1.

1, split the sequence into two sequences, either where both have the same number of elements or where one of the sequences has one more element than the other. The problem is reduced to finding the maximum and minimum of each of the two smaller sequences. The solution to the original problem results from the comparison of the separate maxima and minima of the two smaller sequences to obtain the overall maximum and minimum. Let f(n) be the total number of comparisons needed to find the maximum and minimum elements of the sequence with n elements. We have shown that a problem of size n can be reduced into two problems of size n /2, when n is even, using two comparisons, one to compare the maxima of the two sequences and the other to compare the minima of the two sequences. This gives the recurrence relation

f(n) 2f(n/2) 2 when n is even. =

EXAMPLE 3

+

Merge Sort The merge sort algorithm (introduced in Section 4.4) splits a list to be sorted with n items, where n is even, into two lists with n /2 elements each, and uses fewer than n comparisons to merge the two sorted lists of n /2 items each into one sorted list. Consequently, the number of comparisons used by the merge sort to sort a list of n elements is less than M (n), where the function M(n) satisfies the divide-and-conquer recurrence relation

M(n) 2M(n/2) n. =

+

Fast Multiplication ofIntegers Surprisingly, there are more efficient algorithms than the con­ ventional algorithm (described in Section 3. 6) for multiplying integers. One of these algorithms, which uses a divide-and-conquer technique, will be described here. This fast multiplication al­ lInks E::l gorithm proceeds by splitting each of two 2n-bit integers into two blocks, each with n bits. Then, the original multiplication is reduced from the multiplication of two 2n-bit integers to three multiplications of n-bit integers, plus shifts and additions. Suppose that a and b are integers with binary expansions of length 2n (add initial bits of zero in these expansions if necessary to make them the same length). Let

EXAMPLE 4

and Let where

Al (a2n-1 . . . an + lan h , Ao (an - I ' . . alaO)2 , BI (b2n-1 . . . bn + lbn h. Bo (bn - I blboh ·

as

=

=

=

=





.

The algorithm for fast multiplication of integers is based on the fact that ab can be rewritten

476

7 / Advanced Counting Techniques

7-28

The important fact about this identity is that it shows that the multiplication of two 2n -bit integers can be carried out using three multiplications of n-bit integers, together with additions, subtractions, and shifts. This shows that if is the total number of bit operations needed to multiply two n -bit integers, then

f(n)

f(2n) 3f(n) + =

Cn.

3 f(n

The reasoning behind this equation i s as follows. The three multiplications of n-bit integers are carried out using )-bit operations. Each of the additions, subtractions, and shifts uses a constant multiple of n -bit operations, and C n represents the total number of bit operations used � by these operations. EXAMPLE 5

linkS �

5

3. 8

n3

Fast Matrix Multiplication In Example of Section we showed that multiplying two n x n matrices using the definition of matrix multiplication required multiplications and I ) additions. Consequently, computing the product of two n x n matrices in this way requires operations (multiplications and additions). Surprisingly, there are more efficient divide-and-conquer algorithms for multiplying two n x n matrices. Such an algorithm, invented by V. Strassen in reduces the multiplication of two n x n matrices, when n is even, to seven multiplications of two x matrices and additions of(n /2) x matrices. (See [CoLeRiStO l ] for the details of this algorithm.) Hence, if is the number of operations (multiplications and additions) used, it follows that

n 2 (n

-

O(n 3 )

1969, (n/2) (n/2)

f(n) 7f(n/2) + 15n 2 /4

15 f(n)

(n/2)

=

when n is even.

1-5

f(n) af(n/b) + g(n)

As Examples show, recurrence relations of the form = arise in many different situations. It is possible to derive estimates of the size of functions that satisfy such recurrence relations. Suppose that satisfies this recurrence relation whenever n is divisible by Let n = where k is a positive integer. Then

b.

f(n )

f bk , af(n/b) + g(n) = a 2f(n/b2 ) + ag(n/b) + g(n) a 3f(n/b3 ) + a2g(n/b2 ) + ag(n/b) + g(n) k-J ai g(n/bi ). akf(n/bk) + L i =O / bk = I , k-J ai / bi ). akf(1) + L i =O f(n) =

=

=

Because n

f(n )

it follows that

=

We can use this equation for relations.

g(n

to estimate the size of functions that satisfy divide-and-conquer

7-29

7.3 Divide-and-Conquer Algorithms and Recurrence Relations

THEOREM

1

477

f be an increasing function that satisfies the recurrence relation fen) af(njb) + c whenever n is divisible by b, where a :::: 1, b is an integer greater than 1, and c is a positive real number. Then ( ) if a > 1 ' fen) is 1 O (log n ) if a 1. Furthermore, when bk , where k is a positive integer, Let

=

o n iogb a

=

n

=

f(l) + cj(a - 1) and -cj(a - 1). Proof: First let = bk • From the expression for f(n) obtained in the discussion preceding the theorem, with g(n) c, we have where C1

=

C2

=

n

=

k- l aj c akf(1) + c L k- l aj . fen) = akf(1) + L =

j=O

j =O

First consider the case when

a = 1. Then

fen) f(1) + ck. Because n = bk , we have k = 10gb n. Hence, fen) = f(l) + c 10gb n . k+ 1 , for a positive integer k. Because f When is not a power of b, we have bk b 1 k+ it follows that fen) ::::: f(b ) = f(1) + c(k 1) (/(1) + c) + ck ::::: (/(1) + c)is increasing, +Now c logb n. Therefore, in both cases, fen) is O (log n) when a 1. suppose that a > 1. First assume that = b k , where k is a positive integer. From the formula for the sum of terms of a geometric progression (Theorem 1 in Section it follows =


3 .5). b} ?

b) {(a, b) I a =1= b}? d) {(a, b) I a = I }?

c) {(a, b) I a = b + I }?

10.

e) {(a, b) l ab = l }? How many nonzero entries does the matrix representing the relation R on A = {I, 2, 3 , . . . , 1 000} consisting of the first 1 000 positive integers have if R is a) {(a, b) b) {(a, b) c) {(a, b) d) {(a, b) e) {(a, b)

11.

I I I I I

a :'S b}? a = b ± I }? a + b = 1 000}? a + b :'S I OO I }? a =1= O} ?

How can the matrix for R, the complement ofthe relation R, be found from the matrix representing R, when R is a relation on a finite set A?

12.

How can the matrix for R I , the inverse of the relation R, be found from the matrix representing R, when R is a relation on a finite set A?

13.

Let R be the relation represented by the matrix

16.

o

1 0 1

]

Find the matrix representing a) R - 1 . b) R.

[ ] 0 0 1

1 0 1

0 1 0

.

17.

Let R b e a relation o n a set A with n elements. If there are k nonzero entries in MR , the matrix representing R , how many nonzero entries are there i n MR, the matrix representing Ii, the complement of R ?

18.

Draw the directed graphs representing each o f the rela­ tions from Exercise 1 .

19.

Draw the directed graphs representing each of the rela­ tions from Exercise 2 .

20.

Draw the directed graph representing each of the relations from Exercise 3 .

21.

Draw the directed graph representing each ofthe relations from Exercise 4.

22.

Draw the directed graph that represents the relation {(a, a), (a, b), (b, c) , (c, b), (c, d), (d, a), (d, b)} .

In Exercises 23-28 list the ordered pairs in the relations rep­ resented by the directed graphs. 23.

25.

.

=

Find the matrices that represent 2 a) R . b) R 3 . Let R be a relation on a set A with n elements. If there are k nonzero entries in MR , the matrix representing R , how many nonzero entries are there i n MR-l , the matrix representing R - 1 , the inverse of R ?

24.

a

b

g

a

b

c

d

-

1 1

543

, 26.

D

A a

b

c

d

544

27.

8 / Relations

8-26

28.

a

32.

b

33. c

29. 30.

31.

34.

How can the directed graph of a relation R on a finite set A be used to determine whether a relation is asymmetric? How can the directed graph of a relation R on a finite set A be used to determine whether a relation is irreflexive? Determine whether the relations represented by the di­ rected graphs shown in Exercises 23-25 are reflexive, ir­ reflexive, symmetric, antisymmetric, and/or transitive.

35.

36.

Determine whether the relations represented by the di­ rected graphs shown in Exercises 26-28 are reflexive, ir­ reflexive, symmetric, anti symmetric, asymmetric, and/or transitive. Let R be a relation on a set A. Explain how to use the di­ rected graph representing R to obtain the directed graph representing the inverse relation R-' . Let R be a relation on a set A. Explain how to use the di­ rected graph representing R to obtain the directed graph representing the complementary relation R. Show that ifMR is the matrix representing the relation R, then M � l is the matrix representing the relation R n . Given the directed graphs representing two relations, how can the directed graph of the union, intersection, sym­ metric difference, difference, and composition of these relations be found?

8.4 Closures of Relations Introduction A computer network has data centers in Boston, Chicago, Denver, Detroit, New York, and San Diego. There are direct, one-way telephone lines from Boston to Chicago, from Boston to Detroit, from Chicago to Detroit, from Detroit to Denver, and from New York to San Diego. Let R be the relation containing (a , b) if there is a telephone line from the data center in a to that in b. How can we determine if there is some (possibly indirect) link composed of one or more telephone lines from one center to another? Because not all links are direct, such as the link from Boston to Denver that goes through Detroit, R cannot be used directly to answer this. In the language of relations, R is not transitive, so it does not contain all the pairs that can be linked. As we will show in this section, we can find all pairs of data centers that have a link by constructing a transitive relation S containing R such that S is a subset of every transitive relation containing R . Here, S is the smallest transitive relation that contains R . This relation is called the transitive closure of R . In general, let R b e a relation on a set A . R may or may not have some property P, such as reflexivity, symmetry, or transitivity. If there is a relation S with property P containing R such that S is a subset of every relation with property P containing R , then S is called the closure of R with respect to P. (Note that the closure of a relation with respect to a property may not exist; see Exercises 1 5 and 35 at the end of this section.) We will show how reflexive, symmetric, and transitive closures of relations can be found.

Closures The relation R = {( I , 1 ), ( 1 , 2), (2 , 1 ), (3 , 2)} on the set A = { l , 2, 3 } is not reflexive. How can we produce a reflexive relation containing R that is as small as possible? This can be done by adding (2 , 2) and (3 , 3) to R , because these are the only pairs of the form (a , a) that are not in R . Clearly, this new relation contains R . Furthermore, any reflexive relation that contains R must also contain (2, 2) and (3 , 3). Because this relation contains R , is reflexive, and is contained within every reflexive relation that contains R , it is called the reflexive closure of R . A s this example illustrates, given a relation R on a set A, the reflexive closure of R can be formed by adding to R all pairs of the form (a , a) with a E A, not already in R . The

8.4 Closures of Relations 545

8-2 7

addition of these pairs produces a new relation that is reflexive, contains R, and is con­ tained within any reflexive relation containing R . We see that the reflexive closure of R equals R U /),., where /),. = {(a , a) I a E A } is the diagonal relation on A. (The reader should verify this.) EXAMPLE 1

=

What is the reflexive closure of the relation R

{(a , b) I a




b} U {(b, a) I a

>

b}

=

{(a , b) I a # b } .

This last equality follows because R contains all ordered pairs o f positive integers where the first element is greater than the second element and R - 1 contains all ordered pairs of positive ..... integers where the first element is less than the second. Suppose that a relation R is not transitive. How can we produce a transitive relation that contains R such that this new relation is contained within any transitive relation that contains R? Can the transitive closure of a relation R be produced by adding all the pairs of the form (a , c) , where (a , b) and (b, c) are already in the relation? Consider the relation R = {(1 , 3), ( 1 , 4), (2, I), (3 , 2)} on the set { I , 2, 3 , 4}. This relation is not transitive because it does not contain all pairs of the form (a , c) where (a , b) and (b, c) are in R . The pairs of this form not in R are ( 1 , 2), (2, 3), (2, 4), and (3 , I ). Adding these pairs does not produce a transitive relation, because the resulting relation contains (3 , I ) and ( I , 4) but does not contain (3 , 4). This shows that constructing the transitive closure of a relation is more complicated than construct­ ing either the reflexive or symmetric closure. The rest of this section develops algorithms for constructing transitive closures. As will be shown later in this section, the transitive closure of a relation can be found by adding new ordered pairs that must be present and then repeating this process until no new ordered pairs are needed.

546

8-28

8 / Relations

Paths in Directed Gra p hs We will see that representing relations by directed graphs helps in the construction of transitive closures. We now introduce some terminology that we will use for this purpose. A path in a directed graph is obtained by traversing along edges (in the same direction as indicated by the arrow on the edge).

DEFINITION 1

A path from a to b in the directed graph G is a sequence of edges (xo, X l ), (Xl , X2), (X2 ' X3), . . . , (Xn - l , xn ) in G, where n is a nonnegative integer, and Xo = a and Xn = b, that

is, a sequence of edges where the terminal vertex of an edge is the same as the initial vertex in the next edge in the path. This path is denoted by xo , X l , X2 , . . . , Xn - l , Xn and has length n. We view the empty set of edges as a path from a to a . A path of length n � 1 that begins and ends at the same vertex is called a circuit or cycle.

A path in a directed graph can pass through a vertex more than once. Moreover, an edge in a directed graph can occur more than once in a path.

EXAMPLE 3

Which of the following are paths in the directed graph shown in Figure 1 : a , b, e, d; a , e, c, d, b; a , c , b, a, a , b; d, c; c, b, a ; e , b, a , b, a , b, e? What are the lengths of those that are paths? Which of the paths in this list are circuits?

b,

Solution: Because each of (a , b), (b, e) , and (e , d) is an edge, a , b, e, d is a path of length three. Because (c, d) is not an edge, a , e , c , d, b is not a path. Also, b, a, c, b, a, a, b is a path of length six because (b, a ) , (a , c), (c, b), (b, a ) , (a , a ) , and (a , b) are all edges. We see that d, c is a path of length one, because (d, c) is an edge. Also c, b, a is a path of length two, because (c , b) and (b, a ) are edges. All of (e, b), (b, a ) , (a , b), (b, a ) , (a , b), and (b, e) are edges, so e, b, a , b, a , b, e is a path of length six. The two paths b, a, c, b, a , a , b and e , b, a , b, a, b, e are circuits because they begin and 0 and y > 0 ; the set of pairs (x , y), where x > 0 and y :'S 0; the set of pairs (x , y), where x :'S 0 and y > 0 ; and the set of pairs (x , y), where x :'S 0 and y :'S 0 t) the set of pairs (x , y), where x =1= 0 and y =1= 0 ; the set of pairs (x , y), where x = 0 and y =1= 0; and the set of pairs (x , y), where x =1= 0 and y = 0 Which of these are partitions of the set of real numbers? a) the negative real numbers, {O } , the positive real numbers b) the set of irrational numbers, the set of rational numbers c) the set of intervals [k, k + 1 ], k = . . . , - 2, - 1 , 0,

1 , 2, . . .

d) the set of intervals (k, k + I ) , k = . . . , - 2, - 1 , 0,

1 , 2, . . .

e) the set of intervals (k, k + I], k = . . . , - 2, - 1 , 0 ,

1 , 2, . . .

t) the sets { x + n I n E 47.

Z} for all x

E

[0, I )

List the ordered pairs in the equivalence relations pro­ duced by these partitions of {O , 1 , 2, 3, 4 , 5}. a)

the partition consisting of subsets of people living in the same state. 5 1 . Show that the partition ofthe set of bit strings oflength 1 6 . formed by equivalence classes o f bit strings that agree on the last eight bits is a refinement of the partition formed from the equivalence classes of bit strings that agree on the last four bits. In Exercises 5 2 and 5 3 , Rn refers to the family of equivalence relations defined in Example 5. Recall that s Rn t, where s and t are two strings if s = t or s and t are strings with at least n characters that agree in their first n characters. 52. Show that the partition of the set of all bit strings formed by equivalence classes of bit strings with respect to the equivalence relation R4 is a refinement of the partition formed by equivalence classes of bit strings with respect to the equivalence relation R 3 • 53. Show that the partition of the set of all identifiers in C formed by the equivalence classes of identifiers with re­ spect to the equivalence relation R 3 1 is a refinement of the partition formed by equivalence classes of identifiers with respect to the equivalence relation R 8 . (Compilers for "old" C consider identifiers the same when their names agree in their first eight characters, while compilers in standard C consider identifiers the same when their names agree in their first 3 1 characters.) 54. Suppose that RI and Rz are equivalence relations on a set A. Let PI and Pz be the partitions that correspond to R I and R z, respectively. Show that R I � R z if and only if PI is a refinement of Pz . 55. Find the smallest equivalence relation on the set {a , b , c , d , e } containing the relation { (a , b ), (a , c), (d , e) }. 56. Suppose that R I and R z are equivalence relations on the set S. Determine whether each of these combinations of R I and Rz must be an equivalence relation. c) R I tB R2 b) R I n R2 a) R I U R2 57.

{OJ, { I , 2 }, { 3, 4 , 5}

b) {O , I }, { 2, 3} , {4 , 5 } c) {O , I , 2 }, { 3, 4 , 5} d) {O}, { I }, { 2 }, { 3 }, {4}, {5} 48.

List the ordered pairs i n the equivalence relations pro­ duced by these partitions of {a , b , c , d , e , f, g }. a)

{a , b}, {c , d}, {e , f, g }

*58.

Consider the equivalence relation from Example 3 , namely, R = {(x , y ) I x - y is an integer} . a) What is the equivalence class of I for this equivalence relation? b) What is the equivalence class of 1 12 for this equiva­ lence relation? Each bead on a bracelet with three beads is either red, white, or blue, as illustrated in the figure shown.

b) {a}, {b}, {c , d}, {e , f} ' {g } c) {a , b , c , d}, {e , f, g } d) {a , c , e , g }, {b , d}, {f } A partition PI is called a refinement of the partition Pz if every set in PI is a subset of one of the sets in Pz . 49. Show that the partition formed from congruence classes modulo 6 is a refinement of the partition formed from congruence classes modulo 3 . 50. Show that the partition of the set of people living in the United States consisting of subsets of people living in the same county (or parish) and same state is a refinement of

565

Bead I Red

Bead Blue

30

Bead 2 White

Define the relation R between bracelets as: (B I , B2), where B I and B2 are bracelets, belongs to R if and only

566

8-48

8 / Relations

if B2 can be obtained from B 1 by rotating it or rotating it and then reflecting it. a) Show that R is an equivalence relation. b) What are the equivalence classes of R ? *59.

Let R b e the relation o n the set o f all colorings o f the 2 x 2 checkerboard where each of the four squares is col­ ored either red or blue so that (C 1 , C 2), where e l and C2 are 2 x 2 checkerboards with each of their four squares colored blue or red, belongs to R if and only if C2 can be obtained from C 1 either by rotating the checkerboard or by rotating it and then reflecting it.

*64.

65.

66.

a) Show that R is an equivalence relation.

b) What are the equivalence classes of R ? 60.

61. 62. *63.

a ) Let R be the relation o n the set of functions from Z+ to Z+ such that (j, g) belongs to R if and only if f is 8(g) (see Section 3 .2). Show that R is an equivalence relation. b) Describe the equivalence class containing fen ) = n2 for the equivalence relation of part (a).

Determine the number of different equivalence relations on a set with three elements by listing them. Determine the number of different equivalence relations on a set with four elements by listing them. Do we necessarily get an equivalence relation when we

67. *68.

69.

form the transitive closure ofthe symmetric closure ofthe reflexive closure of a relation? Do we necessarily get an equivalence relation when we form the symmetric closure of the reflexive closure of the transitive closure of a relation? Suppose we use Theorem 2 to form a partition P from an equivalence relation R . What is the equivalence rela­ tion R' that results if we use Theorem 2 again to form an equivalence relation from P ? Suppose we use Theorem 2 to form an equivalence rela­ tion R from a partition P . What is the partition P' that results if we use Theorem 2 again to form a partition from R ? Devise an algorithm to find the smallest equivalence re­ lation containing a given relation. Let pen ) denote the number of different equivalence relations on a set with n elements (and by Theorem 2 the number of partitions of a set with n elements). Show that p (n ) satisfies the recurrence relation p (n ) = L�:6 C (n - I , j )p (n - j - I ) and the initial condition p (O) = I . (Note: The numbers p (n ) are called Bell numbers after the American mathematician E. T. Bell.) Use Exercise 68 to find the number of different equiv­ alence relations on a set with n elements, where n is a positive integer not exceeding 1 0.

8.6 Partial Orderings Introduction

LinkS� DEFINITION 1

We often use relations to order some or all of the elements of sets. For instance, we order words using the relation containing pairs of words (x , y), where x comes before y in the dictionary. We schedule projects using the relation consisting of pairs (x , y), where x and y are tasks in a project such that x must be completed before y begins. We order the set of integers using the relation containing the pairs (x , y), where x is less than y. When we add all of the pairs of the form (x , x) to these relations, we obtain a relation that is reflexive, anti symmetric, and transitive. These are properties that characterize relations used to order the elements of sets.

A relation R on a set S is called a partial ordering or partial order if it is reflexive, antisym­ metric, and transitive. A set S together with a partial ordering R is called a partially ordered set, or poset, and is denoted by (S, R). Members of S are called elements of the poset. We give examples of po sets in Examples 1-3 .

EXAMPLE l Extra � Examples �

Show that the "greater than o r equal" relation C::: ) is a partial ordering o n the set o f integers.

Solution: Because a ::: a for every integer a, ::: is reflexive. If a ::: b and b ::: a, then a = b. Hence, ::: is antisymmetric. Finally, ::: is transitive because a ::: b and b ::: c imply that a ::: c. It follows that ::: is a partial ordering on the set of integers and (Z, ::: ) IS a ��



8-49

8.6 Partial Orderings

567

EXAMPLE 2

The divisibility relation 1 is a partial ordering on the set of positive integers, because it is reflexive, antisymmetric, and transitive, as was shown in Section 8. 1 . We see that (Z+ , I ) is a ... poset. Recall that (Z+ denotes the set of positive integers.)

EXAMPLE 3

Show that the inclusion relation � is a partial ordering on the power set of a set S .

Solution: Because A � A whenever A is a subset of S , � is reflexive. It is anti symmetric because A � B and B � A imply that A = B . Finally, � is transitive, because A � B and B � C imply ... that A � C . Hence, � is a partial ordering on P (S), and (P (S), �) is a po set. Example 4 illustrates a relation that is not a partial ordering.

EXAMPLE 4

Let R be the relation on the set of people such that x R y if x and y are people and x is older than y. Show that R is not a partial ordering.

Extra � ExamPles ""

Solution: Note that R is anti symmetric because if a person x is older than a person y, then y is not older than x . That is, if x R y, then y R x . The relation R is transitive because if person x

is older than person y and y is older than person z, then x is older than z. That is, if x R y and y R z, then x R z. However, R is not reflexive, because no person is older than himself or herself. ... That is, x R x for all people x . It follows that R is not a partial ordering. In different po sets different symbols such as �, � , and I , are used for a partial ordering. However, we need a symbol that we can use when we discuss the ordering relation in an arbitrary poset. Customarily, the notation a � b is used to denote that (a , b) E R in an arbitrary poset (S, R). This notation is used because the "less than or equal to" relation on the set of real numbers is the most familiar example of a partial ordering and the symbol � is similar to the � symbol. (Note that the symbol � is used to denote the relation in any poset, not just the "less than or equals" relation.) The notation a -< b denotes that a � b, but a =1= b. Also, we say "a is less than b" or "b is greater than a " if a -< b. When a and b are elements of the poset (S, � ), it is not necessary that either a � b or b � a . For instance, in (P(Z), �), { l , 2 } is not related to { I , 3 } , and vice versa, because neither set is contained within the other. Similarly, in (Z+ , I ), 2 is not related to 3 and 3 is not related to 2, because 2 [ 3 and 3 [ 2. This leads to Definition 2.

DEFINITION 2

EXAMPLE 5

The elements a and b ofa poset (S, � ) are called comparable if either a � b or b � a . When a and b are elements of S such that neither a � b nor b � a, a and b are called incomparable.

In the poset (Z+ , I ), are the integers 3 and 9 comparable? Are 5 and 7 comparable?

Solution: The integers 3 and 9 are comparable, because 3 1 9. The integers 5 and 7 are incom­ ... parable, because 5 [ 7 and 7 [ 5 . The adjective "partial" is used to describe partial orderings because pairs of elements may be incomparable. When every two elements in the set are comparable, the relation is called a

total ordering.

568

8 / Relations

DEFINITION 3

8-50

If (S, �) is a poset and every two elements of S are comparable, S is called a totally ordered or linearly ordered set, and � is called a total order or a linear order. A totally ordered set is also called a chain. - oceanography, so we examine the left child of v. This left child exists, so we set v, the vertex under examination, to this left child. At this step, we also have v =I null and label( v) = metereology < oceanography, so we try to examine the right child of v. However, this right child does not exist, so we add a new vertex as the right child of v (which at this point is the vertex with the key metereology) and we set v : = null. We now exit the while loop because v = null. Because the root of T is not null and v = null, we use the else if statement at the end of the algorithm to � label our new vertex with the key oceanography.

698

/ 0- / 6

1 0 / Trees

Unlabeled vertices circled

FIGURE 2

Adding Unlabeled Vertices to Make a Binary Search Tree Full.

We will now determine the computational complexity o f this procedure. Suppose we have a binary search tree T for a list of n items. We can form a full binary tree U from T by adding unlabeled vertices whenever necessary so that every vertex with a key has two children. This is illustrated in Figure 2. Once we have done this, we can easily locate or add a new item as a key without adding a vertex. The most comparisons needed to add a new item is the length of the longest path in U from the root to a leaf. The internal vertices of U are the vertices of T . It follows that U has n internal vertices. We can now use part (ii) of Theorem 4 in Section 1 0. 1 to conclude that U has n + leaves. Using Corollary 1 of Section 1 0. 1 , we see that the height of U is greater than or equal to h = flog(n + 1 )1 . Consequently, it is necessary to perform at least flog(n + 1 )1 comparisons to add some item. Note that if U is balanced, its height is flog(n + 1 )1 (by Corollary 1 of Section 1 0. 1 ) . Thus, if a binary search tree is balanced, locating or adding an item requires no more than flog(n + 1 )1 comparisons. A binary search tree can become unbalanced as items are added to it. Because balanced binary search trees give optimal worst-case complexity for binary searching, algorithms have been devised that rebalance binary search trees as items are added. The interested reader can consult references on data structures for the description of such algorithms.

I

unkS�

EXAMPLE 3

Extra � Examples "'"

Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. For instance, a binary search tree can be used to locate items based on a series of comparisons, where each comparison tells us whether we have located the item, or whether we should go right or left in a subtree. A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree. The possible solutions of the problem correspond to the paths to the leaves of this rooted tree. Example 3 illustrates an application of decision trees. Suppose there are seven coins, all with the same weight, and a counterfeit coin that weighs less than the others. How many weighings are necessary using a balance scale to determine which of the eight coins is the counterfeit one? Give an algorithm for finding this counterfeit coin.

Solution: There are three possibilities for each weighing on a balance scale. The two pans can have equal weight, the first pan can be heavier, or the second pan can be heavier. Consequently, the decision tree for the sequence of weighings is a 3-ary tree. There are at least eight leaves in

J O- 1 7

1 0.2 Applications of Trees

Lighter

CD @ b

FIGURE 4

a AB => Ab => Cab => cbab.

B ackus-Naur Form There is another notation that is sometimes used to specify a type 2 grammar, called the Backus-Naur form (BNF), after John Backus, who invented it, and Peter Naur, who refined it for use in the specification of the programming language ALGOL. (Surprisingly, a notation quite similar to the Backus-Naur form was used approximately 2500 years ago to describe the gram­ mar of Sanskrit.) The Backus-Naur form is used to specify the syntactic rules of many computer languages, including Java. The productions in a type 2 grammar have a single nonterminal sym­ bol as their left-hand side. Instead of listing all the productions separately, we can combine all those with the same nonterminal symbol on the left-hand side into one statement. Instead of using the symbol -+ in a production, we use the symbol : : = . We enclose all nonterminal symbols in brackets, ( ) , and we list all the right-hand sides of productions in the same statement, separating them by bars. For instance, the productions A -+ Aa, A -+ a, and A -+ AB can be combined into (A) : : = (A)a I a I (A) ( B ) . Example 1 3 illustrates how the Backus-Naur form i s used to describe the syntax of pro­ gramming languages. Our example comes from the original use of Backus-Naur form in the description of ALGOL 60.

J O H N BAC K U S ( BO R N 1 92 4 )

John Backus was born in Philadelphia and grew up in Wilmington, Delaware. He attended the Hill School in Pottstown, Pennsylvania. He needed to attend summer school every year because he disliked studying and was not a serious student. But he enjoyed spending his summers in New Hampshire where he attended summer school and amused himself with summer activities, including sailing. He obliged his father by enrolling at the University of Virginia to study chemistry. But he quickly decided chemistry was not for him, and in 1 943 he entered the army, where he received medical training and worked in a neurosurgery ward in an army hospital. Ironically, Backus was soon diagnosed with a bone tumor in his skull and was fitted with a metal plate. His medical work in the army convinced him to try medical school, but he abandoned this after nine months because he disliked the rote memorization required. After dropping out of medical school, he entered a school for radio technicians because he wanted to build his own high fidelity set. A teacher in this school recognized his potential and asked him to help with some mathematical calculations needed for an article in a magazine. Finally, Backus found what he was interested in: mathematics and its applications. He enrolled at Columbia University, from which he received both bachelor's and master's degrees in mathematics. Backus joined IBM as a programmer in 1 950. He participated in the design and development of two of IBM's early computers. From 1 954 to 1 958 he led the IBM group that developed FORTRAN. Backus became a staff member at the IBM Watson Research Center in 1 958. He was part of the committees that designed the programming language ALGOL, using what is now called the Backus-Naur form for the description of the syntax of this language. Later, Backus worked on the mathematics of families of sets and on a functional style of programming. Backus became an IBM Fellow in 1 963 , and he received the National Medal of Science in 1 974 and the prestigious Turing Award from the Association of Computing Machinery in 1 977.

P E T E R NAU R ( BO R N 1 92 8 )

Peter Naur was born in Frederiksberg, near Copenhagen. As a boy he became interested in astronomy. Not only did he observe heavenly bodies, but he also computed the orbits of comets and asteroids. Naur attended Copenhagen University, receiving his degree in 1 949. He spent 1 950 and 1 95 1 in Cambridge, where he used an early computer to calculate the motions of comets and planets. After returning to Denmark he continued working in astronomy but kept his ties to computing. In 1 955 he served as a consultant to the building of the first Danish computer. In 1 959 Naur made the switch from astronomy to computing as a full-time activity. His first job as a full-time computer scientist was participating in the development of the programming language ALGOL. From 1 960 to 1 967 he worked on the development of compilers for ALGOL and COBOL. In 1 969 he became professor of computer science at Copenhagen University, where he has worked in the area of programming methodology. His research interests include the design, structure, and performance of computer programs. Naur has been a pioneer in both the areas of software architecture and software engineering. He rejects the view that computer programming is a branch of mathematics and prefers that computer science be called datalogy.

1 2. 1 Languages and Grammars

12-9

EXAMPLE 13

Exam=�

793

In ALGOL 60 an identifier (which is the name of an entity such as a variable) consists of a string of alphanumeric characters (that is, letters and digits) and must begin with a letter. We can use these rules in Backus-Naur to describe the set of allowable identifiers:

(identifier) : : = (letter) I (identifier) (letter) I (identifier) (digit) (letter) : : = a I b I . . . I y I z the ellipsis indicates that all 26 letters are included (digit) : : = 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 I 9 For example, we can produce the valid identifier x 99a by using the first rule to replace (identifier) by (identifier) (letter) , the second rule to obtain (identifier) a , the first rule twice to obtain (identifier) (digit) (digit) a, the third rule twice to obtain (identifier) 99a , the first rule to obtain .... (letter) 99a, and finally the second rule to obtain x 99a .

EXAMPLE 14

What is the Backus-Naur form of the grammar for the subset of English described in the introduction to this section?

Solution: The Backus-Naur form of this grammar is (sentence) : : = (noun phrase) (verb phrase) (noun phrase) : : = (article) (adjective) (noun) I (article) (noun) (verb phrase) : : = (verb) (adverb) I (verb) (article) : : = a I the (adjective) : : = large I hungry (noun) : : = rabbit I mathematician (verb) : : = eats I hops (adverb) : : = quickly I wildly EXAMPLE 15

Give the Backus-Naur form for the production of signed integers in decimal notation. (A signed integer is a nonnegative integer preceded by a plus sign or a minus sign.)

Solution: The Backus-Naur form for a grammar that produces signed integers is (signed integer) : : = (sign) (integer) (sign) : : = + I (integer) : : = (digit) I (digit) (integer) (digit) : : = 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 I 9 The Backus-Naur form, with a variety of extensions, is used extensively to specify the syntax of programming languages, such as Java and LISP; database languages, such as SQL; and markup languages, such as XML. Some extensions of the Backus-Naur form that are commonly used in the description of programming languages are introduced in the preamble to Exercise 34 at the end of this section.

Exercises Exercises 1-3 refer to the grammar with start symbol sen­ tence, set of terminals T = {the, sleepy, happy, tortoise, hare, passes, runs, quickly, slowly } , set of nonterminals N = { noun

phrase, transitive verb phrase, intransitive verb phrase, article, adjective, noun, verb, adverb } , and productions: sentence ... noun phrase noun phrase

transitive verb phrase

sentence ... noun phrase intransitive verb phrase noun phrase ... article adjective noun noun phrase ... article noun transitive verb phrase ... transitive verb intransitive verb phrase ... intransitive verb adverb intransitive verb phrase ... intransitive verb article ... the

1 2 / Modeling Computation

794

adjective � sleepy adjective � happy noun � tortoise noun � hare transitive verb � passes intransitive verb � runs adverb � quickly adverb � slowly 1. Use the set of productions to show that each of these sen­ tences is a valid sentence.

1 2- 1 0

10.

a ) Show that the grammar G l given in Example 6 gen­ erates the set {O'" I n 1 m , n

=

0, 1 , 2 , . . . } .

b ) Show that the grammar G2 in Example 6 generates

the same set. 2 2 2 - 1 1 . Construct a derivation of 0 1 2 in the grammar given in Example 7 . *12. Show that the grammar given in Example 7 generates the set {on I n 2 n I n = 0 , 1 , 2 , . . . } . 1 3 . Find a phrase-structure grammar for each o f these languages.

a) b) c) d)

2. Find five other valid sentences, besides those given in Exercise 1 .

the set consisting of the bit strings 0, 1, and 1 1 the set of bit strings containing only I s the set o f bit strings that start with 0 and end with 1 the set of bit strings that consist of a 0 followed by an even number of 1 s 14. Find a phrase-structure grammar for each of these languages.

3 . Show that the hare runs the sleepy tortoise i s not a valid sentence.

a) the set consisting of the bit strings 1 0, 0 1 , and 1 0 1 b ) the set o f bit strings that start with 0 0 and end with

a) b) c) d)

the happy hare runs the sleepy tortoise runs quickly the tortoise passes the hare the sleepy hare passes the happy tortoise

one or more 1 s

4. Let G = ( V, T, S, P) be the phrase-structure grammar with V = {O, I , A , S}, T = {O, I }, and set of produc­ tions P consisting of S � I S, S � OOA, A � OA, and A � O.

a) Show that 1 1 1 000 belongs to the language generated by G .

b) Show that 1 1 00 1 does not belong to the language gen­

c) the set of bit strings consisting of an even number of I s followed by a final 0

d) the set of bit strings that have neither two consecutive Os nor two consecutive 1 s

*15. Find a phrase-structure grammar for each of these languages.

a) the set of all bit strings containing an even number of

erated by G .

Os and no I s

c) What is the language generated b y G?

b) the set of all bit strings made up of a 1 followed by an

5. Let G = ( V, T, S , P ) be the phrase-structure g rammar with V = {O, 1 , A , B , S}, T = {O, I }, and set of pro­ ductions P consisting of S � OA, S � l A, A � OB, B � lA, B � 1 .

odd number of Os

c) the set of all bit strings containing an even number of Os and an even number of 1 s

d) the set of all strings containing 1 0 or more Os and no

a) Show that 1 0 1 0 1 belongs to the language generated

by G . b) Show that 1 0 1 1 0 does not belong t o the language gen­ erated by G . c) What i s the language generated by G? * 6. Let V = IS, A, B , a , b} and T = {a, b } . Find the lan­ guage generated by the g rammar ( V, T, S, P) when the set P of productions consists of

a) b) c) d) e)

� AB, � AB, � AB, � AA, B � b. S � AB,

S S S S

A � ab, B � bb. S � a A , A � a, B � ba. S � A A , A � a B , A � ab, B � b. S � B , A � aaA, A � aa, B � bB,

A � a Ab, B � bBa, A � A, B � A. 3 3 7. Construct a derivation of 0 1 using the grammar given in Example 5 . 8. Show that the g rammar given in Example 5 generates the set {on I n I n = 0 , 1 , 2 , . . . } . 2 9 . a) Construct a derivation of 0 1 4 using the g rammar G 1 in Example 6. b) Construct a derivation of 02 1 4 using the grammar G2 in Example 6.

Is

e) the set o f all strings containing more O s than I s

16.

1 7.

18.

19.

f) the set of all strings containing an equal number of Os and l s g) the set of all strings containing an unequal number of Os and I s Construct phrase-structure grammars to generate each of these sets. a) W i n ?: O} b) { I on I n ?: O} c) { ( l l )n I n ?: O} Construct phrase-structure grammars to generate each of these sets. a) W i n ?: O} b) W O I n ?: O} c) { (ooo)n I n ?: O} Construct phrase-structure grammars to generate each of these sets. a) { O I 2n I n ?: O} b) W 1 2n I n ?: O} c) {on l m on 1 m ?: O and n ?: O} Let V = IS, A, B , a, b} and T = {a , b}. Determine whether G = ( V, T, S, P) is a type 0 grammar but not a type 1 grammar, a type 1 grammar but not a type 2

12. 1

/ 2- / /

grammar, or a type 2 grammar but not a type if P , the set of productions, is

a) b) c) d) e) t)

g) h) i) j)

S S S S S S S S S S

� � � � � � � � � �

derivation trees for

a) bebba. b) bbbebba. c) beabbbbbeb. *25. Use top-down parsing to determine whether each of the following strings belongs to the language generated by the grammar in Example 12.

*2 1. Let G l and G 2 be context-free grammars, generating the languages L ( G 1 ) and L ( G 2), respectively. Show that there is a context-free grammar generating each of these sets.

*26. Use bottom-up parsing to determine whether the strings in Exercise 25 belong to the language generated by the grammar in Example 12. 27. Construct a derivation tree for - 1 09 using the grammar given in Example 1 5 . 28. a) Explain what the productions are in a grammar if the Backus-Naur form for productions is as follows:

(expression)

b) L(G dL(G2)

22. Find the strings constructed using the derivation trees shown here. sentence

/ �

a

adjective

(variable)

29.

verb phrase

/I� I I I

article

noun

large mathematician

/ \ I I

verb

adverb

hops

wildly

30.

/ "'"

Find a derivation tree for (x

I

+

/� /� integer

digit

9

digit

integer

8

digit

I

I

7

23. Construct derivation trees for the sentences in Exercise

1.

24. Let G be the grammar with V = { a , b , e , S } ; T = { a , b , e } ; starting symbol S; and productions S � abS,

* y) + x in this grammar.

a)

Construct a phrase-structure grammar for the set of all fractions of the form alb, where a is a signed integer in decimal notation and b is a positive integer. What is the Backus-Naur form for this grammar? Construct a derivation tree for +3 1 1 / 1 7 in this grammar.

Construct a phrase-structure grammar that generates all signed decimal numbers, consisting of a sign, either + or -; a nonnegative integer; and a decimal fraction that is either the empty string or a decimal point fol­ lowed by a positive integer, where initial zeros in an integer are allowed. b) Give the Backus-Naur form of this grammar. c) Construct a derivation tree for -3 1 .4 in this grammar.

b) c)

integer

«(expression} ) I (expression) + (expression) I (expression) * (expression) I (variable) : := x I y ::=

b) a)

signed integer

sign

b) abab d) bbbeba

a) baba c) ebaba

20. A palindrome is a string that reads the same backward as it does forward, that is, a string w, where w = w R , where w R is the reversal of the string w . Find a context-free grammar that generates the set of all palindromes over the alphabet {O, I } .

noun phrase

795

S � beS, S � bbS, S � a , and S � eb. Construct

3 grammar

aAB , A � Bb, B � A. aA, A � a , A � b. ABa , AB � a. ABA , A � aB , B � abo bA, A � B, B � a. aA, aA � B , B � aA , A � b. bA , A � b, S � A. AB, B � aAb, aAb � b. aA , A � bB , B � b, B � A . A, A � B, B � A.

a) L( G l ) U L( G 2) c) L( G 1 )*

Languages and Grammars

3 1 . Give production rules in Backus-Naur form for an iden­ tifier if it can consist of a) one or more lowercase letters. b) at least three but no more than six lowercase letters. c) one to six uppercase or lowercase letters beginning with an uppercase letter. d) a lowercase letter, followed by a digit or an under­ score, followed by three or four alphanumeric charac­ ters (lower or uppercase letters and digits). 32. Give production rules in Backus-Naur form for the name of a person if this name consists of a first name, which is a string of letters, where only the first letter is uppercase; a middle initial; and a last name, which can be any string of letters. 33. Give production rules in Backus-Naur form that gener­ ate all identifiers in the C programming language. In C

796

1 2 / Modeling Computation

12-12

an identifier starts with a letter or an underscore ( ) that is followed by one or more lowercase letters, uppercase letters, underscores, and digits. _



Several extensions to Backus-Naur form are commonly used to define phrase-structure grammars. In one such extension, a question mark (?) indicates that the symbol, or group of symbols inside parentheses, to its left can appear zero or once (that is, it is optional), an asterisk (*) indicates that the symbol to its left can appear zero or more times, and a plus (+) indi­ cates that the symbol to its left can appear one or more times. These extensions are part of extended Backus-Naur form (EBNF), and the symbols ?, * , and + are called metacharac­ ters. In EBNF the brackets used to denote nonterminals are usually not shown. 34. Describe the set of strings defined by each of these sets of productions in EBNF.

a) string : : = L + D?L+

L ::= a I b I c D ::= 0 1 1 b) string : : = sign D+ I D+ sign ::= + 1 D ::= 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I c) string : : = L*(D+)? L* L ::= x I y D ::= 0 1 1

Give production rules in extended Backus-Naur form for identifiers in the C programming language (see Exercise 33) . 38. Describe how productions for a grammar in extended Backus-Naur form can be translated into a set of pro­ ductions for the grammar in Backus-Naur form. This is the Backus-Naur form that describes the syntax of expressions in postfix (or reverse Polish) notation.

(expression) : : = (term) I (term) (term) (addOperator) (addOperator) : : = + I (term) : : = ljactor) I ljactor) ljactor) (muIOperator) (muIOperator) : : = * I I

ljactor) : : = (identifier) I (expression) (identifier) : : = a I b I . . . I z

39. For each of these strings, determine whether it is gener­ ated by the grammar given for postfix notation. If it is, find the steps used to generate the string

8I9

35. Give production rules in extended Backus-Naur form that generate all decimal numerals consisting of an optional sign, a nonnegative integer, and a decimal fraction that is either the empty string or a decimal point followed by an optional positive integer optionally preceded by some number of zeros. 36. Give production rules in extended Backus-Naur form that generate a sandwich if a sandwich consists of a lower slice of bread; mustard or mayonnaise; optional lettuce; an op­ tional slice of tomato; one or more slices of either turkey, chicken, or roast beef (in any combination); optionally some number of slices of cheese; and a top slice of bread.

12.2

37.

a) abc*+ b) xy++ d) wxyz-*I e) ade- *

c) xy-z*

40. Use Backus-Naur form to describe the syntax of expres­ sions in infix notation, where the set ofoperators and iden­ tifiers is the same as in the BNF for postfix expressions given in the preamble to Exercise 39, but parentheses must surround expressions being used as factors. 41. For each of these strings, determine whether it is gener­ ated by the grammar for infix expressions from Exercise 40. If it is, find the steps used to generate the string.

a) x + y + z c) m * (n + p) e) (m + n) * (p - q)

b) alb + cld d) + m - n + p - q

42. Let G be a grammar and let R be the relation contain­ ing the ordered pair (w o , W I ) if and only if W I is directly derivable from Wo in G . What is the reflexive transitive closure of R?

Finite-State Machines with Output Introduction

unks�

Many kinds of machines, including components in computers, can be modeled using a structure called a finite-state machine. Several types of finite-state machines are commonly used in models. All these versions of finite-state machines include a finite set of states, with a designated starting state, an input alphabet, and a transition function that assigns a next state to every state and input pair. Finite-state machines are used extensively in applications in computer science and data networking. For example, finite-state machines are the basis for programs for spell checking, grammar checking, indexing or searching large bodies of text, recognizing speech, transforming text using markup languages such as XML and HTML, and network protocols that specify how computers communicate.

12- 1 3

1 2.2 Finite-State Machines with Output

797

TABLE 1 State Table for a Vending Machine.

Next State

Output

State

5

10

Input 25

0

R

So

SI

S2

Ss

So

So

S3

S4

S6

S2

S2

SI

S2

S3

S4

Ss

S6

S2

S4 Ss

S6

S6

S3

Ss

S6 S6

S3

S6

Ss

S6

S6

S6

S6

S6

SI

S4 So

SI

S3

S4 Ss

So

5

10

n n

n n

n n n

Input 25

0

R

n n

n n

n n

n

5

n

n

n n

10

n

15

n

n n

n

5

20

n

n

5

10

25

OJ

AJ

In this section, we will study those finite-state machines that produce output. We will show how finite-state machines can be used to model a vending machine, a machine that delays input, a machine that adds integers, and a machine that determines whether a bit string contains a specified pattern. Before giving formal definitions, we will show how a vending machine can be modeled. A vending machine accepts nickels (5 cents), dimes ( 1 0 cents), and quarters (25 cents). When a total of 30 cents or more has been deposited, the machine immediately returns the amount in excess of 30 cents. When 30 cents has been deposited and any excess refunded, the customer can push an orange button and receive an orange juice or push a red button and receive an apple juice. We can describe how the machine works by specifying its states, how it changes states when input is received, and the output that is produced for every combination of input and current state. The machine can be in any of seven different states Sj , i = 0, 1 , 2, . . . , 6, where Sj is the state where the machine has collected 5i cents. The machine starts in state So, with 0 cents received. The possible inputs are 5 cents, 1 0 cents, 25 cents, the orange button ( 0), and the red button (R). The possible outputs are nothing (n ) , 5 cents, 1 0 cents, 1 5 cents, 20 cents, 25 cents, an orange juice, and an apple juice. We illustrate how this model ofthe machine works with this example. Suppose that a student puts in a dime followed by a quarter, receives 5 cents back, and then pushes the orange button for an orange juice. The machine starts in state So. The first input is 1 0 cents, which changes 25, n

0, orange jui ce

FIGURE 1

A Vending Machine.

25, 5

25, 20

798

1 2 / Modeling Computation

12- 1 4

the state of the machine to S2 and gives no output. The second input is 25 cents. This changes the state from S2 to S6, and gives 5 cents as output. The next input is the orange button, which changes the state from S6 back to So (because the machine returns to the start state) and gives an orange juice as its output. We can display all the state changes and output of this machine in a table. To do this we need to specify for each combination of state and input the next state and the output obtained. Table 1 shows the transitions and outputs for each pair of a state and an input. Another way to show the actions of a machine is to use a directed graph with labeled edges, where each state is represented by a circle, edges represent the transitions, and edges are labeled with the input and the output for that transition. Figure 1 shows such a directed graph for the vending machine.

Finite-State Machines with Outputs We will now give the formal definition of a finite-state machine with output.

DEFINITION 1

A finite-state machine M = (S, I, 0 , f, g, so) consists of a finite set S of states, a finite input alphabet /, a finite output alphabet 0, a transition function fthat assigns to each state and input pair a new state, an outputfunction g that assigns to each state and input pair an output, and an initial state so.

Let M = (S, 1, 0 , J, g, so) be a finite-state machine. We can use a state table to represent the values of the transition function f and the output function 9 for all pairs of states and input. We previously constructed a state table for the vending machine discussed in the introduction to this section. EXAMPLE 1

The state table shown in Table 2 describes a finite-state machine with S = {so , SI , S2 , S3 }, I = {O, I } , and 0 = {O, I } . The values of the transition function f are displayed in the first two columns, and the values of the output function g are displayed in the last two columns. .... Another way to represent a finite-state machine is to use a state diagram, which is a directed graph with labeled edges. In this diagram, each state is represented by a circle. Arrows labeled with the input and output pair are shown for each transition.

EXAMPLE 2

Construct the state diagram for the finite-state machine with the state table shown in Table 2.

Solution: The state diagram for this machine is shown in Figure 2. EXAMPLE 3

Construct the state table for the finite-state machine with the state diagram shown in Figure 3 .

Solution: The state table for this machine i s shown i n Table 3 . An input string takes the starting state through a sequence o f states, as determined by the transition function. As we read the input string symbol by symbol (from left to right), each input symbol takes the machine from one state to another. Because each transition produces an output, an input string also produces an output string. Suppose that the input string is x = XIX2 Xk. Then, reading this input takes the machine from state So to state SI , where SI = f(so , X I ), then to state S2 , where S2 = f(SI , X2), and so on, .



.

1 2.2 Finite-State Machines with Output

12-15

TABLE 2 Input

State

0

So

SI

S2

SI

SI

S3

S3 S2

Start

g

f 1 So

So

S2

SI

Input

799

0

1

1

0

1

1

0

1

0

0

I, I

The 'State Diagram for the Finite-State Machine Shown in Table 2.

FIGURE 2

with Sj = !(Sj - l , Xj ) for j = 1 , 2, . . . , k, ending at state Sk = !(Sk- I , Xk). This sequence of transitions produces an output string YIY2 . . . Yk. where YI = g(so , X I ) is the output correspond­ ing to the transition from So to SI , Y2 = g(si , X2) is the output corresponding to the transition from si tO S2, and so on. In general, Yj = g(Sj _ l , Xj ) for j = 1 , 2, . . . , k. Hence, we can extend the definition of the output function 9 to input strings so that g(x) = y, where y is the output corresponding to the input string x . This notation is useful in many applications. EXAMPLE 4

Find the output string generated by the finite-state machine in Figure 3 if the input string is

10101 1 .

Solution: The output obtained is 00 1 000. The successive states and outputs are shown in �

��

We can now look at some examples of useful finite-state machines. Examples 5, 6, and 7 illustrate that the states of a finite-state machine give it limited memory capabilities. The states can be used to remember the properties of the symbols that have been read by the machine. However, because there are only finitely many different states, finite-state machines cannot be used for some important purposes. This will be illustrated in Section 1 2.4. EXAMPLE 5

An important element in many electronic devices is a unit-delay machine, which produces as output the input string delayed by a specified amount of time. How can a finite-state machine be constructed that delays an input string by one unit of time, that is, produces as output the bit string OXIX2 . . . Xk- I given the input bit string X I X2 . . . Xk? 0, 1

TABLE 3 g

'f

State

Start -----i�

So 1,0

FIGURE 3

A Finite-State Machine.

Input

Input 0 SI

1 S3

SI

SI

S2

S3

SI

So

S2 S4

S3 S3

S4

S4

0

1

1

0

1

1

0

0

0

0

0

0

800

1 2 / Modeling Computation

1 2- 1 6

TABLE 4 Input

1

0

1

0

1

1

-

State

So

S3

SI

S2

S3

So

S3

Output

0

0

1

0

0

0

-

FIGURE 4

A Unit-Delay Machine.

Solution: A delay machine can be constructed that has two possible inputs, namely, 0 and 1 . The machine must have a start state So. Because the machine has to remember whether the previous input was a 0 or a I , two other states S I and S2 are needed, where the machine is in state S I if the previous input was 1 and in state S2 if the previous input was O. An output of 0 is produced for the initial transition from So . Each transition from SI gives an output of 1 , and each transition from S2 gives an output of o. The output corresponding to the input of a string XI . . . Xk is the string that begins with 0, followed by XI . followed by X2 , , ending with Xk- I . The state diagram for .... this machine is shown in Figure 4. •

EXAMPLE 6





Produce a finite-state machine that adds two integers using their binary expansions.

Solution: When (xn . . . XI xoh and (yn . . . YIYoh are added, the following procedure (as described in Section 3 .6) is followed. First, the bits Xo and Yo are added, producing a sum bit Zo and a carry bit Co. This carry bit is either 0 or 1 . Then, the bits X I and YI are added, together with the carry Co . This gives a sum bit Z I and a carry bit CI . This procedure is continued until the nth stage, where Xn , Yn , and the previous carry Cn - I are added to produce the sum bit Zn and the carry bit Cn , which is equal to the sum bit Zn - I .

. A finite-state machine to carry out this addition can be constructed using just two states. For simplicity we assume that both the initial bits Xn and Yn are 0 (otherwise we have to make special arrangements concerning the sum bit Z n + I ). The start state So is used to remember that the previous carry is 0 (or for the addition of the rightmost bits). The other state, SJ , is used to remember that the previous carry is 1 . Because the inputs to the machine are pairs of bits, there are four possible inputs. We represent these possibilities by 00 (when both bits are 0), 0 1 (when the first bit is 0 and the second is 1 ), 1 0 (when the first bit is 1 and the second is 0), and 1 1 (when both bits are 1 ). The transitions arid the outputs are constructed from the sum of the two bits represented by the input and the carry represented by the state. For instance, when the machine is in state SI and receives 01 as input, the next state is SI and the output is 0, because the sum that arises is .... 0 + 1 + 1 = ( 1 0)2 . The state diagram for this machine is shown in Figure 5. 01, 1

01, 0

1 1, 1

00, 0

1 0, 1

FIGURE 5

1 0, 0

A Finite-State Machine for Addition.

1 2.2 Finite-State Machines with Output

1 2- 1 7

801

1, 1

0, 0

FIG URE 6 A Finite-State Machine That Gives an Output of 1 If and Only If the Input String Read So Far Ends with 1 1 1 .

EXAMPLE 7

In a certain coding scheme, when three consecutive 1 s appear in a message, the receiver of the message knows that there has been a transmission error. Construct a finite-state machine that gives a 1 as its current output bit if and only if the last three bits received are all I s.

Solution: Three states are needed in this machine. The start state So remembers that the previous input value, if it exists, was not a 1 . The state Sl remembers that the previous input was a 1 , but the input before the previous input, if it exists, was not a 1 . The state S2 remembers that the previous two inputs were 1 s. An input of 1 takes So to SI , because now a I , and not two consecutive 1 s, has been read; it takes SI to S2 , because now two consecutive I s have been read; and it takes S2 to itself, because at least two consecutive 1 s have been read. An input of 0 takes every state to So, because this breaks up any string of consecutive 1 s. The output for the transition from S2 to itself when a 1 is read is 1 , because this combination of input and state shows that three consecutive 1 s have been read. All other outputs are O. The state diagram of this machine is shown in Figure 6. � The final output bit of the finite-state machine we constructed in Example 7 is 1 if and only if the input string ends with 1 1 1 . Because of this, we say that this finite-state machine recognizes the set of bit strings that end with 1 1 1 . This leads us to Definition 2.

DEFINITION 2

Let M = (S, I, 0 , f, g, so) be a finite-state machine and L £ I*. We say that M recognizes (or accepts) L if an input string x belongs to L if and only if the last output bit produced by M when given x as input is a 1 .

TYPES OF FINITE-STATE MACHINES Many different kinds of finite-state machines have been developed to model computing machines. In this section we have given a definition of one type of finite-state machine. In the type of machine introduced in this section, outputs correspond to transitions between states. Machines of this type are known as Mealy machines, because they were first studied by G. H. Mealy in 1 955. There is another important type of finite­ state machine with output, where the output is determined only by the state. This type of finite­ state machine is known as a Moore machine, because E. F. Moore introduced this type of machine in 1 956. Moore machines are considered in a sequence of exercises at the end of this section. In Example 7 we showed how a Mealy machine can be used for language recognition. However, another type of finite-state machine, giving no output, is usually used for this purpose. Finite-state machines with no output, also known as finite-state automata, have a set of final states and recognize a string if and only if it takes the start state to a final state. We will study this type of finite-state machine in Section 1 2. 3 .

1 2- 1 8

12 / Modeling Computation

802

Exercises 1. Draw the state diagrams for the finite-state machines with these state tables.

1 ,0

b)

a) f

g

Input

Input 1 0

State

0

1

80

81

80

82

81

81

81

80

82

1 1 0

0 0 0

Start

c)

1,1

Start

b) f

g

Input

Input 1 0

State

0

1

80

81

80

82

80

81

83

82

80

83

81

82

0 1 0 1

0 1 1 0

0

80

80

81

82

83

84

f

g

Input

Input 1 0

1

84

80

83

81

81

80 81

82 80

1 0 0 1 1

1 1 0 1 0

2. Give the state tables for the finite-state machines with these state diagrams.

a)

Start

3. Find the output generated from the input string 01 1 1 0 for the finite-state machine with the state table in a) Exercise l (a). b) Exercise 1 (b). c) Exercise l (c). 4. Find the output generated from the input string 1 000 1 for the finite-state machine with the state diagram in

a) b) c)

c)

State

1 ,0

Exercise 2(a). Exercise 2(b). Exercise 2(c).

5. Find the output for each of these input strings when given as input to the finite-state machine in Example 2.

a) 0 1 1 1 c) 0 1 0 1 0 1 0 1 0 1 0

b) 1 1 0 1 1 0 1 1

6 . Find the output for each o f these input strings when given as input to the finite-state machine in Example 3.

a) 0000 c ) 1 1 0 1 1 1 000 1 0

b) 1 0 1 0 1 0

7 . Construct a finite-state machine that models an old­

fashioned soda machine that accepts nickels, dimes, and quarters. The soda machine accepts change until 35 cents has been put in. It gives change back for any amount greater than 35 cents. Then the customer can push but­ tons to receive either a cola, a root beer, or a ginger ale.

8. Construct a finite-state machine that models a newspa­ per vending machine that has a door that can be opened only after either three dimes (and any number of other coins) or a quarter and a nickel (and any number of other coins) have been inserted. Once the door can be opened, the customer opens it and takes a paper, closing the door. No change is ever returned no matter how much extra money has been inserted. The next customer starts with no credit.

1 2.2 Finite-State Machines with Output

1 2- 1 9

9. Construct a finite-state machine that delays an input string two bits, giving 00 as the first two bits of output.

10. Construct a finite-state machine that changes every other bit, starting with the second bit, of an input string, and leaves the other bits unchanged.

1 1 . Construct a finite-state machine for the log-on procedure for a computer, where the user logs on by entering a user identification number, which is considered to be a single input, and then a password, which is considered to be a single input. If the password is incorrect, the user is asked for the user identification number again.

state and an input, an output function g that assigns an output to every state, and a starting state so . A Moore machine can be represented either by a table listing the transitions for each pair of state and input and the outputs for each state, or by a state diagram that displays the states, the transitions between states, and the output for each state. In the diagram, transi­ tions are indicated with arrows labeled with the input, and the outputs are shown next to the states.

20. Construct the state diagram for the Moore machine with this state table.

12. Construct a finite-state machine for a combination lock

f

that contains numbers 1 through 40 and that opens only when the correct combination, 1 0 right, 8 second left, 37 right, is entered. Each input is a triple consisting of a num­ ber, the direction of the turn, and the number of times the lock is turned in that direction.

13. Construct a finite-state machine for a toll machine that opens a gate after 25 cents, in nickels, dimes, or quarters, has been deposited. No change is given for overpayment, and no credit is given to the next driver when more than 25 cents has been deposited.

14. Construct a finite-state machine for entering a security code into an automatic teller machine (ATM) that im­ plements these rules: A user enters a string of four dig­ its, one digit at a time. If the user enters the correct four digits of the password, the ATM displays a wel­ come screen. When the user enters an incorrect string of four digits, the ATM displays a screen that informs the user that an incorrect password was entered. If a user enters the incorrect password three times, the account is locked.

803

Input

State

0

1

g

So SI S2 S3

So S3 S2 S2

S2 So SI So

0 1 1 1

2 1 . Construct the state table for the Moore machine with the state diagram shown here. Each input string to a Moore machine M produces an output string. In particular, the output corresponding to the input string a l a2 . . . ak is the string g(so)g(sl ) . . . g(Sk), where S I = !(Si - I , ai ) for

i

=

1 , 2, . . . , k.

o

IS. Construct a finite-state machine for a restricted telephone

switching system that implements these rules. Only calls to the telephone numbers 0, 9 1 1 , and the digit 1 followed by l O-digit telephone numbers that begin with 2 1 2, 800, 866, 877, and 888 are sent to the network. All other strings of digits are blocked by the system and the user hears an error message.

Start

16. Construct a finite-state machine that gives an output of 1

if the number of input symbols read so far is divisible by

3 and an output of 0 otherwise. 17. Construct a finite-state machine that determines whether the input string has a 1 in the last position and a 0 in the third to the last position read so far.

18. Construct a finite-state machine that determines whether the input string read so far ends in at least five consecu­ tive I s.

19. Construct a finite-state machine that determines whether the word computer has been read as the last eight charac­ ters in the input read so far, where the input can be any string of English letters. A Moore machine M = (S, I, 0 , !, g , so) consists ofa finite set of states, an input alphabet I , an output alphabet 0 , a tran­ sition function ! that assigns a next state to every pair of a

22. Find the output string generated by the Moore machine in Exercise 20 with each of these input strings.

a) 0 1 0 1 c) 1 1 1 0 1 1 1 0 1 1 1

b) 1 1 1 1 1 1

23. Find the output string generated by the Moore machine in Exercise 2 1 with each of the input strings in Exercise 22.

24. Construct a Moore machine that gives an output of 1

whenever the number of symbols in the input string read so far is divisible by 4.

25. Construct a Moore machine that determines whether an input string contains an even or odd number of 1 s. The machine should give 1 as output if an even number of 1 s are in the string and 0 as output if an odd number of 1 s are in the string.

804

1 2 / Modeling Computation

12.3

1 2-20

Finite-State Machines with No Output Introduction

links�

One ofthe most important applications of finite-state machines is in language recognition. This application plays a fundamental role in the design and construction of compilers for program­ ming languages. In Section 1 2.2 we showed that a finite-state machine with output can be used to recognize a language, by giving an output of 1 when a string from the language has been read and a 0 otherwise. However, there are other types of finite-state machines that are specially designed for recognizing languages. Instead of producing output, these machines have final states. A string is recognized if and only if it takes the starting state to one of these final states.

Set of Strings Before discussing finite-state machines with no output, we will introduce some important back­ ground material on sets of strings. The operations that will be defined here will be used exten­ sively in our discussion of language recognition by finite-state machines.

DEFINITION 1

EXAMPLE 1

Suppose that A and B are subsets of V*, where V is a vocabulary. The concatenation of A and B , denoted by A B , is the set of all strings of the form xy, where x is a string in A and y is a string in B .

Let A = {O, I I } and B

=

{ I , 1 0 , 1 1 0}. Find A B and B A .

Solution: The set A B contains every concatenation o f a string i n A and a string in B . Hence, A B = {O l , 0 1 0, 0 1 1 0, 1 1 1 , 1 1 1 0, 1 1 1 1 0} . The set B A contains every concatenation of a string .... in B and a string in A . Hence, B A = { I O, 1 1 1 , 1 00, 1 0 1 1 , 1 1 00, 1 1 0 1 1 } . Note that it is not necessarily the case that A B = B A when A and B are subsets of V*, where V is an alphabet, as Example 1 illustrates. From the definition of the concatenation of two sets of strings, we can define A n , for n = 0, 1 , 2, . . . . This is done recursively by specifying that

A O = {A} , An+ ' = An A EXAMPLE 2

Let A

=

for n = 0, 1 , 2, . . . .

{ I , OO } . Find A n for n = 0, 1 , 2, and 3 .

2 Solution: We have A O = { A } and A ' = A O A = { A } A = { I , OO} . To find A we take concate­ 2 3 nations of pairs of elements of A . This gives A = { 1 1 , 1 00, 00 1 , OOOO}. To find A we take 2 3 concatenations of elements in A and A ; this gives A = { I l l , 1 1 00, 1 00 1 , 1 0000, .... 00 1 1 , 00 1 00, 0000 1 , 000000} .

DEFINITION 2

Suppose that A is a subset of V*. Then the Kleene closure of A , denoted by A*, is the set consisting of concatenations of arbitrarily many strings from A . That is, A * = U�O A k .

12-21

1 2 . 3 Finite-State Machines with No Output

EXAMPLE 3

What are the Kleene closures of the sets A

=

{OJ, B

=

to, I } , and C

=

805

{ I I }?

Solution: The Kleene closure of A is the concatenation of the string ° with itself an arbitrary finite number of times. Hence, A * = {on I n = 0, 1 , 2 , . . . } . The Kleene closure of B is the concatenation of an arbitrary number of strings, where each string is either ° or 1 . This is the set of all strings over the alphabet V = to, I } . That is, B * = V * . Finally, the Kleene closure of C is the concatenation of the string 1 1 with itself an arbitrary number of times. Hence, C* is the set of strings consisting of an even number of 1 s. That is, C* = { 1 2n I n = 0, 1 , 2 , . . . } . ....

Finite-State Automata We will now give a definition of a finite-state machine with no output. Such machines are also called finite-state automata, and that is the terminology we will use for them here. (Note: The singular of automata is automaton. ) These machines differ from the finite-state machines studied in Section 1 2 .2 in that they do not produce output, but they do have a set of final states. As we will see, they recognize strings that take the starting state to a final state.

DEFINITION 3

A finite-state automaton M = (S, I, I, so , F) consists of a finite set S of states, a finite input alphabet I, a transition lunction I that assigns a next state to every pair of state and input (so that I : S x I -+ S), an initial or start state so , and a subset F of S consisting offinal (or accepting states).

We can represent finite-state automata using either state tables or state diagrams. Final states are indicated in state diagrams by using double circles. EXAMPLE 4

Construct the state diagram for the finite-state automaton M = (S, I, J, so , F), where S { so , S t , S2 , S 3 } , I = to, I } , F = { so , S3 } , and the transition function f is given in Table 1 .

=

Solution: The state diagram is shown in Figure 1 . Note that because both the inputs ° and 1 take .... to so, we write 0, 1 over the edge from S2 to so .

S2

EXTENDING THE TRANSITION FUNCTION The transition function I of a finite-state machine M = (S, I, I, so , F) can be extended so that it is defined for all pairs of states and strings; that is, / can be extended to a function / : S x [* -+ S. Let x = X t X2 . . . xk be a string in 1 * . Then I(s t , x) is the state obtained by using each successive symbol of x , from left to right, as

Linl'S�

S T E P H EN C O L E K L E E N E ( 1 909- 1 994) Stephen Kleene was born in Hartford, Connecticut. His mother, Alice Lena Cole, was a poet, and his father, Gustav Adolph Kleene, was an economics professor. Kleene attended Amherst College and received his Ph. D. from Princeton in 1 934, where he studied under the famous logician Alonzo Church. Kleene j oined the faculty of the University of Wisconsin in 1 93 5 , where he remained except for several leaves, including stays at the Institute for Advanced Study in Princeton. During World War 11 he was a navigation instructor at the Naval Reserve's Midshipmen's School and later served as the director of the Naval Research Laboratory. Kleene made significant contributions to the theory of recursive functions, investigating questions of computability and decidability, and proved one of the central results of automata theory. He served as the Acting Director of the Mathematics Research Center and as Dean of the College of Letters and Sciences at the University of Wisconsin. Kleene was a student of natural history. He discovered a previously undescribed variety of butterfty that is named after him. He was an avid hiker and climber. Kleene was also noted as a talented teller of anecdotes, using a powerful voice that could be heard several offices away.

806

1 2 / Modeling Computation

1 2-22

TABLE 1 f

State So

SI

S2

S3

S tart

Input 1

0 So

SI

So

S2

S2

SI

So

So

FIGURE 1 The State Diagram for a Finite-State Automaton.

input, starting with state si . From si we go on to state s2 = l(sl , X I ), then to state s3 = l(s2 , X2), and so on, with l(sl , x ) = I(sk , Xk). Formally, we can define this extended transition function I recursively for the deterministic finite-state machine M = (S, I, I, So , F) by

(i) I(s , ).. ) = S for every state S E S; and (ii) I(s , x a ) = l(f(s , x ) , a ) for all S E S, x

E

1*, and a

E

I.

We can use structural induction and this recursive definition to prove properties of this extended transition function. For example, in Exercise 1 5 we ask you to prove that

I(s , xy)

=

for every state s

1(/(s , x), y) E

S and strings x

E

1* and y

E

1*.

Language Recognition by Finite-State Machines Next, we define some terms that are used when studying the recognition by finite-state automata of certain sets of strings.

DEFINITION 4

A string x is said to be recognized or accepted by the machine M = (S, I, J, So . F) if it takes the initial state So to a final state, that is, I(so, x) is a state in F. The language recognized or accepted by the machine M, denoted by L(M), is the set of all strings that are recognized by M. Two finite-state automata are called equivalent if they recognize the same language.

In Example 5 we will find the languages recognized by several finite-state automata. EXAMPLE 5

Determine the languages recognized by the finite-state automata MJ , M2, and M3 in Figure 2.

Solution: The only final state of MI is So . The strings that take So to itself are those consisting of zero or more consecutive I s. Hence, L(Ml ) = { I n I n = O. 1 . 2 }. The only final state o f M2 i s S2 . The only strings that take So to S 2 are 1 and 0 1 . Hence, L(M2) = { I . O I l . The final states of M3 are So and S3 . The only strings that take So to itself are ).. . O . 00. 000 , that is, any string of zero or more consecutive Os. The only strings that take So to S3 are a string •

.

.

.



.

.

.

1 2.3 Finite-State Machines with No Output

12-23

807

Start

o Start

Mz

Start

0, 1

FIGURE 2

Some Finite-State Automata.

of zero or more consecutive Os, followed by 1 0, followed by any string. Hence, L(M3 ) = .... {on , on l Ox I n = 0, 1 , 2, . . . , and x is any string} . DESIGNING FINITE-STATE AUTOMATA We can often construct a finite-state automaton that recognizes a given set of strings by carefully adding states and transitions and determining which of these states should be final states. When appropriate we include states that can keep track of some of the properties of the input string, providing the finite-state automaton with limited memory. Examples 6 and 7 illustrate some of the techniques that can be used to construct finite-state automata that recognize particular types of sets of strings. EXAMPLE 6

Construct deterministic finite-state automata that recognize each of these languages. (a) (b) (c) (d) (e)

the set of bit strings that begin with two Os the set of bit strings that contain two consecutive Os the set of bit strings that do not contain two consecutive Os the set of bit strings that end with two Os the set of bit strings that contain at least two Os

Solution: (a) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that begin with two Os. Besides the start state So, we include a nonfinal state SI ; we move to SI from So if the first bit is a O. Next, we add a final state S2, which we move to from SI if the second bit is a O. When we have reached SI we know that the first two input bits

808

1 2 / Modeling Computation

1 2-24

( a) Start

0, I

(b) (c)

Start

( d)

0, I

��

Start ---. -+l

_

FIGURE 3

I

I

Start

0, I

(e ) Start

0, I

Deterministic Finite-State Automata Recognizing the Languages in Example 6.

are both Os, so we stay in the state S2 no matter what the succeeding bits (if any) are. We move to a nonfinal state S3 from So if the first bit is a 1 and from Sl if the second bit is a 1 . The reader should verify that the finite-state automaton in Figure 3(a) recognizes the set of bit strings that begin with two Os. (b) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that contain two consecutive Os. Besides the start state So, we include a nonfinal state SJ , which tells us that the last input bit seen is a 0, but either the bit before it was a 1 , or this bit was the initial bit of the string. We include a final state S2 that we move to from Sl when the next input bit after a 0 is also a O. If a 1 follows a 0 in the string (before we encounter two consecutive Os), we return to So and begin looking for consecutive Os all over again. The reader should verify that the finite-state automaton in Figure 3 (b) recognizes the set of bit strings that contain two consecutive Os. (c) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that do not contain two consecutive Os. Besides the start state So, which should be a final state, we include a final state S1 , which we move to from So when 0 in the first input bit. When an input bit is a 1 , we return to, or stay in, state So . We add a state S2, which we move to from Sl when the input bit is a O. Reaching S2 tells us that we have seen two con­ secutive Os as input bits. We stay in state S2 once we have reached it; this state is not final. The reader should verify that the finite-state automaton in Figure 3( c) recognizes the set of bit strings that do not contain two consecutive Os. [The astute reader will notice the relation­ ship between the finite-state automaton constructed here and the one constructed in part (b). See Exercise 39.] (d) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that end with two Os. Besides the start state So, we include a nonfinal state Sl , which we move to if the first bit is O. We include a final state S2 , which we move to from Sl if the next input bit after a 0 is also a O. If an input of 0 follows a previous 0, we stay in state S2 because the last two input bits are still Os. Once we are in state S2, an input bit of 1 sends us back to So, and we begin looking for consecutive Os all over again. We also return to So if the next input is a 1

1 2.3 Finite-State Machines with No Output

1 2-25

Start

Number of Is Number of Os at end

809

o

even

even

o

even

odd

odd

odd

o

FIGU RE 4 A Deterministic Finite-State Automaton Recognizing the Set of Bit Strings Containing an Odd Number of Is and Ending with at Least Two Os.

when we are in state St . The reader should verify that the finite-state automaton in Figure 3(d) recognizes the set of bit strings that end with two Os. (e) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that contain two Os. Besides the start state, we include a state St , which is not final; we stay in So until an input bit is a 0 and we move to St when we encounter the first 0 bit in the input. We add a final state S2, which we move to from St once we en­ counter a second 0 bit. Whenever we encounter a I as input, we stay in the current state. Once we have reached S2, we remain there. Here, St and S2 are used to tell us that we have already seen one or two Os in the input string so far, respectively. The reader should ver­ ify that the finite-state automaton in Figure 3(e) recognizes the set of bit strings that contain

�� EXAMPLE 7



Construct a deterministic finite-state automaton that recognizes the set of bit strings that contain an odd number of 1 s and that end with at least two consecutive Os.

Solution: We can build a deterministic finite-state automaton that recognizes the specified set by including states that keep track of both the parity of the number of 1 bits and whether we have seen no, one, or at least two Os at the end of the input string. The start state So can be used to tell us that the input read so far contains an even number of I s and ends with no Os (that is, is empty or ends with a 1 ). Besides the start state, we include five more states. We move to states St , S2, S3 , S4, and ss , respectively, when the input string read so far contains an even number of I s and ends with one 0; when it contains an even number of 1 s and ends with at least two Os; when it contains an odd number of 1 s and ends with no Os; when it contains an odd number of I s and ends with one 0; and when it contains an odd number of 1 s and ends with two Os. The state Ss is a final state. The reader should verify that the finite-state automaton in Figure 4 recognizes the set of bit � strings that contain an odd number of 1 s and end with at least two consecutive Os.

EQUIVALENT FINITE-STATE AUTOMATA In Definition 4 we specified that two finite­ state automata are equivalent if they recognize the same language. Example 8 provides an example of two equivalent deterministic finite-state machines.

810

1 2 / Modeling Computation

1 2-26

Start

o

Start

FIGURE 5

EXAMPLE 8

0, I

Mo and M. Are Equivalent Finite-State Automata.

Show that the two finite-state automata Mo and M\ shown in Figure 5 are equivalent.

Solution: For a string x to be recognized by Mo, x must take us from So to the final state s\ or the final state S4 . The only string that takes us from So to s \ is the string 1 . The strings that take us from So to S4 are those strings that begin with a 0, which takes us from So to S2, followed by zero or more additional Os, which keep the machine in state S2 , followed by a 1 , which takes us from state S2 to the final state S4 . All other strings take us from So to a state that is not final. (We leave it to the reader to fill in the details.) We conclude that L(Mo) is the set of strings of zero or more 0 bits followed by a final 1 . For a string x to be recognized by M\ , X must take us from So to the final state St . So, for x to be recognized, it must begin with some number of Os, which leave us in state So, followed by a 1 , which takes us to the final state St . A string of all zeros is not recognized because it leaves us in state So, which is not final. All strings that contain a 0 after 1 are not recognized because they take us to state S2, which is not final. It follows that L(Mt } is the same as L(Mo). We conclude that Mo and M\ are equivalent. Note that the finite-state machine M \ only has three states. No finite state machine with fewer than three states can be used to recognize the set of all strings of zero or more 0 bits .... followed by a 1 (see Exercise 3 7 ). As Example 8 shows, a finite-state automaton may have more states than one equivalent to it. In fact, algorithms used to construct finite-state automata to recognize certain languages may have many more states than necessary. Using unnecessarily large finite-state machines to recognize languages can make both hardware and software applications inefficient and costly. This problem arises when finite-state automata are used in compilers, which translate computer programs to a language a computer can understand (object code). Exercises 5� 1 develop a procedure that constructs a finite-state automaton with the fewest states possible among all finite-state automata equivalent to a given finite-state automaton. This procedure is known as machine minimization. The minimization procedure described in these exercises reduces the number of states by replacing states with equivalence classes of states with respect to an equivalence relation in which two states are equivalent if every input string either sends both states to a final state or sends both to a state that is not final. Before the minimization

1 2.3 Finite-State Machines with No Output

12-2 7

81 1

procedure begins, all states that cannot be reached from the start state using any input string are first removed; removing these does not change the language recognized.

Nondeterministic Finite-State Automata The finite-state automata discussed so far are deterministic, because for each pair of state and input value there is a unique next state given by the transition function. There is another important type of finite-state automaton in which there may be several possible next states for each pair of input value and state. Such machines are called nondeterministic. Nondetermin­ istic finite-state automata are important in determining which languages can be recognized by a finite-state automaton.

DEFINITION 5

UnkS�

A nondeterministic finite-state automaton M = (S, I , f, so , F) consists of a set S of states, an input alphabet I, a transition function f that assigns a set of states to each pair of state and input (so that f : S x I -+ P(S)), a starting state so, and a subset F of S consisting of the final states.

We can represent nondeterministic finite-state automata using state tables or state diagrams. When we use a state table, for each pair of state and input value we give a list of possible next

Grace Hopper, born in New York City, displayed an intense curiosity as a child with how things worked. At the age of seven, she disassembled alarm clocks to discover their mechanisms. She inherited her love of mathematics from her mother, who received special permission to study geometry (but not algebra and trigonometry) at a time when women were actively discouraged from such study. Hopper was inspired by her father, a successful insurance broker, who had lost his legs from circulatory problems. He told his children they could do anything if they put their minds to it. He inspired Hopper to pursue higher education and not conform to the usual roles for females. Her parents made sure that she had an excellent education; she attended private schools for girls in New York. Hopper entered Vassar College in 1 924, where she majored in mathematics and physics; she graduated in 1 928. She received a masters degree in mathematics from Yale University in 1 930. In 1 930 she also married an English instructor at the New York School of Commerce; she later divorced and did not have children. Hopper was a mathematics professor at Vassar from 1 93 1 until 1 943, earning a Ph.D. from Yale in 1 934. After the attack on Pearl Harbor, Hopper, coming from a family with strong military traditions, decided to leave her academic position and join the Navy WAVES. To enlist, she needed special permission to leave her strategic position as a mathematics professor, as well as a waiver for weighing too little. In December 1 943, she was sworn into the Navy Reserve and trained at the Midshipman's School for Women. Hopper was assigned to work at the Naval Ordnance Laboratory at Harvard University. She wrote programs for the world's first large-scale automatically sequenced digital computer, which was used to help aim Navy artillery in varying weather. Hopper has been credited with coining the term "bug" to refer to a hardware glitch, but it was used at Harvard prior to her arrival there. However, it is true that Hopper and her programming team found a moth in one of the relays in the computer hardware that shut the system down. This famous moth was pasted into a lab book. In the 1 950s Hopper coined the term "debug" for the process of removing programming errors. In 1 946, when the Navy told her that she was too old for active service, Hopper chose to remain at Harvard as a civilian research fellow. In 1 949 she left Harvard to join the Eckert-Mauchly Computer Corporation, where she helped develop the first commercial computer, UNIVAC. Hopper remained with this company when it was taken over by Remington Rand and when Remington Rand merged with the Sperry Corporation. She was a visionary for the potential power of computers; she understood that computers would become widely used if tools that were both programmer-friendly and application-friendly could be developed. In particular, she believed that computer programs could be written in English, rather than using machine instructions. To help achieve this goal, she developed the first compiler. She published the first research paper on compilers in 1 952. Hopper is also known as the mother of the computer language COBOL; members of Hopper's staff helped to frame the basic language design for COBOL using their . earlier work as a basis. In 1 966, Hopper retired from the Navy Reserve. However, only seven months later, the Navy recalled her from retirement to help standardize high-level naval computer languages. In 1 983 she was promoted to the rank of Commodore by special Presidential appointment, and in 1 985 she was elevated to the rank of Rear Admiral. Her retirement from the Navy, at the age of 80, was held on the USS Constitution. G RACE BREWSTER M U RRAY HOPPER ( 1 906- 1 992)

812

12-28

1 2 / Modeling Computation

TABLE 2

Stan

f

Input

State So

1

0 So , S (

S3

S(

So

S ( , S3

S3

So , S b S2

S(

S2

So , S2

FIGURE 6 The Nondeterministic Finite-State Automaton with State Table Given in Table 2.

states. In the state diagram, we include an edge from each state to all possible next states, labeling edges with the input or inputs that lead to this transition.

EXAMPLE 9

Find the state diagram for the nondeterministic finite-state automaton with the state table shown in Table 2. The final states are S2 and S3 .

Solution: The state diagram for this automaton is shown in Figure 6. EXAMPLE 10

Find the state table for the nondeterministic finite-state automaton with the state diagram shown in Figure 7.

Solution: The state table is given as Table 3 . What does it mean for a nondeterministic finite-state automaton to recognize a string x = Xk? The first input symbol XI takes the starting state So to a set SI of states. The next input symbol X2 takes each of the states in SI to a set of states. Let S2 be the union of these sets. We continue this process, including at a stage all states obtained using a state obtained at the previous stage and the current input symbol. We recognize, or accept, the string X if there is a final state in the set of all states that can be obtained from So using x . The language recognized by a nondeterministic finite-state automaton is the set of all strings recognized by this automaton.

XIX2







o

TABLE 3 f S tan ---I�

State So

0 So , S2

S(

S3

S3

S3

S2

FIGURE 7 A Nondeterministic Finite-State Automaton.

Input

S4

S3

1 S(

S4

S4 S3

12-29

EXAMPLE 11

12.3 Finite-State Machines with No Output

813

Find the language recognized by the nondetenninistic finite-state automaton shown in Figure 7.

Solution: Because So is a final state, and there is a transition from So to itself when 0 is the input, the machine recognizes all strings consisting of zero or more consecutive Os. Furthennore, because S4 is a final state, any string that has S4 in the set of states that can be reached from So with this input string is recognized. The only such strings are strings consisting of zero or more consecutive Os followed by 0 I or 1 1 . Because So and S4 are the only final states, the language .... recognized by the machine is {on , on 0 1 , on 1 1 I n :::: O } . One important fact is that a language recognized by a nondetenninistic finite-state automaton is also recognized by a detenninistic finite-state automaton. We will take advantage of this fact in Section 1 2.4 when we will detennine which languages are recognized by finite-state automata. THEOREM 1

If the language L is recognized by a nondeterministic finite-state automaton Mo, then L is also recognized by a detenninistic finite-state automaton MI .

Proof: We will describe how to construct the detenninistic finite-state automaton Ml that

recognizes L from Mo, the nondetenninistic finite-state automaton that recognizes this language. Each state in MI will be made up of a set of states in Mo . The start symbol of MI is {so } , which is the set containing the start state of Mo . The input set of MI is the same as the input set of Mo . Given a state {Sil ' Siz ' . . . , Sik } of MI , the input symbol x takes this state to the union of the sets ofnext states for the elements ofthis set, that is, the union ofthe sets !(SiJ, !(Si2 )' . . . , !(Sik ). The states of MI are all the subsets of S, the set of states of Mo, that are obtained in this way starting with So . (There are as many as 2n states in the detenninistic machine, where n is the number of states in the nondetenninistic machine, because all subsets may occur as states, including the empty set, although usually far fewer states occur.) The final states of M I are those sets that contain a final state of Mo . Suppose that an input string is recognized by Mo . Then one of the states that can be reached from So using this input string is a final state (the reader should provide an inductive proof of this). This means that in MI , this input string leads from {so } to a set of states of Mo that contains a final state. This subset is a final state of MI , so this string is also recognized by MI . Also, an input string not recognized by Mo does not lead to any final states in Mo . (The reader should provide the details that prove this statement.) Consequently, this input string does not lead from y or x = y . The first of these axioms tells us that given two real numbers, exactly one of three possibilities occurs: the two numbers are equal, the first is greater than the second, or the second is greater than the first. This rule is called the trichotomy law.

Trichotomy Law For all real numbers x and y, exactly one of x

true.

=

y, x > y, or y > x is

Next, we have an axiom, called the transitivity law, that tells us that if one number is greater than a second number and this second number is greater than a third, then the first number is greater than the third. Transitivity Law For all real numbers x , y, and z, if x > y and y > z, then x > z .

We also have two compatibility laws, which tell us that when we add a number to both sides in a greater than relationship, the greater than relationship is preserved and when we multiply both sides of a greater than relationship by a positive real number (that is, a real number x with x > 0), the greater than relationship is preserved. Additive Compatibility Law

For all real numbers x , y, and z, if x > y, then x + z >

y + z. Multiplicative Compatibility Law For all real numbers x , y, and z, if x > y and z > 0,

then x . z > y . z . We leave it to the reader (see Exercise 1 5) to prove that for all real numbers x , y, and z, if x > y and z < 0, then x . z < y . z . That is, multiplication of an inequality by a negative real number reverses the direction of an inequality. The final axiom for the set of real numbers is the completeness property. Before we state this axiom, we need some definitions. First, given a nonempty set A of real numbers, we say that the real number b is an upper bound of A if for every real number a in A, b ::: a . A real number s is a least upper bound of A if s is an upper bound of A and whenever t is an upper bound of A then we have s ::: t . Completeness Property Every nonempty set o f real numbers that i s bounded above has

a least upper bound.

Using Axioms to Prove Basic Facts The axioms we have listed can be used to prove many properties that are often used without explicit mention. We give several examples of results we can prove using axioms and leave the

Appendix 1 / Axioms for the Real Numbers and the Positive Integers

A I -3

A-3

proof of a variety of other properties as exercises. Although the results we will prove seem quite obvious, proving them using only the axioms we have stated can be challenging.

THEOREM l

The additive identity element 0 of the real numbers is unique.

Proof: To show that the additive identity element 0 of the real numbers is unique, suppose that 0' is also an additive identity for the real numbers. This means that 0' + x = x + 0' = x whenever x is a real number. By the additive identity law, it follows that 0 + 0' = 0' . Because 0' is an additive identity, we know that 0 + 0' = O . It follows that 0 = 0', because both equal 0) 1\ (y > 0) � (x - y > 0» c) VxVy (x 2 + y 2 :::: (x + y)2 ) d) VxVy (Ixy l = Ix I lyl) 21 . Vx3a3b3c3d � > � � x � + � + 2 + d2 ), where the domain consists of all integers 23. a) Vx Vy «x < 0) 1\ (y < 0) � (xy > 0» b) Vx(x x = 0) c) Vx3a3b(a =I b 1\ Vc(c2 = X ++ (c = a V c = b» ) d) Vx«x < 0) � -.3y(x = y2 » 25. a) There is a multi­ plicative identity for the real numbers. b) The product of two negative real numbers is always a positive real number. c) There exist real numbers x and y such that x 2 exceeds y but x is less than y. d) The real numbers are closed under the operation of addition. 27. a) True b) True c) True d) True e) True t) False g) False b) True i) False 29. a) P(I , I ) 1\ P(l ,2) 1\ P(l ,3) 1\ P(2, 1 ) 1\ P(2,2) 1\ b) P(I , 1) v P(2, 3) 1\ P(3, 1) 1\ P(3, 2) 1\ P(3, 3) P(I , 2) v P ( I , 3) v P(2, 1 ) v P(2, 2) v P(2, 3) v P(3 , 1) v c) (P( I , 1 ) 1\ P(I , 2) 1\ P(I , 3» v P(3, 2) v P(3, 3) (P(2 , 1) 1\ P(2, 2) 1\ P(2, 3» v (P(3, 1) 1\ P(3, 2) 1\ P(3 , 3»

Answers to Odd-Numbered Exercises

S-7

d) (P( I , I ) v P(2, 1) v P(3 , I » 1\ (P( I , 2) v P(2, 2) v P(3 , 2» 1\ (P(I , 3) v P(2, 3) v P(3, 3» 3 1 . a) 3xVy3z -.T (x , y, z) b) 3xVy-,P(x , Y) I\ 3xVy -. Q(x , y) c) 3xVy (-.P(x , y) v Vz -.R(x , y, z» d) 3xVy(P(x , y) 1\ -.Q(x , y» 33. a) 3x3y-.P(x , y) b) 3yVx-.P(x , y) c) 3y3x(-.P(x, d) (VxVyP(x , y» v (3x3y-.Q(x, y» y) 1\ -.Q(x , y» e) 3x(Vy3z-.P(x ,y,z) v Vz3y-.P(x , y, z» 35. Any domain with four or more members makes the statement true; any domain with three or fewer members makes the state­ ment false. 37. a) There is someone in this class such that for every two different math courses, these are not the two and only two math courses this person has taken. b) Every person has either visited Libya or has not vis­ ited a country other than Libya. c) Someone has climbed every mountain in the Himalayas. d) There is someone who has neither been in a movie with Kevin Bacon nor has been in a movie with someone who has been in a movie with Kevin Bacon. 39. a) x = 2, y = -2 b) x = -4 4 1 . Vx Vy V z«x ·y) ·z = X · (y · z» c) x = 1 7, y = - I 43. VmVb(m =I 0 � 3x(mx + b = 0 1\ Vw(mw + b = 45. a) True b) False c) True o � w = x» ) 47. -.(3xVyP(x , y» ++ Vx(-.VyP(x , y» ++ Vx3y-.P(x,y) 49. a) Suppose that Vx P (x) 1\ 3x Q(x) is true. Then P(x) is true for all x and there is an element y for which Q(y) is true. Because P(x) 1\ Q(y) is true for all x and there is ay for which Q(y) is true, Vx3y(P(x) 1\ Q(y» is true. Conversely, suppose that the second proposition is true. Let x be an element in the domain. There is a y such that Q(y) is true, so 3x Q(x) is true. Because Vx P(x) is also true, it follows that the first proposition is true. b) Suppose that Vx P(x) v 3x Q(x) is true. Then either P(x) is true for all x, or there exists a y for which Q(y) is true. In the former case, P(x) v Q(y) is true for all x, so Vx3y(P(x) v Q(y» is true. In the latter case, Q(y) is true for a particular y, so P(x) v Q(y) is true for all x and consequently Vx3y(P(x) v Q(y» is true. Conversely, suppose that the second proposition is true. If P(x) is true for all x, then the first proposition is true. Ifnot, P(x) is false for some x, and for this x there must be a y such that P(x) v Q(y) is true. Hence, Q(y) must be true, so 3yQ(y) is true. It fol­ lows that the first proposition must hold. 5 1 . We will show how an expression can be put into prenex normal form (PNF) if subexpressions in it can be put into PNF. Then, working from the inside out, any expression can be put in PNF. (To formalize the argument, it is necessary to use the method of structural induction that will be discussed in Section 4.3.) By Exercise 45 of Section 1 .2, we can assume that the proposition uses only v and -. as logical connectives. Now note that any proposition with no quantifiers is already in PNF. (This is the basis case of the argument.) Now suppose that the proposition is ofthe form Qx P(x), where Q is a quantifier. Because P(x) is a shorter expression than the original proposition, we can put it into PNF. Then Q x followed by this PNF is again in PNF and is equivalent to the original proposition. Next, suppose that the proposition is ofthe form -.P. If P is already in PNF, we slide the negation sign past all the quantifiers using the equivalences in Table 2 in Section 1 .3. Finally, assume that proposition is of the form P v Q , where each of P and Q is in PNF. If only one of P and Q has quantifiers, then we can

8-8

Answers to Odd-Numbered Exercises

use Exercise 46 in Section 1 .3 to bring the quantifier in front of both. Ifboth P and Q have quantifiers, we can use Exercise 45 in Section 1 .3, Exercise 48, or part (b) of Exercise 49 to rewrite P v Q with two quantifiers preceding the disjunction of a proposition of the form R v S, and then put R v S into PNF.

S ection 1.5

1. Modus ponens; valid; the conclusion is true, because the hypotheses are true. 3. a) Addition b) Simplification c) Modus ponens d) Modus tollens e) Hypothetical syl­ logism 5. Let w be "Randy works hard," let d be "Randy is a dull boy," and let J be "Randy will get the job." The hypothe­ ses are w , w � d, and d � -.J . Using modus ponens and the first two hypotheses, d follows. Using modus ponens and the last hypothesis, -'J, which is the desired conclusion, "Randy will not get the job," follows. 7. Universal instantiation is used to conclude that "If Socrates is a man, then Socrates is mortal." Modus ponens is then used to conclude that Socrates is mortal. 9. a) Valid conclusions are "I did not take Tuesday off," "I took Thursday off," "It rained on Thursday." b) "I did not eat spicy foods and it did not thunder" is a valid conclusion. c) "I am clever" is a valid conclusion. d) "Ralph is not a CS major" is a valid conclusion. e) "That you buy lots of stuff is good for the U.S. and is good for you" is a valid conclusion. 1) "Mice gnaw their food" and "Rabbits are not rodents" are valid conclusions. 1 1 . Suppose that P I , P2 , . . . , Pn are true. We want to establish that q � r is true. If q is false, then we are done, vacuously. Otherwise, q is true, so by the validity of the given argument form (that whenever P I , P2 , . . . , Pn , q are true, then r must be true), we know that r is true. 1 3 . a) Let c(x) be "x is in this class," J(x) be "x knows how to write pro­ grams in JAVA," and h(x) be "x can get a high-paying job." The premises are c(Doug), J(Doug), VxU(x) � h (x». Us­ ing universal instantiation and the last premise, J(Doug) � h(Doug) follows. Applying modus ponens to this conclusion and the second premise, h(Doug) follows. Using conjunc­ tion and the first premise, c(Doug) /\ h (Doug) follows. Fi­ nally, using existential generalization, the desired conclusion, 3x(c(x) /\ h(x» follows. b) Let c(x) be "x is in this class," w(x) be "x enjoys whale watching," and p(x) be "x cares about ocean pollution." The premises are 3x(c(x) /\ w(x» and Vx(w(x) � p(x» . From the first premise, c(y) /\ w(y) for a particular person y . Using simplification, w(y) fol­ lows. Using the second premise and universal instantiation, w(y) � p(y) follows. Using modus ponens, p(y) follows, and by conjunction, c(y) /\ p(y) follows. Finally, by existen­ tial generalization, the desired conclusion, 3x(c(x) /\ p(x» , follows. c) Let c(x) be "x is in this class," p(x) be "x owns a PC," and w(x) be "x can use a word-processing program." The premises are c(Zeke), Vx(c(x) � p(x» , and Vx(P(x) � w(x» . Using the second premise and univer­ sal instantiation, c(Zeke) � p(Zeke) follows. Using the first premise and modus ponens, p(Zeke) follows. Using the third

S-8

premise and universal instantiation, p(Zeke) � w(Zeke) fol­ lows. Finally, using modus ponens, w(Zeke), the desired con­ clusion, follows. d) Let J(x) be "x is in New Jersey," !(x) be "x lives within 50 miles of the ocean," and s(x) be "x has seen the ocean." The premises are VxU(x) � !(x» and 3xU(x) /\ -.s(x» . The second hypothesis and existential in­ stantiation imply that J(y) /\ -'s(y) for a particular person y. By simplification, J(y) for this person y. Using univer­ sal instantiation and the first premise, J(y) � !(Y), and by modus ponens, !(Y) follows. By simplification, -.s(y) fol­ lows from J(y) /\ -.s(y). So !(Y) /\ -.s(y) follows by con­ junction. Finally, the desired conclusion, 3x(f(x) /\ -.s(x» , follows by existential generalization. 1 5 . a) Correct, using universal instantiation and modus ponens b) Invalid; fallacy of affirming the conclusion c) Invalid; fallacy of denying the hypothesis d) Correct, using universal instantiation and modus tollens 1 7. We know that some x exists that makes H(x) true, but we cannot conclude that Lola is one such x. 1 9. a) Fallacy of affirming the conclusion b) Fallacy ofbeg­ ging the question c) Valid argument using modus tollens d) Fallacy of denying the hypothesis 2 1 . By the second premise, there is some lion that does not drink coffee. Let Leo be such a creature. By simplification we know that Leo is a lion. By modus ponens we know from the first premise that Leo is fierce. Hence, Leo is fierce and does not drink cof­ fee. By the definition of the existential quantifier, there exist fierce creatures that do not drink coffee, that is, some fierce creatures do not drink coffee. 23. The error occurs in step (5), because we cannot assume, as is being done here, that the c that makes P true is the saine as the c that makes Q true. 25. We are given the premises Vx(P(x) � Q(x» and -.Q(a). We want to show -. P (a). Suppose, to the contrary, that -. P (a) is not true. Then P(a) is true. Therefore by universal modus ponens, we have Q(a). But this contradicts the given premise -. Q(a). Therefore our supposition must have been wrong, and so -. P (a) is true, as desired. 27.

Step

1 . Vx(P(x) /\ R(x»

2. P(a) /\ R(a) 3 . P(a) 4. Vx(P(x) � " (Q(x) /\ S(x» ) 5. Q(a) /\ S(a)

29.

Reason

Premise Universal instantiation from ( 1 ) Simplification from (2) Premise

6. S(a) 7. R(a) 8. R(a) /\ S(a) 9. Vx(R(x) /\ S(x»

Universal modus ponens from (3) and (4) Simplification from (5) Simplification from (2) Conjunction from (7) and (6) Universal generalization from (5)

Step

Reason

1 . 3x-.P(x)

Premise Existential instantiation from ( 1 ) Premise Universal instantiation from (3) Disjunctive syllogism from (4) and (2) 6. Vx(-.Q(x) v S(x» Premise

2. -.P(c) 3. Vx(P(x) v Q(x» 4. P(c) v Q(c) 5. Q(c)

Answers to Odd-Numbered Exercises

S-9

7. -.Q(c) v S(c) 8. S(c)

Universal instantiation from (6) Disjunctive syllogism from (5) and (7) 9. Vx(R(x) -+ -,S(x» Premise Universal instantiation from (9) 10. R(c) -+ -.S(c) Modus tollens from (8) and ( 1 0) I I . -'R(c) Existential generalization from 12. 3x-.R(x) (1 1) 3 1 . Let p be "It is raining"; let q be "Yvette has her umbrella"; let r be "Yvette gets wet." Assumptions are -.p v q, -.q v -.r, and p v -.r . Resolution on the first two gives -'p v -.r . Res­ olution on this and the third assumption gives -.r, as desired. 33. Assume that this proposition is satisfiable. Using resolu­ tion on the first two clauses enables us to conclude q v q; in other words, we know that q has to be true. Using resolution on the last two clauses enables us to conclude -.q v -'q ; in other words, we know that -.q has to be true. This is a contradiction. So this proposition is not satisfiable. 35. Valid Section 1.6 1. Let n = 2k + I and m = 21 + I be odd integers. Then n + m = 2(k + 1 + I) is even. 3. Suppose that n is even. Then n = 2k for some integer k. Therefore, n 2 = (2k)2 = 4k2 = 2(2k2 ). Because we have written n 2 as 2 times an integer, we conclude that n 2 is even. 5. Direct proof: Suppose that m + n and n + p are even. Then m + n = 2s for some integer s and n + p = 21 for some integer I . If we add these, we get m + p + 2n = 2s + 21. Subtracting 2n from both sides and factoring, we have m + p = 2s + 21 - 2n = 2(s + 1 - n). Because we have written m + p as 2 times an in­ teger, we conclude that m + p is even. 7. Because n is odd, we can write n = 2k + I for some integer k. Then (k + 1)2 � = � + 2k + 1 - k2 = 2k + 1 = n. 9. Suppose that r is rational and i is irrational and s = r + i is rational. Then by Example 7, s + ( -r) = i is rational, which is a contradiction. 1 1 . Because ,.fi . ,.fi = 2 is rational and ,.fi is irrational, the product oftwo irrational numbers is not necessarily irrational. 1 3 . Proof by contraposition: If I /x were rational, then by def­ inition I /x = p/q for some integers p and q with q =1= O. Because I /x cannot be 0 (if it were, then we 'd have the contradiction I = x . 0 by multiplying both sides by x), we know that p =1= O. Now x = I /( I /x) = I /(p/q) = q/p by the usual rules of algebra and arithmetic. Hence, x can be written as the quotient of two integers with the denomina­ tor nonzero. Thus by definition, x is rational. 1 5. Assume that it is not true that x :::: I or y :::: 1 . Then x < I and y < 1 . Adding these two inequalities, we obtain x + y < 2, which is the negation of x + y :::: 2. 1 7. a) Assume that n is odd, so n = 2k + I for some integer k. Then n3 + 5 = 2(4k3 + 6k2 + 3k + 3). Because n 3 + 5 is two times some integer, it is even. b) Suppose that n 3 + 5 is odd and n is odd. Because n is odd and the product oftwo odd numbers is odd, it follows that n 2 is odd and then that n 3 is odd. But then 5 = (n3 + 5) - n3 would have to be even because it is the difference oftwo odd numbers. Therefore, the supposition that n 3 + 5 and n were both odd is wrong. 1 9. The proposition is vacuously true because 0

S-9

is not a positive integer. Vacuous proof. 2 1 . P(I) is true because (a + b)' = a + b :::: a ' + b' = a + b. Direct proof. 23. If we chose 9 or fewer days on each day of the week, this would account for at most 9 . 7 = 63 days. But we chose 64 days. This contradiction shows that at least 1 0 of the days we chose must be on the same day of the week. 25. Suppose by way of contradiction that a / b is a rational root, where a and b are integers and this fraction is in lowest terms (that is, a and b have no common divisor greater than I). Plug this proposed root into the equation to obtain a 3 /b 3 + alb + I = O. Multi­ ply through by b 3 to obtain a 3 + ab2 + b 3 = O. If a and b are both odd, then the left-hand side is the sum of three odd num­ bers and therefore must be odd. If a is odd and b is even, then the left-hand side is odd + even + even, which is again odd. Similarly, if a is even and b is odd, then the left-hand side is even + even + odd, which is again odd. Because the fraction a / b is in simplest terms, it cannot happen that both a and b are even. Thus in all cases, the left-hand side is odd, and therefore cannot equal O. This contradiction shows that no such root ex­ ists. 27. First, assume that n is odd, so that n = 2k + I for some integer k. Then 5n + 6 = 5(2k + I ) + 6 = 10k + 1 1 = 2(5k + 5) + 1 . Hence, 5n + 6 is odd. To prove the converse, suppose that n is even, so that n = 2k for some integer k. Then 5n + 6 = 10k + 6 = 2(5k + 3), so 5n + 6 is even. Hence, n is odd if and only if 5n + 6 is odd. 29. This proposition is true. Suppose that m is neither I nor - I . Then m n has a fac­ tor m larger than 1 . On the other hand, m n = I , and I has no such factor. Hence, m = I or m = - I . In the first case n = I , and in the second case n = - I , because n = I / m . 3 1 . We prove that all these are equivalent to x being even. If x is even, then x = 2k for some integer k. Therefore 3x + 2 = 3 . 2k + 2 = 6k + 2 = 2(3k + I), which is even, because it has been written in the form 21, where 1 = 3k + I . Similarly, x + 5 = 2k + 5 = 2k + 4 + I = 2(k + 2) + I , so x + 5 is odd; and x 2 = (2k)2 = 2(2�), so x 2 is even. For the converses, we will use a proof by contraposition. So as­ sume that x is not even; thus x is odd and we can write x = 2k + I for some integer k. Then 3x + 2 = 3(2k + I ) + 2 = 6k + 5 = 2(3k + 2) + I , which is odd (i.e., not even), because it has been written in the form 21 + I , where 1 = 3k + 2. Similarly, x + 5 = 2k + I + 5 = 2(k + 3), so x + 5 is even (i.e., not odd). That x 2 is odd was already proved in Exam­ ple 1 . 33. We give proofs by contraposition of (i ) -+ (ii ), (ii) -+ (i ), (i ) -+ (iii ), and (iii ) -+ (i ). For the first of these, suppose that 3x + 2 is rational, namely, equal to p / q for some integers p and q with q =1= O. Then we can write x = {(p/q) - 2)/3 = (p - 2q)/(3q), where 3q =1= O. This shows that x is rational. For the second conditional statement, sup­ pose that x is rational, namely, equal to p /q for some in­ tegers p and q with q =1= O. Then we can write 3x + 2 = (3p + 2q)/q, where q =1= O. This shows that 3x + 2 is ra­ tional. For the third conditional statement, suppose that x /2 is rational, namely, equal to p /q for some integers p and q with q =1= O. Then we can write x = 2p/q, where q =1= O. This shows that x is rational. And for the fourth conditional statement, suppose that x is rational, namely, equal to p /q for some integers p and q with q =1= O. Then we can. write x/2 = p/(2q), where 2q =1= O. This shows that x/2 is rational.

S-1 0

Answers to Odd-Numbered Exercises

No 3 7. Suppose that PI � P4 � P2 � Ps � P3 � To prove that one of these propositions implies any of the others, just use hypothetical syllogism repeatedly. 39. We will give a proof by contradiction. Suppose that a I , a2 , . . . , an are all less than A, where A is the average of these num­ bers. Then a I + a2 + . . . + an < n A . Dividing both sides by n shows that A = (a l + a2 + . . . + an )! n < A, which is a con­ tradiction. 4 1 . We will show that the four statements are equivalent by showing that (i ) implies (ii ), (ii ) implies (iii ), (iii ) implies (iv), and (iv) implies (i ). First, assume that n is even. Then n = 2k for some integer k. Then n + 1 = 2k + 1 , so n + 1 i s odd. This shows that (i ) implies (ii ) . Next, sup­ pose that n + 1 is odd, so n + 1 = 2k + 1 for some inte­ ger k. Then 3n + 1 = 2n + (n + 1 ) = 2(n + k) + 1 , which shows that 3n + 1 is odd, showing that (ii ) implies (iii ). Next, suppose that 3n + 1 is odd, so 3n + 1 = 2k + 1 for some integer k. Then 3n = (2k + 1 ) - 1 = 2k, so 3n is even. fhis shows that (iii ) implies (iv). Finally, suppose that n is !lot even. Then n is odd, so n = 2k + 1 for some integer �. Then 3n = 3(2k + 1 ) = 6k + 3 = 2(3k + 1 ) + 1 , so 3n is :>dd. This completes a proof by contraposition that (iv) im­ :>lies (i ).

35.

PI .

Section 1.7 l . 1 2 + 1 = 2 � 2 = 2 1 ; 2 2 + 1 = 5 � 4 = 22 ; 3 2 + 1 = lO � 8 = 2 3 ; 42 + 1 = 1 7 � 1 6 = 24 3. If x ::::: y, then nax(x , y) + min(x , y) = y + x = x + y. If x � y, then nax(x , y) + min(x , y) = x + y. Because these are the only wo cases, the equality always holds. 5. There are four cases. ':ase 1: x � 0 and y � O. Then Ix l + Iyl = x + y = Ix + Y I . ':ase 2 : x < 0 and y < O . Then Ix l + I y l = - x + (-y) = -(x + y) = Ix + y l because x + y < O. Case 3: x � 0 md y < O. Then Ix l + Iyl = x + (-y). If x � -y, then x + y l = x + y. But because y < 0, -y > y, so Ix l + Iyl = r + (-y» x + y = lx + y l . If x < -y, then Ix + yl = -(x + y) = -x + (-y). But because x � 0, x � -x, so x l + Iyl = X + (-y) � -x + (-y) = Ix + Y I . Case 4: x < 0 md y � O. Identical to Case 3 with the roles of x and y re­ {ersed. 7. 10,00 1 , 1 0,002, . . . , 1 0, 1 00 are all nonsquares, )ecause 1002 = 10,000 and 1 01 2 = 10,20 1 ; constructive. �. 8 = 2 3 and 9 = 3 2 1 1 . Let x = 2 and y = ../2 . If ,Y = 2 v'2 is irrational, we are done. lfnot, then letx = 2 v'2 and v'2 ) v'2/4 = 2 v'2· (v'2}/4 = 2 1 /2 = ../2. v = ../2/4. Then xY = (2 l 3. a) This statement asserts the existence of x with a certain )roperty. If we let y = x, then we see that P(x) is true. If y s anything other than x, then P(x) is not true. Thus, x is the mique element that makes P true. b) The first clause here .ays that there is an element that makes P true. The second :lause says that whenever two elements both make P true, hey are in fact the same element. Together these say that P is • atisfied by exactly one element. c) This statement asserts he existence of an x that makes P true and has the further )roperty that whenever we find an element that makes P true, hat element is x . In other words, x is the unique element hat makes P true. 1 5. The equation la - cl = Ib - cl is :quivalent to the disjunction of two equations: a - c = b - c

So l O

or a - c = -b + c. The first of these is equivalent to a = b, which contradicts the assumptions made in this problem, so the original equation is equivalent to a - c = -b + c. By adding b + c to both sides and dividing by 2, we see that this equation is equivalent to c = (a + b)/2. Thus, there is a unique solution. Furthermore, this c is an integer, because the sum of the odd integers a and b is even. 1 7. We are being asked to solve n = (k - 2) + (k + 3) for k. Using the usual, reversible, rules of algebra, we see that this equation is equivalent to k = (n - 1)/2. In other words, this is the one and only value of k that makes our equation true. Because n is odd, n - 1 is even, so k is an integer. 1 9. If x is itself an integer, then we can take n = x and E = O. No other solution is possible in this case, because if the integer n is greater than x, then n is at least x + 1 , which would make E � 1 . If x is not an integer, then round it up to the next integer, and call that integer n . Let E = n - x. Clearly 0 ::::: E < 1 ; this is the only E that will work with this n, and n cannot be any larger, because E is constrained to be less than 1 . 2 1 . The harmonic mean of distinct positive real numbers x and y is always less than their geometric mean. To prove 2xy/(x + y) < ,JXY, multiply both sides by (x + y)/(2.JXY) to obtain the equivalent inequality ,JXY < (x + y)/2, which is proved in Example 14. 23. The parity (oddness or evenness) of the sum of the numbers written on the board never changes, because j + k and U - kl have the same parity (and at each step we reduce the sum by j + k but increase it by U - kl). Therefore the integer at the end of the process must have the same parity as 1 + 2 + . . . + (2n) = n(2n + 1), which is odd because n is odd. 25. Without loss of generality we can assume that n is nonnegative, because the fourth power of an integer and the fourth power of its negative are the same. We divide an arbitrary positive integer n by 10, obtaining a quotient k and remainder I, whence n = 10k + I, and I is an integer between 0 and 9, inclusive. Then we compute n4 in each of these 10 cases. We get the following values, where X is some integer that is a multiple of 10, whose exact value we do not care about. (10k + 0)4 = 1O,000k4 = 1O,000k4 + 0, (10k + 1)4 = 1O,000k4 + X · k3 + X · � + X · k + 1 , (10k + 2)4 = 1O,000k4 + X . k3 + X . k2 + X . k + 16, (10k + 3)4 = 1O,000k4 + X . k3 + X . k2 + X . k + 8 1 , (10k + 4)4 = 1O,000k4 + X . k3 + X . k2 + X . k + 256, (10k + 5)4 = IO,OOOk4 + X . k3 + X . k2 + X . k + 625, (10k + 6)4 = 1O,000k4 + X . k3 + :x . k2 + X . k + 1296, (10k + 7)4 = 1O,000k4 + X · k3 + X · k2 + X · k + 240 1 , (10k + 8)4 = 1O,000k4 + X ·k3 + X · � + X · k + 4096, (10k + 9)4 = I O,OOOk4 + X . k3 + X . � + X . k + 6561 . Because each coefficient indicated by X i s a multiple of 10, the corresponding term has no effect on the ones digit of the answer. Therefore the ones digits are 0, 1, 6, 1 , 6, 5, 6, 1, 6, 1, respectively, so it is always a 0, 1, 5, or 6. 27. Because n 3 > 100 for all n > 4, we need only note that n = l , n = 2, n = 3, and n = 4 do not satisfy n 2 + n 3 = 100 . 29. Because 54 = 625, both x and y must be less than 5. Then x4 + y4 ::::: 44 + 44 = 5 1 2 < 625. 3 1 . If it is not true that a ::::: 4'n, b ::::: 4'n, or c ::::: 4'n, then a > 4'n, b > 4'n, and c > 4'n. Multiplying these inequalities of positive num­ bers together we obtain abc < (4'n)3 = n, which implies the

S- l l

Answers to Odd-Numbered Exercises

negation of our hypothesis that n = abc. 33. By finding a common denominator, we can assume that the given ra­ tional numbers are alb and c/b, where b is a positive in­ teger and a and c are integers with a < c. In particular, (a + 1)/b :::: c/b. Thus, x = (a + � J2)/b is between the two given rational numbers, because 0 < J2 < 2. Furthermore, x is irrational, because if x were rational, then 2( bx - a) = J2 would be as well, in violation of Example 10 in Section 1 .6. 35. a) Without loss of generality, we can assume that the x sequence is already sorted into nondecreasing order, because we can relabel the indices. There are only a finite number of possible orderings for the Y sequence, so if we can show that we can increase the sum (or at least keep it the same) whenever we find Yi and Yj that are out of order (i.e., i < j but Yi > Yj ) by switching them, then we will have shown that the sum is largest when the Y sequence is in nonde­ creasing order. Indeed, if we perform the swap, then we have added Xi Yj + XjYi to the sum and subtracted Xi Yi + XjYj · The net effect is to have added Xi Yj + XjYi - Xi Yi - XjYj = (Xj - Xi )(Yi - Yj ), which is nonnegative by our ordering as­ sumptions. b) Similar to part (a) 37. a) 6 � 3 � 10 � 5 � 16 � 8 � 4 � 2 � 1 b) 7 � 22 � 1 1 � 34 � 17 � 52 � 26 � 13 � 40 � 20 � 10 � 5 � 16 � 8 � 4 � 2 � 1 c) 17 � 52 � 26 � 1 3 � 40 � 20 � 10 � 5 � 16 � 8 � 4 � 2 � 1 d) 2 1 � 64 � 32 � 16 � 8 � 4 � 2 � 1 39. Without loss of gener­ ality, assume that the upper left and upper right comers of the board are removed. Place three dominoes horizontally to fill the remaining portion ofthe first row, and fill each of the other seven rows with four horizontal dominoes. 4 1. Because there is an even number of squares in all, either there is an even number of squares in each row or there is an even num­ ber of squares in each column. In the former case, tile the board in the obvious way by placing the dominoes horizon­ tally, and in the latter case, tile the board in the obvious way by placing the dominoes vertically. 43. We can rotate the board if necessary to make the removed squares be 1 and 16. Square 2 must be covered by a domino. If that domino is placed to cover squares 2 and 6, then the following domino placements are forced in succession: 5-9, 13- 14, and 10- 1 1 , at which point there is no way to cover square 1 5 . Otherwise, square 2 must be covered by a domino placed at 2-3. Then the following domino placements are forced: 4-8, 1 1 -12, 6-7, 5-9, and 10-14, and again there is no way to cover square 15. 45. Remove the two black squares adjacent to a white comer, and remove two white squares other than that comer. Then no domino can cover that white comer. 47. a) (1)

EE

(2)

% p q � (3)

(4)

(5)

b)

S-l 1

The picture shows tilings for the first four patterns. 3

1-t=r=+lR=-l-I+=-+I=!=-j

To show that pattern 5 cannot tile the checkerboard, label the squares from 1 to 64, one row at a time from the top, from left to right in each row. Thus, square 1 is the upper left comer, and square 64 is the lower right. Suppose we did have a tiling. By symmetry and without loss of generality, we may suppose that the tile is positioned in the upper left comer, covering squares 1 , 2, 10, and 1 1 . This forces a tile to be adjacent to it on the right, covering squares 3, 4, 12, and 13 . Continue in this manner and we are forced to have a tile covering squares 6, 7, 15, and 16. This makes it impossible to cover square 8. Thus, no tiling is possible. Supplementary Exercises

b) q 1\ P c) -.q v -'p d) q B- P 1. a) q � p 3. a) The proposition cannot be false unless -'p is false, so p

is true. If p is true and q is true, then -.q 1\ (p � q) is false, so the conditional statement is true. If p is true and q is false, then p � q is false, so -.q 1\ (p � q) is false and the conditional statement is true. b) The proposition cannot be false unless q is false. If q is false and p is true, then (p v q) 1\ -'p is false, and the conditional statement is true. If q is false and p is false, then (p v q) 1\ -'p is false, and the conditional state­ ment is true. 5. -.q � -'p; p � q; -.p � -.q 7. (p 1\ q 1\ r 1\ -'s) v (p 1\ q 1\ -'r 1\ s) v (p 1\ -.q 1\ r 1\ s ) v (-'P 1\ q 1\ r 1\ s) 9. Translating these statements into symbols, using the obvious letters, we have -.[ � -'g, -.g � -'q, r � q, and -.[ 1\ r. Assume the statements are consistent. The fourth statement tells us that -.[ must be true. Therefore by modus ponens with the first statement, we know that -.g is true, hence (from the second state­ ment), that -.q is true. Also, the fourth statement tells us that r must be true, and so again modus ponens (third state­ ment) makes q true. This is a contradiction: q 1\ -'q . Thus the statements are inconsistent. 1 1 . Brenda 1 3. The premises cannot both be true, because they are contradictory. There­ fore it is (vacuously) true that whenever all the premises are true, the conclusion is also true, which by definition makes this a valid argument. Because the premises are not both true, we cannot conclude that the conclusion is true. 1 5. a) F b) T c) F d) T e) F t) T 1 7. Many an­ swers are possible. One example is United States senators.

S-12

S- / 2

Answers to Odd-Numbered Exercises

1 9. 'v'x3y3z (y =1= Z I\ 'v'w(P(w , x) � (w = y v w = z» ) 2 1 . a) -.3x P (x) b) 3x(P(x) 1\ 'v'y(P(y) � y = x» c) 3x 1 3x2 (P(x d 1\ P (X2) 1\ X I =1= X2 1\ 'v'y (P(y) � (y

= X I vy = X2» ) d) 3x I 3 X2 3 X3 (P(X I ) I\ P (X2) I\ P (X3 )I\X I =1= X3 1\ X2 =1= X3 1\ 'v'y(P(y) � (y = X I V = X V Y = X3 » ) 23. Suppose that 3x(P(x) � Q(x» 2 Y =1= X2 1\ X I

is true. Then either Q (xo ) is true for some xo , in which case 'v'x P (x) � 3x Q (x) is true; or P (xo ) is false for some xo , in which case 'v'x P (x) � 3x Q (x) is true. Con­ versely, suppose that 3x(P(x) � Q (x» is false. That means that 'v'x(P(x) 1\ -. Q (x» is true, which implies 'v'x P (x) and 'v'x(-. Q (x» . This latter proposition is equivalent to 25. No -.3x Q (x). Thus, 'v'x P (x) � 3x Q(x) is false. 27. 'v'x 'v'z 3y T (x , y, z) , where T (x , y, z) is the statement that student X has taken class y in department z , where the domains are the set of students in the class, the set of courses at this university, and the set of departments in the school of mathematical sciences 29. 3!x3!y T (x , y) and 3x'v'z«3y'v'w(T(z, w) � w = y» � z = x), where T (x , y) means that student X has taken class y and the domain is all students in this class 3 1 . P (a) � Q (a) and Q (a) � R(a) by universal instantiation; then -. Q (a) by modus tollens and ..... P (a) by modus tollens 33. We give a proof by contra­ position and show that if "fi is rational, then X is rational, assuming throughout that X � 0. Suppose that "fi = p/q is rational, q =1= 0. Then X = ("fi)2 = p 2 /q 2 is also rational (q 2 is again nonzero). 35. We can give a constructive proof by letting m = 1 05 00 + 1 . Then m 2 = ( 1 0 500 + 1 )2 > ( 1 05 00)2 = 1 0 1 000 . 37. 23 cannot be written as the sum of eight cubes. 39. 223 cannot be written as the sum of 36 fifth powers.

CHAPTER 2 S ection 2.1

I . a) {- I , I } b) { I ,2, 3,4, 5,6,7,8,9, 1O, 1 l } c) {0, 1 ,4, 9, 16, 3. a) Yes l5, 36, 49, 64, 8 1 } d) 0 b) No c) No b) No c) Yes d) No e) No t) No 5. a) Yes 7. a) False b) False c) False d) True e) False t) False �) True 9. a) True b) True c) False d) True �) True

II.

u I S. Suppose that x E A. Because A � B , this implies that x E B . Because B � C, we see that x E C . Because x E A implies that x E C, it follows that A � C . 1 7. a) I

b) 1 c) 2 d) 3 1 9. a) {0, {a}} b) {0, {a}, {b}, {a , b}} c) { 0, {0} , {{0} } , ( 0, {0} } } b) 16 c) 2 2 1 . a) 8 2 3 . a) {(a , y) , (b, y) , (e, y) , (d, y) , (a, z) , (b, z) , (e, z) , (d, z)} b) {(y, a), (y , b), (y , c), (y , d), (z, a), (z, b), (z, c), (z, d)} 25. The set of triples (a, b, c) , where a is an airline and b and e are cities 27. 0 x A = {(x , y) l x E 0 and y E A } = 0 = { (x , y) I x E A and y E 0} = A x 0 29. mn 31 . The elements of A x B x C consist of 3-tuples (a, b, c), where a E A , b E B, and e E C, whereas the elements of (A x B) x C look like «a, b), e}-ordered pairs, the first

coordinate of which is again an ordered pair. 33. a) The square of a real number is never - I . True b) There exists an integer whose square is 2. False c) The square of every integer is positive. False d) There is a real number equal to its own square. True 3 5. a) {- I , 0, I } b) Z - to, I } c) 0 37. We must show that { {a}, {a, b}} = {{e}, {e, d}} if and only if a = e and b = d. The "if" part is immediate. So assume these two sets are equal. First, consider the case when a =1= b. Then { {a} , {a, b}} contains exactly two elements, one of which contains one element. Thus, { {e}, {e, d}} must have the same property, so e =1= d and {e} is the element containing exactly one element. Hence, {a} == Ie}, which implies that a = e. Also, the two-element sets {a, b} and {e, d} must be equal. Because a = e and a =1= b, it follows that b = d. Sec­ ond, suppose that a = b. Then {{a}, {a, b}} = {{a}}, a set with one element. Hence, { { e } , {e, d}} has only one element, which can happen only when e = d, and the set is { {ell. It then fol­ lows that a = e and b = d. 39. Let S = {aI , a2 , . . . , an }. Represent each subset of S with a bit string of length n, where the ith bit is I if and only if aj E S. To generate all subsets of S, list a1l 2n bit strings of length n (for instance, in increasing order), and write down the corresponding subsets.

t) False

S ection 2.2 January March

February

April

October

December

1 3. The dots in certain regions indicate that those regions are lot empty.

I. a) The set of students who live within one mile of sc:hool and who walk to classes b) The set of students who live within one mile of school or who walk to classes (or who do both) c) The set of students who live within one mile of school but do not walk to classes d) The set of students who walk to classes but live more than one mile away from school 3. a) {O, 1 ,2,3,4,5,6} b) {3} c) { l , 2, 4,5} d) to, 6} 5. A = (x I -'(x E A) } = (x I -.(-.x E A ) } = {x I x E A } = A 7. a) A U U = {x I x E A v x E U } = {x I x E A v T} = {x I T} = U b) A n 0 = {x I x E A 1\ x E 0} = {x I x E

Answers to Odd-Numbered Exercises S-13

S- / 3

A 1\ F} = { x I F} = 0 9. a) A U A = {x I x E A v x ¢ A} = U b) A n A = {x I x E A 1\ x ¢ A } = 0 1 1. a) A U B = {x I x E A v x E B } = {x I x E B v x E A } = B U A b) A n B = {X I X E A I\ x E B } = {x l x E B I\ X E A } = B n A 13. Suppose x E A n (A U B). Then x E A and x E A U B by the definition of intersection. Because x E A,

we have proved that the left-hand side is a subset of the right-hand side. Conversely, let x E A . Then by the defini­ tion of union, x E A U B as well. Therefore x E A n (A U B ) by the definition o f intersection, s o the right-hand side i s a subset of the left-hand side. 1 5. a) x E (A U B) := x ¢

(A U B) := ..... (x E A v x E B) := ..... (x E A) 1\ (x E B ) := x ¢ A l\ x ¢ B := X E A I\ X E Ii := X E An Ii .....

b)

A

B

AUB

(A U B)

A

B

AnB

I I 0 0

I 0 1 0

I I 1 0

0 0 0 1

0 0 1 1

0 I 0 1

0 0 0 1

1 7. a) x E A n B n e := x ¢ A n B n e := x ¢ A v x ¢ B v x ¢ e :=x E Av X E B v x E C := X E AU Ii u c b)

A B C A n B n C (A n B n C) A B C A U B U C

1 I I I 0 0 0 0

1 1 0 0 I I 0 0

I 0 I 0 1 0 1 0

1 0 0 0 0 0 0 0

0 I I 1 1 1 1 1

0 0 0 0 1 1 1 1

0 0 I 1 0 0 1 I

0 1 0 1 0 I 0 1

0 1 1 1 I I I 1

19. Both sides equal {x I x E A 1\ X ¢ B } . 2 1 . x E A U (B U e) := (x E A) v (x E (B U e» := (x E A) v (x E B v X E e) := (x E A v X E B) v (x E e) := X E (A U B ) U e 23. x E A U (B n e) := (x E A) v (x E (B n e » := (x E A) v (x E B 1\ x E e) := (x E A v x E B ) 1\ (x E A v x E e) := x E (A U B) n (A U e) 25. a) {4, 6 } b) {0, 1 , 2 , 3 , 4, 5 , 6, 7 , 8 , 9, 1O} c ) {4, 5 , 6, 8 , 10} d ) {0,2,4, 5 , 6 , 7 , 8 , 9 , 1O} 27. a) The double-shaded portion i s the desired

set.

c) The desired set is the entire shaded portion.

29. a) B � A b) A � B c) A n B = 0 d) Nothing, be­ cause this is always true e) A = B 3 1 . A � B := Vx(x E

A � x E B ) := Vx(x ¢ B � x ¢ A) := Vx(x E Ii � x E A) := Ii � If 33. The ·set of students who are computer

science majors but not mathematics majors or who are math­ ematics majors but not computer science majors 35. An element is in (A U B ) - (A n B ) if it is in the union of A and B but not in the intersection of A and B , which means that it is in either A or B but not in both A and B . This is exactly what it means for an element to belong to A E9 B . 37. a) A E9 A = ( A - A ) U ( A - A ) = 0 U 0 = 0 b) A E9 o = (A - 0) U (0 - A) = A U 0 = A c) A E9 U = (A - U) U (U - A) = 0 U A = A d) A E9 A = (A - A) U (A - A) = A U A = U 4 1 . Yes 43. Yes 39. B = 0

b) {O, I } 45. a) { 1 , 2, 3 , . . . , n } b) { l } 47. a) A n c) R, [- I , I ] b) Z - {0} , 0 49. a) Z, { - 1 , 0, 1 } d) [ 1 , 00), 0 5 1 . a) { l , 2, 3 , 4, 7, 8, 9, 1 O} b) { 2 , 4, 5 , 6, 7 } c) { I , 1 O} 53. The bit in the ith position o f the bit string of the difference of two sets is · 1 if the ith bit of the first string is I and the ith bit of the second string is 0, and is 0 otherwise. 55. a) 1 1 1 1 1 0 0000 0000 0000 0000 0000 v 0 I 1 1 00 1 000 0000 0 1 00 0 1 0 1 0000 = 1 1 1 1 1 0 1 000 0000 0 1 00 0 1 0 1 0000, representing {a, b, c, d, e, g, p , I , v} b) 1 1 1 1 1 0 0000 0000 0000 0000 0000 1\ 0 1 1 1 00 1 000 0000 0 1 00 0 1 0 1 0000 = 0 1 1 1 00 0000 0000 0000 0000 0000, representing { b , c, d } c) ( 1 1 1 1 1 0 0000 0000 0000 0000 0000 v 00 0 1 1 0 0 1 1 0 000 1 1 000 0 1 1 0 0 1 1 0) 1\ (0 1 1 1 00 1 000 0000 0 1 00 0 1 0 1 0000 v 00 1 0 1 0 00 1 0 0000 1 000 00 1 0 0 1 1 1 ) = 1 1 1 1 1 0 0 1 1 0 000 1 1 000 O l I O O l I O 1\ 0 1 1 1 1 0 1 0 1 0 0000 1 1 00 0 1 1 1 0 1 1 1 = 0 1 1 1 1 0 00 1 0 0000 1 000 0 1 1 0 0 1 1 0, repre­ senting {b, c, d, e, i, o, l , u , x , y} d) 1 1 1 1 1 0 0000 0000 0000 0000 0000 v 0 1 1 1 00 1 000 0000 0 1 00 0 1 0 1 0000 v 00 1 0 1 0 00 1 0 0000 1 000 00 1 0 0 1 1 1 v 00 0 1 1 0 0 1 1 0 000 1 1 000 0 1 1 0 0 1 1 0 = 1 1 1 1 1 0 1 1 1 0 000 1 1 1 00 0 1 1 1 0 1 1 1 , representing {a , b,c,d, e,g, h , i , n , o , p , 1 , u , v , x ,y,z} 57. a) { l , 2, 3 , { l , 2, 3}} b) {0} c) {0, {0}} d) {0, {0} , {0, {0} } } 59. a) {3 . a , 3 . b, 1 . c, 4 · d} b) {2 . a , 2 · b} c) {I . a, 1 . c} d) {I . b, 4 · d} e) {5 . a , 5 · b, 1 · c, 4 · d} 61. If = {OA Alice, 0. 1 Brian, 0.6 Fred, 0.9 Oscar, 0.5 Rita}, Ii = {0.6 Alice, 0.2 Brian, 0.8 Fred, 0. 1 Oscar, 0.3 Rita} 63. F n R = {OA Alice, 0.8 Brian, 0.2 Fred, 0. 1 Oscar, 0.5 Rita}

b) The desired set is the entire shaded portion. S ection 2.3

1. a) f(O) is not defined. b) f(x) is not defined for < o. c) f(x) is not well-defined because there are two

x

S-1 4

Answers to Odd-Numbered Exercises

distinct values assigned to each x . 3. a) Not a func­ tion b) A function e) Not a function 5. a) Domain the set of bit strings; range the set of integers b) Domain the set of bit strings; range the set of even nonnegative integers e) Domain the set of bit strings; range the set of nonnegative integers not exceeding 7 d) Domain the set of positive integers; range the set of squares of pos­ itive integers = { 1 . 4. 9. 1 6 . . . . } 7. a) Domain Z+ x Z+ ; range Z + b ) Domain Z+ ; range to . 1 . 2. 3 . 4. 5 . 6. 7. 8. 9 } e) Domain the set o f bit strings; range N d) Domain the set of bit strings; range N 9 . a) I b) 0 e) 0 d) - I e) 3 t) - I g) 2 b) I 1 1 . Only the function in part (a) 1 3. Only the functions in parts (a) and (d) 15. a) Onto b) Not onto e) Onto d) Not onto e) Onto 1 7. a) The function f(x) with f(x) = 3x + I when x � o and f(x) = -3x + 2 when x < O b) f(x) = lx l + 1 e) The function f(x) with f(x) = 2x + 1 when x � 0 and f(x) = -2x when x < 0 d) f(x) = x 2 + I 19. a) Yes b) No e) Yes d) No 2 1 . Suppose that f is strictly decreasing. This means that f(x) > f(y) whenever x < y. To show that g is strictly increasing, suppose that x < y. Then g(x) = I /f(x) < I /f(y) = g(y). Conversely, suppose that g is strictly increasing. This means that g(x) < g(y) whenever x < y. To show that f is strictly decreasing, suppose that x < y. Then f(x) = I /g(x) > I /g(y) = f(y). 23. Many answers are possible. One example is f(x) = 1 7. 25. The function is not one-to-one, so it is not invertible. On the restricted domain, the function is the identity function on the nonnegative real numbers, f(x) = x, so it is its own inverse. 27. a) f(S) = to . I , 3 } b) f(S) = to . 1 , 3 . 5 . 8} e) f(S) = t o , 8 . 16. 40} d) f(S) = { I . 12, 33. 65} 29. a) Let x and y be distinct elements of A . Because g is one­ to-one, g(x) and g(y) are distinct elements of B . Because f is one-to-one, f(g(x» = (f 0 g)(x) and f(g(y» = (f 0 g)(y) are distinct elements of C. Hence, f o g is one-to-one. b) Let y E C. Because f is onto, y = f(b) for some b E B . Now because g is onto, b = g(x) for some x E A . Hence, y = f(b) = f(g(x» = (f 0 g)(x). It follows that f o g is onto. 3 1 . No. For example, suppose that A = {a } , B = {b, c}, and C = {d}. Let g(a) = b. f(b) = d, and f(c) = d. Then f and f o g are onto, but g is not. 33. (f + g)(x) = x 2 + X + 3, (fg)(x) = x 3 + 2x 2 + X + 2 35. f is one-to-one because f(X l ) = f(X2 ) -+ aX I + b = ax2 + b -+ aX I = aX2 -+ Xl = X2 . f is onto because f«y - b)/a) = y.f- l (y) = (y - b)/a. 37. Let f( l ) = a . f(2) = a . Let S = { I } and T = {2}. Then f(S n T) = f(0) = 0, but f(S) n f(T) = {a} n {a} = {a} . 39. a) {x I O .:s x < I } b) {x l - I .:s x < 2} e) 0 4 1 . f- I (S) = {x E A I f(x) ¢ S} = {x E A I f(x) E S} = f- I (S) 43. Let x = LxJ + E, where E is a real number with 0 .:s E < 1 . If E < ! , then LxJ - 1 < x - ! < LxJ , so rx - ! 1 = LxJ and this is the integer closest to x . If E > ! , then LxJ < x - ! < LxJ + I , so rx - !l = LxJ + 1 and this is the integer closest to x . If E = ! , then rx - ! 1 = Lx J , which is the smaller of the two integers that surround x and are the same distance from x . 45. Write the real number x as LxJ + E, where E is a real number with 0 .:s E < 1 . Because E = X - LxJ , it follows that O .:s - LxJ < 1 . The first two in-

S- / 4

equalities, x - I < LxJ and LxJ .:s x, follow directly. For the other two inequalities, write x = rxl - E', where 0 .:s E' < 1 . Then 0 .:s rx l - x < I , and the desired inequality follows. 47. a) If x < n, because LxJ .:s x, it follows that LxJ < n. Suppose that x � n . By the definition of the floor function, it follows that LxJ � n . This means that if LxJ < n, then x < n o b) If n < x, then because x .:s rxl , it follows that n .:s rxl Suppose that n � x . By the definition of the ceiling func­ tion, it follows that r xl .:s n . This means that if n < rxl , then n < x . 49. If n is even, then n = 2k for some integer k. Thus, Ln/2J = LkJ = k = n/2. If n is odd, then n = 2k + I for some integer k. Thus, Ln/2J = Lk + ! J = k = (n - 1 )/2. 51. Assume that x � O. The left-hand side is r -xl and the right-hand side is - LxJ . Ifx is an integer, then both sides equal '-x . Otherwise, let x = n + E, where n is a natural number and E is a real number with 0 .:s E < 1 . Then r -xl = r -n El = -n and - LxJ = - Ln + EJ = -n also. When x < 0, the equation also holds because it can be obtained by substitut­ ing -x forx . 53. rbl - LaJ - I 55. a) 1 b) 3 e) 126 d) 3600 57. a) 1 00 b) 256 e) 1 030 d) 30,200 4

59.

61.

I-I I -I

e-o

2

e-o

-2

e-o

63. a)

3

2

0

-2

4

-I -I

2

0

-2

-2

-3

---0

I11j_I -1 "-I 1'�L-I--o--O''I ! �I j_1 ' 1 -I b)

--0

2

--0

--0

I -4

I

I

j

-2 --0

--0

2

I

I 4

a--

--0

I

I 9

I 2

-3

4

-I -I

-2

-3

-4

..,

3

-.!......-

-2

..,

t) ....,

3

-3

d)

a--

4

-2

-2

--0

-2

a--

e)

--0

-2

I

- ' H -U \,

I 2

-3

--0

e)

I

0----0

-3

4

3

-2

-I ' 1_1 I

....,

-2

-3

-4

g) See part (a). 65. f- I (y) = (y - 1 )1 / 3 67. a) /AnB (X) = I � x E A n B � x E A and x E B � /A (x) = I and

Answers to Odd-Numbered Exercises S-15

S- / 5

h(x) = 1 ++ /A(x)h (x) = 1 b) /AuB(X) = 1 ++ X E A U B ++ x E A or x E B ++ /A (x) = 1 or h(x) = 1 ++ /A (x) + h(x) - /A (x)h(x) = 1 c) fx(x) = 1 ++ x E A ++ x ¢ A ++ /A(x) = O ++ I - fA(x) = 1 d) /AE!)B(X) = 1 ++ x E A EB B ++ (x E A and x ¢ B) or (x ¢ A and x E B) ++ /A(x) + h (x) - 2/A(x)h (x) = 1 69. a) True; because lxJ is already an integer, r lxJl = lxJ . b) False; x = ! is a counterexample. c) True; if x or y is an integer, then by property 4b in Table 1 , the difference is o. If neither x nor y is an integer, then x = n + E and y = m + 8, where n and m

are integers and E and 8 are positive real numbers less than 1 . Then m + n < x + y < m + n + 2, so rx + y 1 is either m + n + I or m + n + 2. Therefore, the given expression is either (n + 1) + (m + 1) - (m + n + 1) = 1 or (n + 1) + (m + 1 ) - (m + n + 2) = 0, as desired. d) False; x = ! and y = 3 is a counterexample. e) False; x = ! is a coun­ terexample. 7 1 . a) If x is a positive integer, then the two sides are equal. So suppose that x = n 2 + m + E , where n 2 is the largest perfect square less than x , m is a nO egative integer, and 0 < E ::: 1 . Then both ..;x and .J[XJ = n 2 + m are between n and n + 1 , so both sides equal n . b) If x is a positive integer, then the two sides are equal. So suppose that x = n 2 - m - E , where n 2 is the smallest perfect square greater than x , m is a nonnegative integer, and E is a real num­ ber with 0 < E ::: 1 . Then both ..;x and .JTXT = .Jn 2 - m are between n - 1 and n . Therefore, both sides of the equation equal n . 73 . a) Domain is Z; codomain is R; domain of definition is the set of nonzero integers; the set of values for which f is undefined is {O} ; not a total function. b) Domain is Z; codomain is Z; domain of definition is Z; set of values for which f is undefined is 0; total function. c) Domain is Z x Z; codomain is Q; domain of definition is Z x (Z - (OJ); set of values for which f is undefined is Z x (OJ; not a total function. d) Domain is Z x Z; codomain is Z; domain of definition is Z x Z; set of values for which f is undefined is 0; total function. e) Domain is Z x Z; codomain is Z; domain of definitions is {( m , n) 1 m > n } ; set of values for which f is undefined is {(m , n) 1 m ::: n } ; not a total func­ tion. 75. a) By definition, to say that S has cardinality m is to say that S has exactly m distinct elements. Therefore we can assign the first object to 1 , the second to 2, and so on. This provides the one-to-one correspondence. b) By part (a), there is a bijection f from S to { I , 2, . . . , m } and a bijection -I g from T to { I , 2, . . . , m I . Then the composition g 0 f is the desired bijection from S to T . 77. It is clear from the formula that the range of values the function takes on for a fixed value ofm + n, say m + n = x, is (x - 2)(x - 1 )/2 + 1 through (x - 2)(x - 1)/2 + (x - 1), because m can assume the values 1 , 2, 3 , . . . , (x - 1 ) under these conditions, and the first term in the formula is a fixed positive integer when m + n is fixed. To show that this function is one-to-one and onto, we merely need to show that the range of val­ ues for x + 1 picks up precisely where the range of values for x left off, i.e., that f(x - 1 , 1 ) + 1 = f( l , x). We have x2 -; + 2 = f(x - 1 , 1 ) + 1 = (X - 2 )�X - I ) + (x - 1 ) + 1 I)x (X -; + 1 = f( I , x).

j

S ection 2.4

l. a) 3 b) - I c) 787 d) 2639 3. a) ao = 2, aI = 3, b) ao = I , a I = 4, a2 = 2 7 , a3 = 256 a2 = 5, a3 = 9 c) ao = 0, al = 0, a2 = 1, a3 = 1 d) ao = 0, al = 1 , 5. a) 2, 5 , 8, 1 1 , 14, 17, 20, 23 , 26, 29 a2 = 2, a3 = 3 b) 1 , 1 , 1 , 2, 2, 2, 3 , 3 , 3 , 4 c) 1 , 1 , 3 , 3 , 5 , 5, 7, 7, 9, 9 d) - 1 , -2, -2, 8, 88, 656, 49 1 2, 40064, 362368, 3627776

e) 3, 6, 12, 24, 48, 96, 1 92, 384, 768, 1 536 t) 1 , 1 , 2, 3 , 5 , 8, 1 3 , 2 1 , 34, 55 g) 1 , 2, 2, 3 , 3 , 3 , 3 , 4, 4, 4 b) 3 , 3 , 5 , 4 , 4, 3 , 5 , 5 , 4, 3 7 . Each term could b e twice the previous term; the nth term could be obtained from the previous term by adding n - 1 ; the terms could be the positive integers that are not multiples of 3; there are infinitely many other possibilities. 9. a) One 1 and one 0, followed by two Is and two Os, followed by three 1 s and three Os, and so on; 1 , 1 , 1 b) The positive integers are listed in increasing order with each even positive integer listed twice; 9, 1 0, 1 0. c) The terms in odd-numbered locations are the successive powers of2; the terms in even-numbered locations are all 0; 32, 0, 64. d) an = 3 · 2n - I ; 384, 768, 1 536 e) an = 15 - 7(n - 1) = t) a n = (n 2 + n + 4)/2; 57, 22 - 7n ; -34, -4 1 , -48 3 68, 80 g) an = 2n ; 1 024, 1458, 2000 b) an = n ! + 1 ; 36288 1 , 362880 1 , 399 1 680 1 1 1 . Among the integers 1 , 2, . . . , an , where an is the nth positive integer not a perfect square, the nonsquares are a I ,a2 , . . . ,an and the squares are 1 2 , 22 , . . . , k2 , where k is the integer with k2 < n + k < (k + 1 )2 . Consequently, an = n + k, where k2 < an < (k + 1 )2 . To find k, first note that k2 < n + k < (k + 1 )2 , so k2 + 1 ::: n + k ::: (k + 1 ) 2 - 1 . Hence, (k - ! )2 + i = k2 - k + 1 ::: n ::: k2 + k = (k + !)2 - ! . It follows that k - ! < y'n < k + ! , so k = (y'n} and an = n + k = n + {y'n} . 13. a) 20 b) 1 1 c) 30 d) 5 1 1 1 5. a) 1 533 b) 5 1 0 c) 4923 1 7. a) 2 1 d) 1 8 d) 9842 b) 7 8 c) 1 8 19. L:j= I (aj - aj-I ) = a n - a o 2 1 . a) n 2 b) n(n + 1 )/2 23. 1 5 1 50 25. n (n + I �(2n + I ) + n (nt ) + (n + I )(m (n + 1 )2 + 1 ), where n = l.jmJ - 1 27. a) 0 b) 1 680 c) 1 d) 1 024 29. 34 3 1 . a) Countable, - 1 , -2, -3, -4, . . . b) Countable, 0, 2, -2, 4, -4, . . . c) Uncountable 33. a) Countable: d) Countable, 0, 7, -7, 14, - 14, . . . match n with the string of n 1 s. b) Countable. To find a correspondence, follow the path in Example 20, but omit fractions in the top three rows (as well as continu­ ing to omit fractions not in lowest terms). c) Uncountable d) Uncountable 35. Assume that A - B is countable. Then, because A = (A - B) U B , the elements of A can be listed in a sequence by alternating elements of A - B and elements of B . This contradicts the uncountability of A. 37. Assume that B is countable. Then the elements of B can be listed as b I . b2 , b3 , Because A is a subset of B , taking the sub­ sequence of {bn } that contains the terms that are in A gives a listing of the elements of A. Because A is uncountable, this is impossible. 39. We are given bijections f from A to B and g from C to D . Then the function from A x C to B x D that sends (a, c) to (f(a), g(c» is a bijection. 4 1 . Suppose that A I , A 2 , A 3 , . . . are countable sets. Because Ai is countable, • • • •

S-1 6

Answers to Odd-Numbered Exercises

S- J 6

we can list its elements in a sequence as ai l , ai2 , ai 3 , . . . . The elements of the set U7= 1 A i can be listed by listing all terms aij with i + j = 2, then all terms aij with i + j = 3, then all terms aij with i + j = 4, and so on. 43. There are a finite number of bit strings of length m , namely, 2m • The set of all bit, strings is the union of the sets of bit strings oflength m for m = 0, I , 2, . . . . Because the union of a countable number of countable sets is countable (see Exercise 4 1 ), there are a countable number of bit strings. 45. For any finite alphabet there are a finite number of strings of length n, whenever n is a positive integer. It follows by the result of Exercise 4 1 that there are only a countable number of strings from any given finite alphabet. Because the set of all computer programs in a particular language is a subset of the set of all strings of a finite alphabet, which is a countable set by the result from Exercise 36, it is itself a countable set. 47. Exercise 45 shows that there are only a countable number of computer programs. Consequently, there are only a countable number of computable functions. Because, as Exercise 46 shows, there are an uncountable number of functions, not all functions are computable.

( m + r - 1 )/mJ are all just n and the terms from Lx + (m r)/ m J on are all n + 1 . Therefore, the right-hand side is (m r)n + r(n + I ) = nm + r, as well. 29. W I 3 1 . a l = I ; a2n + 1 = n . a2n for all n > 0 ; and a2n = n + a2n - 1 for all n > O. The next four terms are 5346, 5353, 3747 1 , and

37479.

CHAPTER 3 Section 3.1

1. max := I, i : = 2, max : = 8, i : = 3, max : = 12, i := 4, i : = 5, i : = 6, i : = 7, max := 14, i : = 8, i := 9, i := 10,

i := 1 1 procedure sum(a l , . . . , an: integers)

3.

sum : = al for i : = 2 to n sum := sum + ai {sum has desired value} 5. procedure duplicates (a , a2 , . . . , an: I k

Supplementary Exercises

1. a) A

b) A n B

c) A - B

d) A n B

e) A $ B

3. Yes 5. A - (A - B ) = A - (A n B) = A n (A n B) = A n (AU B ) = (A n A) U (A n B ) = 0 U (A n B ) = A n B 7. Let A = ( I } , B = 0, C = { I }. Then (A - B ) - C = 0, but A - (B - C) = { I }. 9. No. For example, let A = B = {a , b}, C = 0, and D = {a}. Then (A - B) - (C - D) = 0 - 0 = 0, but (A - C) - (B - D) = {a, b} - {b} = {a} . b) 1 0 1 :::: 1 1 . a) 1 0 1 :::: I A n B I :::: I A I :::: I A U B I :::: l U I I A - B I :::: I A $ B I :::: I A U B I :::: I A I + I B I 13. a) Yes, no b) Yes, no c) f has inverse with f - I (a ) = 3 , f- I (b) = 4, f- I (C) = 2, f- I (d) = I ; g has no inverse. 15. Let f(a) = f(b) = I , f(c) = f(d) = 2, S = {a, c }, T = {b, d }. Then f(S n T) = f(0) = 0, but f(S) n f(T) = { I , 2} n { I , 2} = { 1 , 2 }. 17. Let x E A. Then Sf ( {x } ) = (f(y) l Y E {x} } = (f(x)}. By the same reasoning, Sg({x } ) = (g(x)}. Because Sf = Sg, we can conclude that (f(x)} = ( g(x ) }, and so nec­ essarily f(x) = g(x). 1 9. The equation is true if and only

if the sum of the fractional parts of x and y is less than 1 . The equation is true if and only if either both x and y are integers, or x is not an integer but the sum of the fractional parts of x and y is less than or equal to 1 . 23. If x is an integer, then LxJ + Lm - xJ = x + m - x = m . Otherwise, write x in terms of its integer and fractional parts: x = n + E, where n = LxJ and 0 < E < 1. In this case LxJ + Lm - xJ = 21.

Lm - n - EJ = n + m - n - I = m - 1 . 2 25. Write n = 2k + I for some integer k. Then n = 2 2 2 2 4t + 4k + 1 , so n /4 = k + k + ! . Therefore, rn /41 = k2 + k + 1 . But (n 2 + 3 ) / 4 = (4t2 + 4k + I + 3 ) / 4 = k2 + k + 1 . 27. Let x = n + (r / m ) + E , where n is an integer, r is a nonnegative integer less than m , and E is a real number with 0 :::: E < I / m . The left-hand side is Lnm + r + m E J = nm + r . On the right-hand side, the terms Lx J through Lx + Ln

+

EJ

+

nondecreasing order) := 0 {this counts the duplicates}

integers in

j := 2 while j :::: n begin if aj = aj_ 1 then begin k := k + I Ck := aj while U :::: n j := j + I

and aj

= Ck)

end j := j + I end { C I , C2 , . . . , Ck is the desired list} 7. procedure last even location(al ,a2 , . . . , an: integers)

k := O

for i : =10 I to n if ai is even then k := i end {k is the desired location (or 0 if there are no evens)} 9. procedure palindrome check(a l a2 . . . an: string) answer := true for i := I to Ln/2J if ai =I: an + l - i then answer := false end {answer is true iff string is a palindrome} 1 1 . procedure interchange(x , y: real numbers) Z

:= X

x := y Y := Z

The minimum number of assignments needed is three. 13. Linear search: i := I , i := 2, i := 3, i := 4, i := 5, i i := 7, location := 7; binary search: i := I , j := 8, m

i := 5, m := 6, i := 7, m := 7, j := 7, location := 7 15.

procedure insert(x , ai , a2 , . . . , an : integers)

{the list is in order: a l

an + ! : = x i := I

+1

:::: a2 :::: . . . :::: an }

:= :=

6,

4,

Answers to Odd-Numbered Exercises

S- / 7

> ai i := i + 1 for j := 0 to n - i an -j + 1 := an -j ai := x {x has been inserted into correct position}

1 7.

j := n while i < j - l begin 1 = l(i + j)/3J u = L2(i + j)/3J if x > au then i := u + 1 else if x > a1 then begin

procedure first largest(al , . . . , an: integers) max := al location := 1 for i := 2 to n begin if max begin


a2 then interchange al and a2 if a2 > a3 then interchange a2 and a3 if al > a2 then interchange al and a2 procedure onto(f: function from A to B where

A = {al , . . . , an }, B = {bl , . . . , bm } , al , . . . , an , bl , , bm are integers) for i := 1 to m hit(bi ) := 0 count := 0 for j := 1 to n if hit(f(aj » = 0 then

begin

count := count + 1 i := i + 1

end if count > modecount then begin modecount := count mode := value

• • •

end end 31.

begin

27.

procedure ones(a : bit string, a = a l a2 . . . an ) ones:= 0 for i := 1 to n begin if ai := 1 then

strings a } procedure ternary search(s : integer, a\ .a2 , . . . , an: increasing integers)



. . .

while i .:::: n and location = 0 begin j := I while j < i and location = 0 if ai = aj then location := i else j := j + 1

end if count = m then onto := true else onto := false

ones := ones + 1 end {ones is the number of ones in the bit

{mode is the first value occurring most often} procedure find duplicate(a I , a2 , an: integers) location := 0 i := 2

hit(f(aj » := 1 count := count + 1

25.

1

while i .:::: n begin

median := b; max := a; min := c

23.

1

procedure find a mode(al , a2 , . . . , an : nondecreasing integers) modecount := 0

i :=

end

21.

+

j := u end else j := I end if x = ai then location := i else if x = aj then location := j else location := 0 {location is the subscript of the term equal to x (0 if not found)}

ai then

end end 1 9. procedure mean-median-max-min(a , b, c: integers) mean := (a + b + c)/ 3 {the six different orderings of a , b, c with respect to ::: will be handled separately} if a > b then begin if b > c then

1

i :=

while x

8-17

i := i

end

+

1

{location is the subscript of the first value that

33.

repeats a previous value in the sequence} procedure find decrease(a l . a2 , an: positive integers) location := 0 . . . •

i := 2

while i .:::: n and location = 0

S-18

Answers to Odd-Numbered Exercises

if ai < ai - I then location := i else i := i + 1 {location is the subscript of the first value less than the immediately preceding one} 35. At the end of the first pass: 1 , 3, 5, 4, 7; at the end of the second pass: 1 , 3, 4, 5, 7; at the end of the third pass: 1 , 3, 4, 5, 7; at the end of the fourth pass: 1 , 3, 4, 5, 7 37. procedure better bubblesort (I'll , . . . , an: integers) i : = 1 ; done : = false while (i < n and done = false) begin done : = true for j : = 1 to n - i if aj > aj + 1 then begin interchange aj and aj +1 done : = false end i :=i+l

end {a I , . . . , an is in increasing order} 39. At the end ofthe first, second, and third passes: 1 , 3 , 5 , 7, 4; at the end of the fourth pass: 1 , 3 , 4, 5 , 7 41. a) 1 , 5, 4, 3, 2; 1 , 2, 4, 3, 5 ; 1 , 2, 3, 4, 5 ; 1 , 2, 3, 4, 5 b) 1 , 4, 3, 2, 5 ; 1 , 2, 3, 4, 5 ; 1 , 2, 3, 4, 5 ; 1 , 2, 3, 4, 5 c) 1 , 2, 3, 4, 5 ; 1 , 2, 3, 4, 5 ; 1 , 2, 3, 4, 5; 1 , 2, 3, 4, 5 43. We carry out the linear search algorithm given as Algo­ rithm 2 in this section, except that we replace x i= ai by x < ai , and we replace the else clause with else location := n + 1 . 45. 2 + 3 + 4 + . . . + n = (n 2 + n 2)/2 47. Find the lo­ cation for the 2 in the list 3 (one comparison), and insert it in front of the 3, so the list now reads 2, 3 , 4, 5 , 1 , 6. Find the location for the 4 (compare it to the 2 and then the 3), and insert it, leaving 2, 3 , 4, 5, 1, 6. Find the location for the 5 (compare it to the 3 and then the 4), and insert it, leaving 2, 3 , 4, 5 , 1 , 6. Find the location for the 1 (compare it to the 3 and then the 2 and then the 2 again), and insert it, leaving 1 , 2, 3 , 4, 5 , 6. Find the location for the 6 (compare it to the 3 and then the 4 and then the 5), and insert it, giving the final answer 1 , 2, 3 , 4, 5 , 6. 49. procedure binary insertion sort(a l ' a2 , . . . , an: real numbers with n � 2) for j := 2 to n begin {binary search for insertion location i } left := 1 right := j 1 while left < right begin -

-

middle := l(left + right)/2J if aj > am idd/e then left := middle + 1 else right := middle

end if aj < a/eft then i := left else i := left + 1 {insert aj in location i by moving ai through aj_ 1 toward back of list}

m := aj for k := 0 to j

-

i - I

S- J 8

aj - k := aj - k- I ai := m end {a I , a2 , . . . , an are sorted}

51. The variation from Exercise 50 53. a) Two quarters, one penny b) Two quarters, one dime, one nickel, four pen­ nies c) A three quarters, one penny d) Two quarters, one dime 55. Greedy algorithm uses fewest coins in parts (a), (c), and (d). a) Two quarters, one penny b) Two quar­ ters, one dime, nine pennies c) Three quarters, one penny d) Two quarters, one dime 57. a) The variable f will give the finishing time of the talk last selected, starting out with f equal to the time the hall becomes available. Order the talks in increasing order ofthe ending times, and start at the top of the list. At each stage of the algorithm, go down the list of talks from where it left off, and find the first one whose starting time is not less than f. Schedule that talk and update f to record its finishing time. b) The 9:00-9:45 talk, the 9:50-10: 1 5 talk, the 10: 1 5-1 0:45 talk, the 1 1 :00-1 1 : 1 5 talk 59. a) Here we assume that the men are the suitors and the women the suitees. procedure stable(M I , M2 , . . . , Ms , W I , W2 , ' Ws: preference lists) for i := 1 to s mark man i as rejected for i := I to s set man i 's rejection list to be empty for j := 1 to s set woman j 's proposal list to be empty while rejected men remain begin for i := 1 to s if man i is marked rejected then add i to the proposal list for the woman j who ranks highest on his preference list but does not appear on his rejection list, and mark i as not rejected for j := l to s if woman j 's proposal list is nonempty then remove from j 's proposal list all men i except the man io who ranks highest on her preference list, and for each such man i mark him as rejected and add j to his rejection list end for j := 1 to s match j with the one man on j 's proposal list {This matching is stable.} b) There are at most s 2 iterations of the while loop, so the algorithm must terminate. Indeed, if at the conclusion of the while loop rejected men remain, then some man must have been rejected and so his rejection list grew. Thus, each pass through the while loop, at least one more of the S 2 possible rejections will have been recorded, unless the loop is about to terminate. Furthermore, when the while loop terminates, each man will have one pending proposal, and each woman will have at most one pending proposal, so the assignment must be one-to-one. c) If the assignment is not stable, then there is a man m and a woman w such that m prefers w to the woman w ' with whom he is matched, and w prefers m to the man with whom she is matched. But m must have proposed • • •.

Answers to Odd-Numbered Exercises

5- 1 9

to w before he proposed to w ' , because he prefers the for­ mer. Because m did not end up matched with w , she must have rejected him. Women reject a suitor only when they get a better proposal, and they eventually get matched with a pending suitor, so the woman with whom w is matched must be better in her eyes than m , contradicting our original assumption. Therefore the marriage is stable. 6 1 . Run the two programs on their inputs concurrently and report which one halts.

S-19

C2 Ig(x)l wheneverx > k, where k = max(kl ,k2 ). Converseiy, if there are positive constants C I , C 2 , and k such that C J ig(x)1 :s I f(x)1 :s C2 Ig(x)1 for x > k, then taking kl = k2 = k shows that f(x) is both O (g(x» and 8(g(x» . 29. Y

Section 3.2

1. The choices of C and k are not unique. a) C

=

1, k = 10

b) C = 4, k = 7 c) No d) C = 5, k = 1 e) C = 1, k = 0 1) C = 1 , k = 2 3. X4 + 9x 3 + 4x + 7 :s 4X4 for all x >

9; witnesses C = 4, k = 9 5. (x 2 + I )/(x + 1 ) = x - I + 2/(x + 1) < x for all x > I ; witnesses C = I , k = 1 7. The choices of C and k are not unique. a) n = 3, C = 3, k = 1 b) n = 3, C = 4, k = 1 c) n = 1 , C = 2, k = 1 d) n = 0, C = 2, k = 1 9. x 2 + 4x + 1 7 :s 3x 3 for all x > 1 7, so x 2 + 4x + 17 is O(x 3 ), with witnesses C = 3, k = 17. How­ ever, if x 3 were O (x 2 + 4x + 1 7), then x 3 :s C (x 2 + 4x + 17) :s 3C x 2 for some C , for all sufficiently large x , which implies that x :s 3C for all sufficiently large x, which is im­ possible. Hence, x 3 is not 0 (x 2 + 4x + 1 7). 1 1 . 3x4 + 1 :s 4x4 = 8(x4/2) for all x > 1 , so 3x4 + 1 is 0 (x4 /2), with witnesses C = 8, k = 1 . Also x4/2 :s 3x4 + 1 for all x > 0, so x4/2 is O (3x4 + 1 ), with witnesses C = 1 , k = O. 13. Because 2n :s 3 n for all n > 0, it follows that 2n is O (3 n ), with witnesses C = 1, k = O. However, if 3 n were O (2n ), then for some C, 3 n :s C · 2 n for all sufficiently large n . This says that C ::: (3/2t for all sufficiently large n, which is impossible. Hence, 3 n is not O (2n ). 15. All functions for which there exist real numbers k and C with I f(x)1 :s C for x > k. These are the functions f(x) that are bounded for all sufficiently large x . 1 7. There are constants C I , C2 , kl ' and k2 such that I f(x)1 :s C J ig(x)1 for all x > kl and Ig(x)1 :s C2 Ih(x)1 for all x > k2 • Hence, for x > max(kl , k2 ) it follows that If(x)1 :s CJ ig(x)1 :s C I C2 Ih(x ) l . This shows that f(x) is O(h(x» . b) O (n5) 19. a) O (n 3 ) c) O(n 3 . n !) b) O (n 2 (log n)2 ) 2 1 . a) O (n 2 10g n) c) O(n 2n ) 23. a) Neither 8(x 2 ) nor Q(X 2 ) b) 8(x 2 ) and Q(x 2 ) c) Neither 8(x 2 ) nor Q(X 2 ) d) Q(x 2 ), but not 8(x 2 ) e) Q(x 2 ), but not 8(x 2 ) 1) Q(X 2 ) and 8(x 2 ) 25. If f(x) is 8(g(x » , then there exist constants C I and C2 with C I lg(x)1 :s If(x)1 :s C2 Ig(x)l. It follows that I f(x) l :s C2 Ig(x)1 and Ig(x)1 :s ( I / C I ) l f(x) 1 for x > k. Thus, f(x) is O (g(x» and g(x) is O (f(x» . Conversely, suppose that f(x) is O (g(x» and g(x) is o (f(x» . Then there are constants C J , C2 , kJ , and k2 such that I f(x) 1 :s CJ ig(x)1 for x > kl and Ig(x)1 :s C2 I f(x)1 for x > k2 . We can assume that C2 > 0 (we can al­ ways make C2 larger). Then we have ( I / C2 ) lg(x)1 :s I f(x)1 :s CJ ig(x)1 for x > max(kl ' k2 )' Hence, f(x) is 8(g(x» . 27. If f(x) is 8(g(x» , then f(x) is both O (g(x» and Q(g(x» . Hence, there are positive constants C I , kJ , C2 , and k2 such that I f(x)1 :s C2 Ig(x)1 for all x > k2 and I f(x)1 ::: CJ ig(x)1 for all x > kl . It follows that C J ig(x)1 :s I f(x)1 :s

k

x

3 1 . If f(x) is 8( 1 ), then I f(x)1 is bounded between pos­ itive constants C I and C2 . In other words, f(x) cannot grow larger than a fixed bound or smaller than the nega­ tive of this bound and must not get closer to 0 than some fixed bound. 33. Because f(x) is O (g(x» , there are con­ stants C and 1 such that I f(x) 1 :s C lg(x)1 for x > I. Hence, I fk (x) 1 :s C k lgk (x) 1 for x > I, so fk (x) is O (gk (x» by tak­ ing the constant to be C k . 35. Because f(x) and g(x) are increasing and unbounded, we can assume f(x) ::: 1 and g(x) ::: 1 for sufficiently large x . There are constants C and k with f(x) :s Cg(x) for x > k. This implies that log f(x) :s log C + log g(x) < 2 log g(x) for sufficiently large x . Hence, log f(x) is O (log g(x» . 37. By definition there are posi­ tive constraints C I , C; , C 2 , C� , kl ' k; , k2 , and k� such that fl (x) ::: C J ig(x)1 for all x > kl ' fi (x) :s C; Ig(x)1 for all x > k; , h(x) ::: C2 Ig(x)1 for all x > k2 ' and f2 (X) :S q lg(x) 1 for all x > k� . Adding the first and third inequalities shows that fl (x) + h(x) ::: (C I + C2 )lg(x) 1 for all x > k where k = max(kl , k2 )' Adding the second and fourth inequalities shows that fl (x) + h ex) :s (C; + CDlg(x)1 for all x > k' where k' = max(k; , k�). Hence, fl (x) + hex) is 8(g(x». This is no longer true if fl and h can assume negative val­ ues. 39. This is false. Let fl = x 2 + 2x , hex) = x 2 + x, and g(x) = x 2 . Then fl (x) and hex) are both O (g(x», but (fl - h)(x) is not. 4 1 . Take fen) to be the function with fen) = n if n is an odd positive integer and fen) = 1 if n is an even positive integer and g(n) to be the function with g(n) = 1 if n is an odd positive integer and g(n) = n if n is an even positive integer. 43. There are positive constants C I , C2 , C; , q , kl ' k; , k2 , and k� such that I fl (x)1 ::: CJ igl (x) 1 for all x > kl ' I fl (x) 1 :s C; lgI (x) 1 for all x ::: k; , Ih(x)1 > C 2 Ig2 (X)1 for all x > k2 , and I h (x)1 :s C� lg2 (X)1 for all x > k� . Because h and g2 are never zero, the last two in­ equalities can be rewritten as I I /h(x)1 :s ( 1 / C2 ) I I /g2 (x) 1 for all x > k2 and I I /h(x)1 ::: ( I /C�) I I /g2 (x) 1 for all x > k� . Multiplying the first and rewritten fourth inequali­ ties shows that I fI (x)/h(x)1 ::: (C I /C�)lg I (X)/g2 (X) 1 for all x > max(kl , kD, and multiplying the second and rewritten third inequalities gives I fI (x)/h (x)1 :s (C; /C2 )lg I (X)/g2 (X) 1 for all x > max(k; , k2 )' It follows that fl /h is big-Theta of gi lg2 . 45. There exist positive constants C I , C2 , kl , k2 , k; , k� such that I f(x , y)I :s C 1 Ig(x , y)1 for

S-20

Answers to Odd-Numbered Exercises

all x > kl and y > k2 and I f(x , y)1 � C2 Ig(x , y)1 for all x > k; and y > k� . 47. (x2 + xy + X Iog y)3 < (3x2y3) = 27x6y3 for x > 1 and y > 1 , because x2 < x2y, xy < x2y, and x log y < x2y. Hence, (x2 + xy + X Iog y)3 is O(x6y3). 49. For all positive real numbers x and y, LxyJ :::: xy. Hence, Lxy J is O(xy) from the definition, taking C = 1 and kl = k2 = O. 5 1 . a) Iimx .... oo X 2/X 3 = Iimx .... oo l /x = 0 x x I . b) 1·lmx-+oo lo x = 1·lmx .... oo x ln 2 = 0 (uSing x ¥ = 1·lmx .... oo � x 2 x L PH"Opl. tal's ru1e» c 1l· mx .... oo F = 1·lmx .... oo X2. ln = 1·lmx .... oo 2 2 x 2 +x+1 . L PH"Opl· ta l's ru1e ) d) 1·lmx .... oo 2- xx2 . (ln2 2)2 - 0 (usmg Iimx-.oo ( 1 + � + �) = 1 ;/= 0 53. Y _

x log x x

--:;z

55. No. Take f(x) = l /x2 and g(x) = l /x . 57. a) Be­ cause limx .... oo f(x)/g(x) = 0, If(x)I/ lg(x)1 < 1 for suf­ ficiently large x . Hence, I f(x)1 < Ig(x)1 for x > k for some constant k. Therefore, f(x) is O(g(x» . b) Let f(x) = g(x) = x. Then f(x) is o (g(x» , but f(x) is not o(g(x» because f(x)/g(x) = 1 . 59. Because fz(x) is o(g(x» , from Exercise 57(a) it follows that fz(x) is O(g(x» . By Corollary 1 , we have fl (x) + fz(x) is O (g(x» . 61. We can easily show that (n - i)(i + 1 ) � n for i = 0 , 1 , . . . , n - 1 . Hence, (n !)2 = (n . 1 )«n - 1 ) . 2) · «n - 2) . 3) · · · (2 · (n 1 » · ( 1 . n) � n n . Therefore, 2 Iog n ! � n log n . 63. Compute that log 5 ! � 6.9 and (5 log 5)/4 � 2.9, so the inequality holds for n = 5. Assume n � 6. Because n ! is the prod­ uct of all the integers from n down to 1 , we have n ! > n(n - I )(n - 2) · · · rn/21 (because at least the term 2 is missing). Note that there are more than n/2 terms in this product, and each term is at least as big as n /2. Therefore the product is greater than (n/2)(n f2). Taking the log of both sides of the inequality, we have log n ! > log ( � r /2 = � log � = � (log n - 1 ) > (n Iog n)/4, because n > 4 implies Iog n - 1 > (log n)/2. 65. All are not asymptotic. S ection 3.3 1. 2n - 1 3. Linear 5. 0(n) 7. a) power := 1 , y := 1 ; i := 1 , power := 2 , y := 3; i := 2 , power := 4 , y := 1 5 b) 2n multiplications and n additions 9. a) 2 109 � 1 03 x 10' b) 1 09 c) 3 .96 X 1 07 d) 3 . 1 6 X 1 04 e) 29 t) 1 2 1 1 . a) 36 years b) 1 3 days c) 1 9 minutes 13. The av­ erage number of comparisons is (3n+ 4)/2. 1 5. O(log n) 1 7. 0(n) 19. 0 (n2) 21. O(n) 23. 0 (n) 25. 0(Iog n)

S-20

comparisons; O(n2) swaps by 1

27. a) doubles

b)

increases

Section 3.4

1. a) Yes b) No c) Yes d) No 3. Suppose that a I b. Then there exists an integer k such that ka = b. Because a( ek) = be it follows that a I be. 5. Ifa I b and b I a, there are integers e and d such that b = ae and a = bd. Hence, a = aed. Because a ;/= 0 it follows that cd = 1 . Thus either e = d = 1 or e = d = - 1 . Hence, either a = b or a = -b. 7. Because ae I be there is an integer k such that aek = be. Hence, ak = b, so a l b. 9. a) 2, 5 b) - l 1 , 1 O c) 34 , 7 d) 77 , O e) O , O t) 0 , 3 g) - 1 , 2 b) 4 , 0 1 1 . If a mod m = b mod m, then a and b have the same remainder when divided by m. Hence, a = ql m + r and b = q2 m + r, where O :::: r < m . It follows that a - b = (ql - q2 )m , so m I (a - b). It foI­ Iows that a == b (mod m). 1 3. There is some b with (b - 1 ) k < n :::: bk. Hence, (b - I )k :::: n - 1 < bk. Divide by k to obtain b - 1 < n/ k :::: b and b - 1 :::: (n - 1)/ k < b. Hence, rn/ kl = b and L(n - 1)/ kJ = b - 1 . 1 5. x mod m if x mod m :::: rm/21 and (x mod m) - m if x mod m > rm/21 1 7. a) 1 b) 2 c) 3 d) 9 19. a) No b) No c) Yes d) No 2 1 . Let m = tn. Because a == b (mod m) there exists an integer s such that a = b + sm . Hence, a = b + (st)n , so a == b (mod n). 23. a) Let m = e = 2, a = 0, and b = 1 . Then 0 = ae == be = 2 (mod 2), but 0 = a ¥= b = 1 (mod 2). b) Let m = 5, a = b = 3, e = 1 , and d = 6. Then 3 == 3 (mod 5) and 1 == 6 (mod 5), but 3 1 = 3 ¥= 4 == 729 = 36 (mod 5). 25. Because a == b (mod m), there exists an integer s such that a = b + sm, so a - b = sm . Then a k - bk = (a - b)(a k - I + a k -2 b + . . . + abk -2 + bk - I ), k � 2, is also a multiple of m . It follows that a k == bk (mod m). 27. a) 7, 1 9, 7, 7, 1 8, 0 b) Take the next available space mod 3 1 . 29. 2, 6, 7, 1 0, 8, 2, 6, 7, 1 0, 8, . . . 3 1 . a) GR QRW SDVV JR b) QB ABG CNFF TB c) QX UXM AHJJ ZX 33. 4 35. The check digit of the ISBN for this book is valid because 1 ·0+2 ·0+3 ·7+4·2+5 · 8+6· 8+7 ·0+8 ·0+9· 8 + 1 0 . 2 == 0 (mod 1 1 ) . Section 3.5

1. 29, 7 1 , 97 prime; 2 1 , I l l , 1 43 not prime 3. a) 23 . 1 1 . 32 . 7 c) 3 6 d) 7 . 1 1 . 1 3 e) 1 1 . 1 0 1 t) 2 . 33 . 5 · 7 · 1 3 . 37 5. 28 . 34 . 52 . 7 7. Suppose that Iog2 3 = a/b where a , b E Z+ and b ;/= O. Then 2a /b = 3, so 2a = 3b • This violates the Fundamental Theorem of Arithmetic. Hence, 10g2 3 is irrational. 9. 3, 5, and 7 are primes of the de­ sired form. 1 1 . 1 , 7, 1 1 , 1 3 , 1 7, 19, 23, 29 13. a) Yes b) No c) Yes d) Yes 1 5. Suppose that n is not prime, so that n = ab, where a and b are integers greater than 1 . Because a > 1 , by the identity in the hint, 2a - 1 is a fac­ tor of 2n - 1 that is greater than 1 , and the second factor in this identity is also greater than 1 . Hence, 2n - 1 is not prime. 1 7. a) 2 b) 4 c) 1 2 1 9. ¢ (pk ) = pk _ pk - I e) 1 2 1 . a) 35 . 53 b) 1 c) 23 1 7 d) 4 1 · 43 · 53 b) 2

Answers to Odd-Numbered Exercises S-21

S-2 1

t) 1 1 1 1 23. a) 2 1 1 · 3 7 · 59 · 73 b) 29 . 3 7 . 55 . 73 . 1 1 . d) 4 1 · 43 · 53 e) 2 1 2 3 1 3 5 1 772 1 c) 23 3 1 1 3 . 17 t) Undefined 25. gcd (92928, 123552) = 1 056; Icm(92928, 123552) = 10, 872, 576; both products are 1 1 ,48 1 ,440, 256. 27. Because min(x , y) + max(x , y) = x + y, the exponent of Pi in the prime factorization of gcd(a , b) · lcm(a , b) is the sum of the exponents of Pi in the prime factorizations of a and b. 2 9. a) an = I if n is prime and an = 0 otherwise. b) an is the smallest prime factor of n with al = l . c) an is the number of positive divisors of n . d) an = I if n has no divisors that are perfect squares greater than I and an = 0 otherwise. e) an is the largest prime less than or equal to n . t) an is the product of the first n - 1 primes. 3 1 . Because every second integer is divisible by 2, the product is divisible by 2. Because every third integer is divisible by 3, the product is divisible by 3 . Therefore the product has both 2 and 3 in its prime factorization and is therefore divisible by 3 . 2 = 6. 33. n = 1 601 is a counterexample. 35. Suppose that there are only finitely many primes of the form 4k + 3, namely q l , q2 , . . . , qn , where q l = 3, q2 = 7, and so on. Let Q = 4q l q2 . . . qn - l . Note that Q is of the form 4k + 3 (where k = q l q2 . . . qn - 1). If Q is prime, then we have found a prime of the desired form different from all those listed. If Q is not prime, then Q has at least one prime factor not in the list q l , q2 , . . . , qn , because the remainder when Q is di­ vided by qj is qj - I , and qj - I =f. O. Because all odd primes are either of the form 4k + 1 or of the form 4k + 3, and the product of primes of the form 4k + I is also of this form (be­ cause (4k + 1 )(4m + 1 ) = 4(4km + k + m ) + 1 ), there must be-a factor of Q of the form 4k + 3 different from the primes we listed. 37. Given a positive integer x, we show that there is exactly one positive rational number m / n (in lowest terms) such that K (m / n) = x . From the prime factorization ofx, read off the m and n such that K (m / n) = x . The primes that occur to even powers are the primes that occur in the prime factor­ ization ofm , with the exponents being half the corresponding exponents in x; and the primes that occur to odd powers are the primes that occur in the prime factorization of n, with the exponents being half of one more than the exponents in x . Section 3.6 I. a) 1 1 1 0 01 1 1 b) I 000 1 1 0 1 1 0 1 00 c) I 0 1 1 1 1 1 0 1 01 10 1 1 00 3 . a) 3 1 b) 5 1 3 c) 34 1 d) 26, 896 5. a) 1000 0000 1 1 1 0 b) 1 00 1 1 0 1 0 1 1 0 1 0 1 0 1 1 c) 1 0 1 0 101 1 101 1 1 0 1 0 d) 1 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 00 1 1 1 0 1 1 0 1 7. 1010 101 1 1 1 00 1 1 0 1 1 1 1 0 1 1 1 1 9 . (B7B) 1 6 1 1 . Adding up to three leading Os if necessary, write the binary ex­ pansion as ( . . . b23 b22b2 I b20 b 1 3bl2bl 1 blOb03b02bOl boo}z. The value of this numeral is boo + 2bol + 4b02 + 8b03 +

24 blO + 25 bl 1 + 26 bl2 + 27 b 1 3 + 28b20 + 29b2 1 + 21 0 b22 + 2 1 1 b23 + . . . , which we can rewrite as boo + 2bol + 4b02 + 8b03 + (blO + 2bl l + 4b 1 2 + 8b 1 3) . 24 + (b20 + 2b2 1 + 4b22 + 8b23) · 28 + . . . . Now (bi3bi2bi l biO)2 translates into the hexadecimal digit hi. So our number is ho + h i . 24 + h2 . 28 + . . . = ho + h i . 1 6 + h2 . 1 62 + . . . , which is the hexadecimal expansion ( . . . h l h l ho) 16 . 13. Group together

blocks of three binary digits, adding up to two initial Os if necessary, and translate each block of three binary digits into a single octal digit. 1 5. ( 1 1 1 0 1 1 1 00 1 0 1 0 1 1 0 1 O00 1 )z , ( 1 273) 8 1 7. Convert the given octal numeral to binary using Exercise 14, then convert from binary to hexadeci­ mal using Example 6. 1 9. 436 2 1 . 27 2 3. a) 6 b) 3 c) 1 1 d) 3 e) 40 t) 1 2 25. 8 27. The binary ex­ pansion of the integer is the unique such sum. 29. Let a = (an-I an-2 . . . alaO) I O. Then a = lOn-I an_ 1 + l On-2 an_2 + . . . + 10al + ao = an-I + an-2 + . . . + al + ao (mod 3), because lW = l (mod 3) for all nonnegative integers j. It follows that 3 I a if and only if 3 divides the sum of the decimal digits ofa . 3 1 . Let a = (an-I an-2 . . . al ao}z. Then

a = ao + 2al + 22 a2 + . . . + 2n-I an_ 1 = ao - al + a2 a3 + . . . ± an-I (mod 3). It follows that a is divisible by

3 if and only if the sum of the binary digits in the even­ numbered positions minus the sum of the binary digits in the odd-numbered positions is divisible by 3 . 33. a) -6 b) 1 3 c) - 14 d) 0 35. The one's complement of the sum is found by adding the one's complements of the two integers except that a carry in the leading bit is used as a carry to the last bit of the sum. 37. If m � 0, then the leading bit an-I of the one's complement expansion of m is 0 and the formula reads m = L7::� ai2i . This is correct because the right-hand side is the binary expansion of m . When m is negative, the leading bit an-I of the one's com­ plement expansion of m is 1 . The remaining n - 1 bits can be obtained by subtracting -m from 1 1 1 . . . I (where there are n - l i s), because subtracting a bit from 1 is the same as complementing it. Hence, the bit string an-2 . . . ao is the binary expansion of (2 n - 1 - I ) - ( -m). Solving the equa­ tion (2n- 1 - 1 ) - ( -m) = L7::� ai 2 i for m gives the desired equation because an-I = l . 39. a) -7 b) 1 3 c) - 1 5 d) - 1 4 1 . To obtain the two's complement representation of the sum of two integers, add their two's complement repre­ sentations (as binary integers are added) and ignore any carry out of the leftmost column. However, the answer is invalid if an overflow has occurred. This happens when the leftmost dig­ its in the two's complement representation of the two terms agree and the leftmost digit of the answer differs. 43. If m � 0, then the leading bit an-I is 0 and the formula reads m = L7::� ai2i . This is correct because the right-hand side is the binary expansion of m . If m < 0, its two's complement expansion has 1 as its leading bit and the remaining n - 1 bits are the binary expansion of 2n - 1 - ( -m). This means that (2n- l ) - (-m) = L7::� ai 2i . Solving for m gives the desired equation because an-I == 1 . 45. 4n 47. procedure Cantor(x : positive integer) n := 1 ; f := 1 while (n + 1 ) . f ::::: x

begin n := n + 1 f := f · n end y := x while n > 0 begin

S-22

Answers to Odd-Numbered Exercises

:= LYlfJ y := y - an ' I 1 := li n n := n - l end (x = an n ! + an _ l (n - I ) ! + . . . + ad ! } 49. First step: e = O, d = O, so = 1 ; second step: e = O, d = 1 , S I = 0 ; third step: e = 1 , d = 1 , S2 = 0 ; fourth step: e = 1 , d = 1 , S3 = 0 ; fifth step: e = 1 , d = 1 , S4 = 1 ; sixth step: e = 1 , S5 = 1 5 1 . procedure subtraet(a , b: positive integers, a > b, a = (an- l an-2 . . . a l aO )2 , b = (bn - l bn - 2 . . . bl bo )z ) B := 0 { B is the borrow} for j := 0 to n - 1

an

begin

if aj ::: bj + B then

begin

Sj

:= aj - bj - B B := 0

end else begin

Sj

:= aj + 2 - bj - B B := 1

end end {(Sn - I Sn - 2 . . . S I SO )z is the difference} 53. procedure eompare(a, b: positive integers, a = (anan-I . . . al aoh , b = (bn bn-I . . . b l boh )

k := n

while ak = bk and k >

0 k := k - l if ak = bk then print "a equals b"

if ak > bk then print "a is greater than b" if ak < bk then print "a is less than b" 55. 0 (log n) 57. The only time-consuming part of the algo­ rithm is the while loop, which is iterated q times. The work done inside is a subtraction of integers no bigger than a, which has log a bits. The result now follows from Example 8. Section 3.7

b) 1 = 2 1 · 2 1 + (- 1 0) · 44 1 . a) 1 = (- 1 ) · 1 0 + 1 · 1 1 c) 1 2 = (- 1 ) · 36 + 48 d) 1 = 1 3 · 55 + (-2 1 ) . 34 e) 3 = 1 1 · 2 1 3 + (-20) · 1 1 7 t) 223 = 1 · 0 + 1 · 223 g) 1 = 37 . 2347 + (-706) . 1 23 h) 2 = 1 1 28 . 3454 + i) 1 = 2468 · 9999 + (-222 1 ) · 1 1 1 1 1 (-835) · 4666

5. 7 7. 52 3. 1 5 · 7 = 1 05 == 1 (mod 26) 9. Suppose that b and e are both inverses of a modulo m. Then ba == 1 (mod m) and ea == 1 (mod m) . Hence, ba == ea (mod m) . Because gcd(a , m) = 1 it follows by Theorem 2 that b == e (mod m). 1 1 . x == 8 (mod 9) 13. Let m' = ml gcd(e, m). Because all the common factors of m and e are divided out of m to obtain m', it follows that m' and e are relatively prime. Because m divides (ae - be) = (a - b)e, it follows that m' divides (a - b)e. By Lemma 1 , we see that m' divides a - b,

S-22

so a == b (mod m') . 1 5. Suppose that x 2 == 1 (mod p). 2 Then p divides x - 1 = (x + 1 )(x - 1 ). By Lemma 2 it follows that p I (x + 1) or p I (x - I ), so x == - 1 (mod p) or x == 1 (mod pl. 1 7. a) Suppose that ia == ja (mod p), where 1 :'S i < j < p. Then p divides ja - ia = aU - i). By Theorem 1, because a is not divisible by p, p divides j - i, which is impossible because j - i is a positive integer less than p. b) By part (a), because no two ofa , 2a , . . . , (p - l )a are congruent modulo p, each must be congruent to a differ­ ent number from 1 to p - 1 . It follows that a . 2a . 3a . . . . . (p - l ) · a == 1 · 2 · 3 · . . . . (p - 1 ) (mod pl. It follows that (p - I ) ! . aP- 1 == (p - I ) ! (mod pl. c) By Wilson's Theorem and part (b), if P does not divide a, it follows that (- I ) . aP- 1 == - 1 (mod p). Hence, ap- 1 == 1 (mod p). d) If p I a, then p i aP• Hence, aP == a == 0 (mod pl. If p does not divide a, then aP- 1 == a (mod p), by part (c). Mul­ tiplying both sides of this congruence by a gives aP == a (mod p). 19. All integers of the form 323 + 330k, where k is an integer 2 1 . All integers of the form 1 6 + 252k, where k is an integer 23. Suppose that p is a prime ap­ pearing in the prime factorization of m 1 m 2 ' . . m n • Because the m i S are relatively prime, p is a factor of exactly one of the m i S, say mj . Because mj divides a - b, it follows that a - b has the factor p in its prime factorization to a power at least as large as the power to which it appears in the prime factorization of m j . It follows that m 1 m 2 ' . . m n divides a - b, so a == b (mod m l m 2 ' " m n ). 25. x == 1 (mod 6) 27. a) By Fermat's Little Theorem, we have 2 1 0 == 1 (mod 1 1 ). Hence, 2 3 40 = (2 1 0) 34 == 1 3 4 = 1 (mod 1 1 ). b) Because 32 == 1 (mod 3 1 ), it follows that 2 340 = (25) 68 = 32 68 == 1 68 = 1 (mod 3 1 ). c) Because 1 1 and 3 1 are relatively prime, and 1 1 · 3 1 = 34 1 , it follows by parts (a) and (b) and Exercise 23 that 2 340 == 1 (mod 34 1 ). 2 9 . a) 3, 4, 8 b) 983 3 1 . First, 2047 = 23 . 89 is composite. Write 2047 - 1 = 2046 = 2 . 1 023, so S = 1 and t = 1 023 in the definition. Then 2 1 023 = (2 1 1 )93 = 204893 == 193 = 1 (mod 2047), as desired. 33. We must show that b2820 == 1 (mod 282 1 ) for all b relatively prime to 282 1 . Note that 282 1 = 7 · 1 3 · 3 1 , and if gcd(b , 282 1 ) = 1 , then gcd(b, 7) = gcd(b, 1 3 ) = gcd(b, 3 1 ) = 1 . Using Fermat's Little Theorem we find that b 6 == 1 (mod 7), b l 2 == 1 (mod 1 3), and b 3 0 == 1 (mod 3 1 ). It follows that b2820 == (b6 )4 70 == 1 (mod 7), b2820 == (b I2 )23 5 == 1 (mod 1 3), and b2820 == (b 3 0)94 == 1 (mod 3 1 ). By Exercise 23 (or the Chinese Re­ mainder Theorem) it follows that b2820 == 1 (mod 282 1 ), as desired. 35. a) If we multiply out this expression, we get n = 1 296m 3 + 396m 2 + 36m + 1 . Clearly 6m I n - 1 , 1 2m I n - 1 , and 1 8m I n - 1 . Therefore, the conditions of Exercise 34 are met, and we conclude that n is a Carmichael number. b) Letting m = 5 1 gives n = 1 72,947, 529. 37. 0 = (0, 0), 1 = ( 1 , 1), 2 = (2, 2), 3 = (0, 3), 4 = ( 1 , 4), 5 = (2, 0), 6 = (0, 1 ), 7 = ( 1 , 2), 8 = (2, 3), 9 = (0, 4), 10 = ( 1 , 0), 1 1 = (2, 1 ), 12 = (0, 2), 13 = ( 1 , 3), 14 = (2, 4) 39. We have m l = 99, m 2 = 98, m 3 = 97, and m 4 = 95, so m = 99 · 98 · 97 · 95 = 89,403 , 930. We find that M I = mim I = 903 , 070, M2 = mlm 2 = 9 1 2, 285, M3 = mlm 3 = 92 1 , 690, and M4 = mlm4 = 94 1 , 094. Using the Euclidean algorithm, we compute that Y I = 37, Y2 = 33,

Answers to Odd-Numbered Exercises

S-23

4 3, 4, 65 · 903,070 · 37 + 2 · 912,285 · 33 + 51 · 921,690 · 24 + 10 . 941,094 · 4 = 3,397,886,480 537, 140 (mod 89,403,930). 41. By Exercise 40 it follows that gcd(2b - 1 , (2a - 1 ) mod (2b - 1» = gcd(2b - 1, 2a mod b - 1). Because the expo­ nents involved in the calculation are b and a mod b, the same as the quantities involved in computing gcd(a , b), the steps used by the Euclidean algorithm to compute gcd(2a - I , 2b 1) in parallel to those used to compute gcd(a , b) and show that gcd(2a - 1 , 2 b - 1) = 2gcd(a, b) - 1. 43. Suppose that q is an odd prime with q I 2P - l . By Fermat's Little Theorem, q I 2q - 1 - l . From Exercise 41, gcd(2P I , 2q - 1 - 1 ) = 2gcd(p , q - l ) - l . Because q is a common di­ visor of 2P - 1 and 2q- 1 - 1 , gcd(2P - 1 , 2q - 1 - 1 ) l . Hence, gcd(p, q - 1) = p , because the only other possibility, namely, gcd(p, q - 1 ) = 1, gives us gcd(2P - 1 , 2q - 1 - 1) = 1. Hence, p I q - 1 , and therefore there is a positive inte­ ger m such that q - 1 = mp. Because q is odd, m must be even, say, m = 2k, and so every prime divisor of 2P - 1 is of the form 2kp + I . Furthermore, the product of num­ bers of this form is also of this form. Therefore, all divi­ sors of 2P - 1 are of this form. 45. Suppose we know both n = pq and (p - 1 )(q - 1). To find p and q , first note that (p - 1 )(q - 1) = pq - p - q + 1 = n - (p + q) + l . From this we can find s = (p + q). Because q = s - p, we have n = p(s - p). Hence, p 2 - ps + n = O. We now can use the quadratic formula to find p. Once we have found p, we can find q because q = n i p. 4 7 . SILVER 49. 34 · 1 44 + (-55) . 89 = 1 51. procedure extended Euclidean (a , b: positive

Y3 = 24, and Y4 = are inverses of Mk modulo m k for k = I , 2, respectively. It follows that the solution is =

run

>

integers)

x := a y := oldolds := olds := oldoldt := oldt := 1

b

1 0 while y =1= 0 0

begin

q := x div y r := x mod y x := y y := r s := oldolds - q . olds t := oldoldt - q . oldt oldolds := olds oldoldt := oldt olds := s oldt := t end (gcd( a , is x, and (oldolds)a Assume that s is a solution Then because (-S )2 = S 2 , -s is also more, s ¢ -s (mod p). Otherwise, p

53.

b)

+ (oldoldt)b = x} 2

of x = a (mod p). a solution. Further­ I 2s , which implies that p i s, and this implies, using the original assump­ tion, that p I a, which is a contradiction. Furthermore, if s and t are incongruent solutions modulo p, then because s 2 = t 2 (mod p), p I (s 2 - t 2 ) . This implies that p I (s + t)

S-23

(s - I), and by Lemma 2, p I (s - t) or p I (s t), so s = t (mod p) or s = -I (mod p). Hence, there are at most two

+

solutions. 55. The value of (�) depends only on whether a is a quadratic residue modulo p, that is, whether x 2 = a (mod p) has a solution. Because this depends only on the equivalence class of a modulo p, it follows that (�) = (�) if a = b (mod p). 57. By Exercise 56, (�)(�) = a (p- I )/2 b(p - I )/2 = (a b)(p - I )/2 = (�) (mod p). 59. x = 8, 13, 22, or 2 7 (mod 35) 61 . Suppose that we use a prime for n . To find a private decryption key from the corresponding pub­ lic encryption key e, one would need to find a number d that is an inverse for e modulo n - 1 so that the calculation shown before Example 12 can go through. But finding such a d is easy using the Euclidean algorithm, because the person doing this would already know n - 1 . In particular, to find d, one can work backward through the steps of the Euclidean algorithm to express 1 as a linear combination of e and n - 1; then d is the coefficient of e in this linear combination. Section 3.8

�[ � ] � [=�� � ]

1. a) 3

e)

x

4

d) l



1 4 3 3 6 7

c)

c) [ 2 0 4 6 ]

-t �3 !8 �: -�3

5. 9 1 5 -6/ 5 ] [ -1/5 41 5

7. 0 + A = [0 + aij] = [aij + 0] = O + A 9. A + (B + C) = [aij + (bij + Cij )] = [(aij + bij ) + Cij ] = (A + B) + C 11. The number of rows of A equals the number of columns of B, and the number of columns of A equals the number of rows of B. 13. A(BC) = [ Lq aiq (Lr bqr Cr/) ] = [Lq Lr aiq bqr Crl ] = [Lr Lq aiq bqr Crl ] = [ Lr { Lq ai q bqr ) crl ] = (AB)C 15. An = 17. a) Let U n A = [aij ] and B = [bij ] . Then A + B = [aij + bij ] . We have (A + B)I = [aji + bj ; ] = [aj ; ] + [bj ; ] = AI + BI. b) Using the same notation as in part (a), we have BI AI = [Lq bq iajq] = [ Lq ajq bq i] = (ABY, because the (i, j)th entry is the U, i)th entry of AB. 19. The result folb = a d bc lows because [ � � ] [ ! � ] [ � ad � bC ] = C b (ad - bc)I2 = [ ::. � ] [ � � J 21. An (A - I )n = c A(A . . . (A(AA- 1 )A- 1 ) · · · A- 1 )A-1 by the associative law. Because AA - I = I, working from the inside shows that An (A- I )n = I. Similarly (A-I)n An = I. Therefore (An )-I =

S-24

Answers to Odd-Numbered Exercises

(A - I )n . 23. There are m 2 multiplications used to find each of the m l m 3 entries of the product. Hence, m l m 2 m 3 multiplications are used. 25. A I «A2 A3 )�) 27. X I = I , X2 = - 1 , x3 = -2 29. 8)

31 . a)

[: 1 n [l ! n

33. a) A v B = [aij V bij] = [bij vaij] = B v A b) A /\ B = [aij /\ bij] = [bij /\ aij] = B /\ A 35. a) A v (B /\ C) =

[aij] v [bjj /\ Cij ] = [ajj v (bjj /\ Cjj )] = [(aij V bij) /\ (aij V Cij)] = [aij V bij] /\ [ajj V Cij] = (A v B) /\ (A v C) b) A /\ (B v C) = [aij] /\ [bij v cij] = [ajj /\ (bij v Cij)] = [(ajj /\ bij) v (aij /\ Cjj )] = [ajj /\ bij] v [ajj /\ Cij ] = (A /\ 37. A 0 (B 0 C) = [ Vq ajq /\ (Vr (bqr /\ B) v (A /\ C) Crt) )] = [ Vq Vr (aj q /\ bqr /\crt)] = [Vr Vq (aj q /\ bqr /\crt)] = [Vr (Vq (ajq /\ bqr) ) /\ Crt ] = (A 0 B) 0 C Supplementary Exercises

1. a) procedure last max(a I , . . . , an : integers)

max := al last := I i := 2 while i .::: n begin if aj :::: max then begin max := aj last := i end i := i + I end {last is the location of final occurrence of largest integer in list} b) 2n - I = O (n) comparisons 3. a) procedure pair zeros(bl b2 . . . bn : bit string, n :::: 2) X := bl y := b2 k := 2 while (k < n and (x =1= 0 or y =1= 0» begin

k := k + I

:= y y := bk end if (x = 0 and y = 0) then print "YES" x

else print "NO" b) O (n) comparisons

S-24

5.

a) and b) procedure smallest and largest (at , a2 , . . . , an: integers)

min := al max := al for i := 2 t o n begin if ai < min then min := ai if aj max then max := aj end {min is the smallest integer among the input, and max is the largest} c) 2n - 2 >

7. Before any comparisons are done, there is a possibility that each element could be the maximum and a possibility that it could be the minimum. This means that there are 2n differ­ ent possibilities, and 2 n - 2 of them have to be eliminated through comparisons of elements, because we need to find the unique maximum and the unique minimum. We classify comparisons of two elements as "virgin" or "nonvirgin," de­ pending on whether or not both elements being compared have been in any previous comparison. A virgin comparison elim­ inates the possibility that the larger one is the minimum and that the smaller one is the maximum; thus each virgin com­ parison eliminates two possibilities, but it clearly cannot do more. A nonvirgin comparison must be between two elements that are still in the running to be the maximum or two elements that are still in the running to be the minimum, and at least one of these elements must not be in the running for the other category. For example, we might be comparing x and y, where all we know is that x has been eliminated as the minimum. If we find that x > y in this case, then only one possibility has been ruled out-we now know that y is not the maximum. Thus in the worst case, a nonvirgin comparison eliminates only one possibility. (The cases of other nonvirgin compar­ isons are similar.) Now there are at most ln /2 J comparisons of elements that have not been compared before, each removing two possibilities; they remove 2ln /2 J possibilities altogether. Therefore we need 2n - 2 - 2ln/2J more comparisons that, as we have argued, can remove only one possibility each, in order to find the answers in the worst case, because 2n - 2 possibilities have to be eliminated. This gives us a total of 2n - 2 - 2 ln/2J + In/2J comparisons in all. But 2n - 2 2 ln/2J + In/2J = 2n - 2 - In/2J = 2n - 2 + f -n/21 = f2n - n/21 - 2 = f3n/21 - 2, as desired. 9. At end of first pass: 3, 1 , 4, 5, 2, 6; at end of second pass: 1 , 3, 2, 4, 5, 6; at end of third pass: 1 , 2, 3, 4, 5, 6; fourth pass finds nothing to exchange and algorithm terminates 1 1. There are pos­ sibly as many as n passes through the list, and each pass uses O (n) comparisons. Thus there are O (n 2 ) comparisons in all. 13. Because log n < n, we have (n log n + n 2 )3 .::: (n 2 + n 2 )3 .::: (2n 2 )3 = 8n 6 for all n > O. This proves that (n log n + n 2 )3 is O (n 6 ), with witnesses C = 8 and k = O. 1 5. 0 (x 2 2X ) 1 7. Note that � = � . n 2 1 . . . � . � . ! > � . I . 1 · · · 1 · ! = � . 19. 5, 22, - 12, -29 2 1 . Because ac == bc (mod m) there is an integer k such that ac = bc + km. Hence, a - b = km /c. Because a - b is an integer, C I km. Letting d = gcd(m , c), write C = de. Because no factor of e divides mid, it follows that d i m and e l k.

Answers to Odd-Numbered Exercises S-25

S-25

Thus a - b = (k/e)(m /d), where k/ e E Z and m id E Z. Therefore a == b (mod m id). 23. I 25. 27. (an an - I . . . a l aO ) 1 O = I:� =o I Ok ak == I:;=o ak (mod 9) because 1 0k == I (mod 9) for every nonnegative integer k. 29. If not, then suppose that qt, q2 , . . . , qn are all the primes of the form 6k + 5. Let Q = 6qlq2 · · · qn - 1 . Note that Q is of the form 6k + 5, where k = qlq2 · · · qn - I. Let Q = P I P2 · · · PI be the prime factorization of Q . No Pi is 2, 3, or any qj ' because the remainder when Q is divided by 2 is I , by 3 is 2 , and by qj is qj - I. All odd primes other than 3 are of the form 6k + I or 6k + 5, and the product of primes of the form 6k + I is also of this form. Therefore at least one of the Pi ' S must be of the form 6k + 5, a contradiction. 3 1 . a) Not mutually relatively prime b) Mutually relatively prime c) Mutually relatively prime d) Mutually relatively prime 33. a) The decryption function is g(q) = ii(q - b) mod 26, where ii is an inverse of a modulo 26. b) PLEASE SEND MONEY 35. x == 28 (mod 30) 37. Recall that a nonconstant polynomial can take on the same value only a finite number of times. Thus f can take on the values 0 and ± 1 only finitely many times, so if there is not some y such that f(y) is composite, then there must be some xo such that ±f(xo) is prime, say p. Look at f(xo + kp). When we plug Xo + kp in for x in the polynomial and multiply it out, every term will contain a factor of p except for the terms that fOrIn f(xo). Therefore f(xo + kp ) = f(xo) + mp = (m ± I )p for some integer m . As k varies, this value can be 0, p, or -p only finitely many times; therefore it must be a composite number for some values of k. 39. Assume that every even integer greater than 2 is the sum of two primes, and let n be an integer greater than 5. If n is odd, write n = 3 + (n - 3) and decom­ pose n - 3 = p + q into the sum of two primes; if n is even, then write n = 2 + (n - 2) and decompose n - 2 = p + q into the sum of two primes. For the converse, assume that ev­ ery integer greater than 5 is the sum of three primes, and let n be an even integer greater than 2. Write n + 2 as the sum of three primes, one of which is necessarily 2, so n + 2 = 2 + A4n +1 = p + q , whence n = p + q . 4 1 . A4n =

I

[� � ] , [ � �I J . for [ �I � J . [ �I � I J . 0 Suppose that [� � l Let [ � � l A4n+2 =

n



A4n+ 3 =

43.

A=

B=

Because AB = BA, it follows that c = 0 and a = d. Let B =

[ � �] . Because [� �]

A=

AB = BA,

it follows that b = O. Hence,

= al.

45. procedure triangular matrix multiplication(A, B:

upper triangular n B = [bi} ] ) for i := I to n

for j := i to n

begin

Cij :=

0

x

n

matrices, A = [ai} ] ,

for k := i to j Cij := Cij + ai k bkj

end

47. (AB)(B - 1 A - 1 ) = A(BB- 1 )A - 1 = AIA - 1 = AA - 1 =

Similarly, (B - 1 A - 1 )(AB) = I. Hence, (ABr l = B - 1 A - 1 . v (aip 1\ 49. a) Let A � PH I is true. The weaker

8-27

statement (PI /\ . . . /\ Pk- I /\ Pk ) � PH I follows from this. We will first prove the result when n is a power of 2, that is, ifn = 2k , k = 1 , 2 , . . . . Let P(k) be the statement A ::: G , where A and G are the arithmetic and geometric means, re­ spectively, of a set of n = 2k positive real numbers. Basis step: k = 1 and n = 2 1 = 2. Note that (..[iii - ,.jii2)2 ::: O. Expanding this shows that al - 2.ja l a2 + a2 ::: 0, that is, (al + a 2 )/2 ::: (al a2 ) 1 /2 . Inductive step: Assume that P(k) is true, with n = 2 k . We will show that P(k + 1 ) is true. We have 2H I = 2n. Now (a l + a2 + . . . + a2n )/(2n) = [(at + a2 + . . . + an )/n + (an+ 1 + a n+ 2 + . . . + a2n )/n]/2 and similarly (a l a2 ' " a2n ) I /(2n ) = [(a t . . . an ) l / n (an+ 1 . . . a2n ) l / n ] I /2 . To simplify the notation, let A(x , y , . . . ) and G (x , y, . . . ) denote the arithmetic mean and geometric mean of x , y , . . . , respectively. Also, if x ::: x', y ::: y', and so on, then A(x , y, . . . ) ::: A(x' , y', . . . ) and G (x , y , . . . ) ::: G (x' , y', . . . ). Hence, A(aJ , . . . , a2n ) = A(A(a l , . . . , an ), 59.

A(an+ I , . . . , a2n » ::: A(G(al , . . . , a n ), G (a n+ I , . . . , a2n » ::: G(G(al , . . . , an ), G (an+ J , . . . , a 2n » = G (a l , . . . , a 2n ). This finishes the proof for powers of 2. Now if n is not a power of 2, let m be the next higher power of 2, and let an+ l , . . . , am all equal A(a J , . . . , an ) = "ii . Then we have [(al a2 . . . an )"iim - n ] l / m ::: A(al , . . . , am ), because m is a power of 2. Because A(al , " " am ) = "ii, it follows that l (a l . . . an ) l / m "ii - n / m ::: "iin / m . Raising both sides to the (m/n)th power gives G (al , . . . , an ) ::: A(al , . . . , an ). 6 1 . Basis step: For n = 1 , the left-hand side is just t, which is 1 . For n = 2, there are three nonempty subsets { I }, {2}, and { l , 2}, so the left-hand side is t + ! + /2 = 2. Inductive step: Assume that the statement is true for k. The set of the first k + 1 positive integers has many nonempty subsets, but

they fall into three categories: a nonempty subset of the first k positive integers together with k + 1 , a nonempty subset of the first k positive integers, or just {k + I }. By the inductive hypothesis, the sum of the first category is k. For the second category, we can factor out 1 /(k + 1 ) from each term of the sum and what remains is just k by the inductive hypothesis, so this part of the sum is k/(k + 1 ) . Finally, the third cate­ gory simply yields 1 /(k + 1 ) . Hence, the entire summation is k + k/(k + 1 ) + 1 /(k + 1 ) = k + 1 . 63 . Basis step: If A I � A 2 , then A I satisfies the condition of being a subset of each set in the collection; otherwise A 2 � A t . so A 2 satisfies the condition. Inductive step: Assume the inductive hypothe­ sis, that the conditional statement is true for k sets, and suppose we are given k + 1 sets that satisfy the given conditions. By the inductive hypothesis, there must be a set Ai for some i ::: k such that Ai � Aj for 1 ::: j ::: k. If Ai � A H I , then we are done. Otherwise, we know that A k+ I � Ai , and this tells us that A k+ 1 satisfies the condition of being a subset of Aj for 1 ::: j ::: k + 1 . 65. G ( I ) = 0, G (2) = 1 , G (3) = 3, G (4) = 4 67. To show that 2n ...., 4 calls are sufficient to exchange all the gossip, select persons 1 , 2, 3 , and 4 to be the central committee. Every person outside the central commit­ tee calls one person on the central committee. At this point the central committee members as a group know all the scan­ dals. They then exchange information among themselves by making the calls 1 -2, 3-4, 1 -3 , and 2-4 in that order. At this

S-28

Answers to Odd-Numbered Exercises

point, every central committee member knows all the scan­ dals. Finally, again every person outside the central committee calls one person on the central committee, at which point ev­ eryone knows all the scandals. [The total number of calls is (n - 4) + 4 + (n - 4) = 2 n - 4.] That this cannot be done with fewer than 2n - 4 calls is much harder to prove; see the website h t t p : / / www . c s . c o rne l l . edu / voge l s / Ep i demi c s / go s s ip s - t e l ephones . pd f for details. 69. We prove this by mathematical induction. The basis step (n = 2) is true tautologically. For n = 3, suppose that the intervals are (a , b), (c, d), and (e, j), where without loss of generality we can assume that a :s c :s e. Because (a , b) n (e, f) =1= 13, we must have e < b; for a similar reason, e < d. It follows that the number halfway between e and the smaller of b and d is common to all three intervals. Now for the inductive step, assume that whenever we have k intervals that have pairwise nonempty intersections then there is a point common to all the intervals, and suppose that we are given intervals h , /z , . . . , h+1 that have pairwise nonempty intersections. For each i from I to k, let Ji = Ii n h+ l . We claim that the collection JI , ]Z , . . . , Jk satisfies the inductive hypothesis, that is, that Jit n Ji2 =1= 13 for each choice of sub­ scripts i l and i2 ' This follows from the n = 3 case proved above, using the sets Ii I ' Ii2 , and Ik+l . We can now invoke the inductive hypothesis to conclude that there is a number com­ mon to all of the sets Ji for i = I , 2, . . . , k, which perforce is in the intersection of all the sets Ii for i = 1 , 2, . . . , k + 1 . .71 . Pair up the people. Have the people stand at mutually distinct small distances from their partners but far away from everyone else. Then each person throws a pie at his or her partner, so everyone gets hit. 73.

75. Let P (n) be the statement that every 2 n x 2 n x 2 n checker­ board with a I x 1 x 1 cube .removed can be covered by tiles that are 2 x 2 x 2 cubes each with a 1 x 1 x 1 cube removed. The basis step, P ( 1 ), holds because one tile coincides with the solid to be tiled. Now assume that P (k) holds. Now consider a 2k+ I x 2k+ I x 2k+ I cube with a 1 x 1 x I cube removed. Split this object into eight pieces using planes pamllel to its faces and running through its center. The missing 1 x 1 x 1 piece occurs in one of these eight pieces. Now position one tile with its center at the center of the large object so that the missing 1 x 1 x 1 cube lies in the octant in which the large object is missing a 1 x 1 x 1 cube. This creates eight 2 k x 2k X 2 k cubes, each missing a 1 x I x I cube. By the inductive hypothesis we can fill each of these eight objects with tiles. Putting these tilings together produces the desired tiling.

S-28

77.

79. Let Q(n) be P (n + b - I). The statement that P (n) is true for n = b, b + 1 , b + 2, . . . is the same as the statement that Q (m ) is true for all positive integers m . We are given that P(b) is true [i.e., that Q ( l ) is true], and that P (k) -+ P (k + 1) for all k 2: b [i.e., that Q(m ) -+ Q(m + 1) for all positive inte­ gers m]. Therefore, by the principle of mathematical induc­ tion, Q (m ) is true for all positive integers m .

S ection 4.2

1. Basis step: We are told we can run one mile, so P ( l ) is true. Inductive step: Assume the inductive hypothesis, that we can run any number of miles from 1 to k. We must show that we can run k + 1 miles. If k = 1 , then we are already told that we can run two miles. If k > 1 , then the inductive hypothesis tells us that we can run k - I miles, so we can run (k - 1) + 2 = k + 1 miles. 3. a) P(8) is true, because we can form 8 cents of postage with one 3-cent stamp and one 5-cent stamp. P(9) is true, because we can form 9 cents of postage with three 3-cent stamps. P ( l O) is true, because we can form 10 cents of postage with two 5-cent stamps. b) The statement that using just 3-cent and 5-cent stamps we can form j cents postage for all j with 8 :s j :s k, where we assume that k 2: 10 c) Assuming the inductive hypothesis, we can form k + 1 cents postage using just 3-cent and 5-cent stamps d) Because k 2: 10, we know that P (k - 2) is true, that is, that we can form k - 2 cents of postage. Put one more 3-cent stamp on the envelope, and we have formed k + 1 cents of postage. e) We have completed both the basis step and the inductive step, so by the principle of strong induction, the statement is true for every integer n greater than or equal to 8. 5. a) 4, 8, 1 1 , 12, 15, 1 6, 19, 20, 22, 23, 24, 26, 27, 28, and all values greater than or equal to 30 b) Let P(n) be the statement that we can form n cents of postage using just 4-cent and I I -cent stamps. We want to prove that P (n) is true for all n 2: 30. For the basis step, 30 = 1 1 + 1 1 + 4 + 4. Assume that we can form k cents of postage (the inductive hy­ pothesis); we will show how to form k + 1 cents of postage. If the k cents included an I I-cent stamp, then replace it by three 4-cent stamps. Otherwise, k cents was formed from just 4-cent stamps. Because k 2: 30, there must be at least eight 4-cent stamps involved. Replace eight 4-cent stamps by three I I-cent stamps, and we have formed k + 1 cents in postage. c) P (n) is the same as in part (b). To prove that P (n) is true for all n 2: 30, we check for the basis step that

S-2 9

30 = 1 1 + 1 1 + 4 + 4, 31 = 1 1 + 4 + 4 + 4 + 4 + 4, 32 = 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4, and 33 = 1 1 + 1 1 + 1 1 . For the inductive step, assume the inductive hypothesis, that P (j) is true for all j with 30 ::: j ::: k, where k is an arbitrary integer greater than or equal to 33. We want to show that P (k + 1 ) is true. Because k - 3 � 30, we know that P (k - 3) is true, that is, that we can form k - 3 cents of postage. Put one more 4cent stamp on the envelope, and we have formed k + 1 cents of postage. In this proof, our inductive hypothesis was that P(j) was true for all values of j between 30 and k inclu­ sive, rather than just that P (30) was true. 7. We can form all amounts except $ 1 and $3 . Let P (n) be the statement that we can form n dollars using just 2-dollar and 5-dollar bills. We want to prove that P (n ) is true for all n � 5. (It is clear that $ 1 and $3 cannot be formed and that $2 and $4 can be formed.) For the basis step, note that 5 = 5 and 6 = 2 + 2 + 2. Assume the inductive hypothesis, that P (j ) is true for all j with 5 ::: j ::: k, where k is an arbitrary integer greater than or equal to 6. We want to show that P (k + 1 ) is true. Be­ cause k - 1 � 5, we know that P(k - 1) is true, that is, that we can form k - 1 dollars. Add another 2-dollar bill, and we have formed k + 1 dollars. 9. Let P (n ) be the statement that there is no positive integer b such that "fi = n ib. Basis step: P ( I ) is true because "fi > 1 � l ib for all positive integers b. Inductive step: Assume that P (j) is true for all j ::: k, where k is an arbitrary positive integer; we prove that P (k + 1 ) is true by contradiction. Assume that "fi = (k + 1 )lb for some 2 2 positive integer b. Then 2b2 = (k + 1 ) , so (k + 1 ) is even, and hence, k + 1 is even. So write k + 1 = 2t for some pos­ 2 2 itive integer t, whence 2b 2 = 4 t and b = 2t 2 . By the same reasoning as before, b is even, so b = 2s for some positive in­ teger s. Then "fi = (k + 1 )lb = (2t)/(2s) = t is . But t ::: k, so this contradicts the inductive hypothesis, and our proof of the inductive step is complete. 1 1 . Basis step: There are four base cases. If n = 1 = 4 . 0 + 1 , then clearly the second player wins. If there are two, three, or four matches (n = 4 . 0 + 2, n = 4 . 0 + 3 , or n = 4 . 1 ), then the first player can win by removing all but one match. Inductive step: Assume the strong inductive hypothesis, that in games with k or fewer matches, the first player can win if k == 0, 2, or 3 (mod 4) and the second player can win if k == 1 (mod 4). Suppose we have a game with k + 1 matches, with k � 4. If k + 1 == o (mod 4), then the first player can remove three matches, leaving k - 2 matches for the other player. Because k - 2 == 1 (mod 4), by the inductive hypothesis, this is a game that the second player at that point (who is the first player in our game) can win. Similarly, if k + 1 == 2 (mod 4), then the first player can remove one match; and if k + 1 == 3 (mod 4), then the first player can remove two matches. Finally, if k + 1 == 1 (mod 4), then the first player must leave k, k - 1 , or k - 2 matches for the other player. Because k == 0 (mod 4), k - 1 == 3 (mod 4), and k - 2 == 2 (mod 4), so by the inductive hypothesis, this is a game that the first player at that point (who is the second player in our game) can win. 1 3 . Let P(n) be the statement that exactly n - 1 moves are required to assemble a puzzle with n pieces. Now P ( I ) is trivially true. Assume that P(j) is true for all j ::: k, and

Answers to Odd-Numbered Exercises

S-29

consider a puzzle with k + 1 pieces. The final move must be the joining of two blocks, of size j and k + 1 - j for some integer j with 1 ::: j ::: k. By the inductive hypothe­ sis, it required j - 1 moves to construct the one block, and k + 1 - j - 1 = k - j moves to construct the other. There­ fore, 1 + (j - 1 ) + (k - j) = k moves are required in all, so 1 5. Let the Chomp board have n rows P(k + 1 ) is true. and n columns. We claim that the first player can win the game by making the first move to leave just the top row and leftmost column. Let P (n) be the statement that if a player has presented his opponent with a Chomp configuration con­ sisting of just n cookies in the top row and n cookies in the leftmost column, then he can win the game. We will prove Vn P (n ) by strong induction. We know that P { l ) is true, be­ cause the opponent is forced to take the poisoned cookie at his first turn Fix k � 1 and assume that P (j) is true for all j ::: k. We claim that P (k + 1) is true. It is the opponent's turn to move. If she picks the poisoned cookie, then the game is over and she loses. Otherwise, assume she picks the cookie in the top row in column j, or the cookie in the left column in row j, for some j with 2 ::: j ::: k + 1 . The first player now picks the cookie in the left column in row j, or the cookie in the top row in column j, respectively. This leaves the position covered by P (j - 1 ) for his opponent, so by the inductive hy­ pothesis, he can win. 1 7. Let P (n) be the statement that if a simple polygon with n sides is triangulated, then at least two of the triangles in the triangulation have two sides that border the exterior of the polygon. We will prove "In � 4 P (n). The statement is clearly true for n = 4, because there is only one diagonal, leaving two triangles with the desired property. Fix k � 4 and assume that P (j ) is true for all j with 4 ::: j ::: k. Consider a polygon with k + 1 sides, and some triangulation of it. Pick one of the diagonals in this triangulation. First sup­ pose that this diagonal divides the polygon into one triangle and one polygon with k sides. Then the triangle has two sides that border the exterior. Furthermore, the k-gon has, by the inductive hypothesis, two triangles that have two sides that border the exterior of that k-gon, and only one of these trian­ gles can fail to be a triangle that has two sides that border the exterior of the original polygon. The only other case is that this diagonal divides the polygon into two polygons with j sides and k + 3 - j sides for some j with 4 ::: j ::: k - 1 . By the inductive hypothesis, each of these two polygons has two triangles that have two sides that border their exterior, and in each case only one of these triangles can fail to be a trian­ gle that has two sides that border the exterior of the original polygon. 19. Let P (n) be the statement that the area of a simple polygon with n sides and vertices all at lattice points is given by I ( P ) + B ( P )/2 - 1 . We will prove P (n ) for all n � 3. We begin with an additivity lemma: If P is a simple polygon with all vertices at the lattice points, divided into poly­ gons PI and P2 by a diagonal, then I ( P ) + B ( P )/2 - 1 = [I (PI ) + B (PI )/2 - 1 ] + [I(P2 ) + B(P2)/2 - 1 ] . To prove this, suppose there are k lattice points on the diagonal, not counting its endpoints. Then I (P ) = I ( PI ) + I ( P2 ) + k and B ( P ) = B(PI ) + B ( P2 ) - 2k - 2; and the result follows by simple algebra. What this says in particular is that if Pick's .

S-30

Answers to Odd-Nwnbered Exercises

formula gives the correct area for PI and P2 , then it must give the correct formula for P , whose area is the sum of the areas for PI and P2 ; and similarly if Pick's formula gives the correct area for P and one of the Pi 'S, then it must give the correct formula for the other Pi . Next we prove the the­ orem for rectangles whose sides are parallel to the coordi­ nate axes. Such a rectangle necessarily has vertices at (a , b), (a , c), (d, b), and (d, c), where a, b, c, and d are integers with b < c and a < d. Its area is (c - b)(d - a). Also, B = 2(c b + d - a) and 1 = (c - b - 1 )(d - a - I ) = (c - b)(d ­ a) - (c - b) - (d - a) + 1 . Therefore, I + B /2 - 1 = (c ­ b)(d - a) - (c - b) - (d - a) + 1 + (c - b + d - a) - 1 = (c - b)(d - a), which is the desired area. Next consider a

right triangle whose legs are parallel to the coordinate axes. This triangle is half a rectangle of the type just considered, for which Pick's formula holds, so by the additivity lemma, it holds for the triangle as well. (The values of B and I are the same for each of the two triangles, so if Picks's formula gave an answer that was either too small or too large, then it would give a correspondingly wrong answer for the rect­ angle.) For the next step, consider an arbitrary triangle with vertices at the lattice points that is not of the type already con­ sidered. Embed it in as small a rectangle as possible. There are several possible ways this can happen, but in any case (and adding one more edge in one case), the rectangle will have been partitioned into the given triangle and two or three right triangles with sides parallel to the coordinate axes. Again by the additivity lemma, we are guaranteed that Pick's formula gives the correct area for the given triangle. This completes the proof of P (3), the basis step in our strong induction proof. For the inductive step, given an arbitrary polygon, use Lemma 1 in the text to split it into two polygons. Then by the addi­ tivity lemma above and the inductive hypothesis, we know that Pick's formula gives the correct area for this polygon. 2 1 . a) In the left figure Labp is smallest, but bp is not an in­ terior diagonal. b) In the right figure bd is not an interior diagonal. c) In the right figure bd is not an interior diago­ nal. 23. a) When we try to prove the inductive step and find a triangle in each subpolygon with at least two sides bordering the exterior, it may happen in each case that the triangle we are guaranteed in fact borders the diagonal (which is part of the boundary of that polygon). This leaves us with no trian­ gles guaranteed to touch the boundary ofthe original polygon. b) We proved the stronger statement "In ::: 4 T (n) in Exercise 1 7. 25. a) The inductive step here allows us to conclude that P (3), P (5), . . . are all true, but we can conclude nothing about P (2), P (4), . . . . b) P (n) is true for all positive integers n , using strong induction. c) The inductive step here enables us to conclude that P (2), P (4), P (8), P ( 1 6), . . . are all true, but we can conclude nothing about P (n) when n is not a power of 2. d) This is mathematical induction; we can conclude that P (n) is true for all positive integers n . 27. Suppose, for a proofby contradiction, that there is some positive integer n such that P (n) is not true. Let m be the smallest positive inte­ ger greater than n for which P (m ) is true; we know that such an m exists because P (m ) is true for infinitely many values of m . But we know that P (m ) -+ P (m - 1 ), so P (m - 1 ) is also

S-3 0

true. Thus, m - 1 cannot be greater than n , so m - 1 = n and P (n) is in fact true. This contradiction shows that P (n) is true for all n . 29. The error is in going from the base case n = 0 to the next case, n = 1 ; we cannot write 1 as the sum of two smaller natural numbers. 3 1 . Assume that the well-ordering property holds. Suppose that P ( l ) is true and that the condi­ tional statement [P( I ) /\ P (2) /\ . . . /\ P (n)] -+ P(n + 1 ) is true for every positive integer n . Let S be the set of positive integers n for which P (n) is false. We will show S = 0. As­ sume that S =1= 0. Then by the well-ordering property there is a least integer m in S. We know that m cannot be 1 because P ( l ) is true. Because n = m is the least integer such that P (n) is false, P ( I ), P (2), . . . , P (m - 1) are true, and m - 1 ::: 1 . Because [ P ( I ) /\ P (2) /\ . . . /\ P (m - 1 )] -+ P (m ) is true, it follows that P (m ) must also be true, which is a contradiction. Hence, S = 0. 33. In each case, give a proof by contradic­ tion based on a "smallest counterexample," that is, values of n and k such that P (n , k) is not true and n and k are small­ est in some sense. a) Choose a counterexample with n + k as small as possible. We cannot have n = 1 and k = 1 , because we are given that P ( I , 1 ) is true. Therefore, either n > 1 or k > 1 . In the former case, by our choice of counterexam­ ple, we know that P (n - 1 , k) is true. But the inductive step then forces P (n , k) to be true, a contradiction. b) Choose a counterexample with n as small as possible. We cannot have n = 1 , because we are given that P ( l , k) is true for all k. Therefore, n > 1 . By our choice of counterexample, we know that P (n - 1 , k) is true. But the inductive step then forces P (n , k) to be true, a contradiction. c) Choose a counterex­ ample with k as small as possible. We cannot have k = 1 , because we are given that P (n , 1 ) is true for all n . There­ fore, k > 1 . By our choice of counterexample, we know that P (n , k - 1 ) is true. But the inductive step then forces P (n , k) to be true, a contradiction. 35. Let P (n) be the statement that if X I , X2 , , Xn are n distinct real numbers, then n - 1 multiplications are used to find the product of these numbers no matter how parentheses are inserted in the product. We will prove that P (n) is true using strong induction. The ba­ sis case P ( I ) is true because 1 - 1 = 0 multiplications are required to find the product of x ) , a product with only one factor. Suppose that P (k) is true for 1 ::: k ::: n . The last mul­ tiplication used to find the product of the n + 1 distinct real numbers X I , X2 , , Xn , Xn + 1 is a multiplication of the prod­ uct of the first k of these numbers for some k and the prod­ uct of the last n + 1 - k of them. By the inductive hypoth­ esis, k - 1 multiplications are used to find the product of k of the numbers, no matter how parentheses were inserted in the product of these numbers, and n - k multiplications are used to find the product of the other n + 1 - k of them, no matter how parentheses were inserted in the product of these numbers. Because one more multiplication is required to find the product of all n + 1 numbers, the total number of mul­ tiplications used equals (k - 1 ) + (n - k) + 1 = n . Hence, P (n + 1 ) is true. 37. Assume that a = dq + r = dq ' + r' with 0 ::: r < d and 0 ::: r' < d. Then d(q - q ' ) = r' - r . It follows that d divides r' - r . Because -d < r' - r < d, we have r' - r = o. Hence, r' = r. It follows that q = q ' . • . •

• • •

S-3 J

39. This is a paradox caused by self-reference. The answer is clearly "no." There are a finite number of English words, so only a finite number of strings of 1 5 words or fewer; there­ fore, only a finite number of positive integers can be so de­ scribed, not all of them. 4 1 . Suppose that the well-ordering property were false. Let S be a nonempty set of nonnegative integers that has no least element. Let P (n) be the statement "i f/. S for i = 0, 1 , . . . , n ." P (O) is true because if 0 E S then S has a least element, namely, O . Now suppose that P (n ) is true. Thus, 0 f/. s , 1 f/. s, . . . , n f/. S. Clearly, n + 1 cannot be in S, for if it were, it would be its least element. Thus P (n + 1 ) is true. So by the principle of mathematical induction, n f/. S for all nonnegative integers n . Thus, S = 0, a contradiction. 43. This follows immediately from Exercise 4 1 (if we take the principle of mathematical induction as an axiom) and from this exercise together with the discussion following the for­ mal statement of strong induction in the text, which showed that strong induction implies the principle of mathematical induction (if we take strong induction as an axiom). Section 4.3

1. a) f( 1 ) = 3, f(2) = 5, f(3) = 7, f(4) = 9 b) f( l ) = 3 , f(2) = 9, f(3) = 2 7 , f(4) = 8 1 c) f( l ) = 2, f(2) = 4 , f(3) = 1 6 , f(4) = 65 , 536 d ) f( 1 ) = 3 , f(2) = 1 3 , f(3) = 1 83, f(4) = 3 3 , 673 3. a) f(2) = - 1 , f(3) = 5, f(4) = 2, f(5) = 17 b) f(2 ) = -4, f(3) = 32, f(4) = -4096, f(5) = 536, 870, 9 1 2 c) f(2) = 8, f(3) = 1 76, f(4) = 92, 672, f(5) = 25, 764, 1 74, 848 d) f(2) = - ! , f(3) = -4, f(4) = 4, f(5) = -32 5. a) Not valid b) f(n) = 1 - n . Basis step: f(O) = 1 = 1 - O. Inductive step: if f(k) = 1 - k, then f(k + 1) = f(k) - 1 = 1 - k - 1 = 1 (k + 1 ). c) f(n) = 4 - n if n > 0, and f(O) = 2 . Basis step: f(O) = 2 and f( l ) = 3 = 4 - 1 . Inductive step (with k � 1 ): f(k + 1 ) = f(k) - 1 = (4 - k) - 1 = 4 - (k + 1 ). d) f(n) = 2 L{n + I )/2j . Basis step: f(O) = 1 = 2 l{0+ 1 )/2j and f( l ) = 2 = 2 l{ 1+ I )/2j . Inductive step (with k � 1 ) : f(k + 1 ) = 2f(k - 1 ) = 2 . 2 lk/2j = 2 lk/2J + I = 2 l{{k+ 1 )+ 1 )/2j . e) f(n) = 3 n . Basis step: Trivial. Inductive step: For odd n , nf(n) = 3 f(n - 1 ) = 3 . 3 1 = 3 n ; and for even n > 1 , n n 7 . There are many f(n) = 9 f(n - 2 ) = 9 . 3 - 2 = 3 .

possible correct answers. We will supply relatively simple ones. a) an + 1 = an + 6 for n � 1 and a l = 6 b) an+1 = an + 2 for n � 1 and al = 3 c) an+ 1 = Wan for n � 1 and al = 1 0 d) an+ 1 = an for n � 1 and a l = 5 9. F (O) = 0, F(n) = F(n - 1 ) + n for n � 1 1 1 . Pm (O) = 0, Pm (n + 13. Let P (n) be "It + h + . . . + 1 ) = Pm (n) + m " Basis step: P ( I ) is true because It = = f nn fi I 2 ' 1 = fi . Inductive step: Assume that P (k) is true. Then fl + f3 + . . . + 12k- I + fik+ 1 = fik + fik+ 1 = fik+2 + 1 5. Basis step: fofl + fl fi = O · 1 + 1 . 1 = h{k+I ) ' 1 2 = ff . Inductive step: Assume that folt + fd2 + . . . + f2k - d2k = flk ' Then fo ft + fd2 + . . . + f2k- l fik + f2d2k+1 + fik+d2k+2 = flk + fid2k + I + fik + d2k+2 = hk (f2k + fik+ l ) + fik + d2k + 2 = hd2k + 2 + fik+d2kH = (f2k + fik+ l ) fik+2 = flkH ' 1 7. The number of divisions

Answers to Odd-Numbered Exercises

S-3 1

used by the Euclidean algorithm to find gcd(fn+ 1 ' fn ) is o for n = 0, 1 for n = 1 , and n - 1 for n � 2. To prove this result for n � 2 we use mathematical induction. For n = 2, one division shows that gcd(/3 , h ) = gcd(2, 1 ) = gcd( l , 0) = 1 . Now assume that k - 1 divisions are used to find gcd(fk+I . Jic). To find gcd(fk+2 . fk+ I ), first divide fk+2 by fk+1 to obtain fk+2 = 1 . fk+ 1 + fk . After one div­ ision we have gcd(fk+2 , fk+ l ) = gcd(fk+ 1 t fk ). By the inductive hypothesis it follows that exactly k - 1 more di­ visions are required. This shows that k divisions are re­ quired to find gcd(fk+2 . fk+ I ), finishing the inductive proof. 1 9. I A I = - 1 . Hence, I A n l = ( _ I )n . It follows that n fn+ dn - I - /; = ( _ I ) . 2 1 . a) Proof by induction. Basis step: For n = 1 , max( -a l ) = -al = - min(a l )' For n = 2, there are two cases. If a2 � a l t then -al � -a2 , so max( -al t -a2 ) = -a l = - rnin(al ' a2 ). If a2 < a l t then - a l < -a2 , so max( -al . -a 2 ) = -a 2 = - min(al . a2 ) . Inductive step: Assume true for k with k � 2. Then max( -al . -a2 • . . . • -ak . -ak+ l ) = max(max( -al • . . . • -ak ). -ak+ l ) = max(- min(al , . . . , ak ) . -ak+ 1 ) = - min(min (ai , . . . , ak ), ak+ l ) = - min(a l ' . . . , ak+ I ). b) Proofby mathematical induction. Basis step: For n = 1 , the result is the identity a l + bl = al + bl . For n = 2, first con­ sider the case in which al + bl � a2 + b2 . Then max(al + bl . a2 + b2 ) = a l + bl . Also note that a l :::: max(a l . a2 ) and bl :::: max(bl • b2 ), so a l + bl :::: max(a l . a2 ) + max(bl • b2 ). Therefore, max(a l + b l • a 2 + b2 ) = al + bl :::: max(a l . a 2 ) + max(bl ' b2 ). The case with a l + bl < a2 + b2 is simi­ lar. Inductive step: Assume that the result is true for k. Then max(al + bl . a2 + b2 • . . . • ak + bk . ak+ 1 + bk+ l ) = max(max(a l + bi t a2 + b2 • . . . • ak + bk ). ak+ 1 + bk+ l ) :::: max(max(al . a2 , . . . , ak ) + max(bl ' b2 • . . . , bk ), ak + I + bk+ l ) :::: max(max(al . a2 . · . · , ak ), ak+ l ) + max(max(bl , b2 , . . . • bk ), bk+ 1 ) = max(al . a2 , . . . • at , ak+ 1 ) + max(bl , b2 , . . . , bk . bk+ I ) ' c) Same as part (b), but replace every occurrence of "max" by "min" and invert each inequality. 23. 5 E S, and X + Y E S if X . y E S . 25. a) O E S, and if X E S, then x + 2 E S and x - 2 E S . b) 2 E S, and if X E S, then x + 3 E S. c) 1 E S , 2 E S . 3 E S , 4 E S, and if X E S, then X + 5 E S. 27. a) (0, 1 ), ( 1 , 1 ), (2, 1 ); (0, 2), ( 1 , 2), (2, 2), (3 , 2), (4, 2); (0 . 3), ( 1 . 3), (2 . 3), (3 . 3), (4 . 3), (5 . 3), (6 . 3); (0 . 4), ( 1 . 4), (2 . 4), (3 . 4), (4 . 4), (5 . 4), (6 . 4), (7 . 4), (8 . 4) b) Let P (n) be the statement that a :::: 2b whenever (a . b) E S is obtained by n applications of the recursive step. Basis step: P (O) is true, because the only element of S obtained with no applications of the recur­ sive' step is (0, 0), and indeed 0 :::: 2 . O. Inductive step: As­ sume that a :::: 2b whenever (a , b) E S is obtained by k or fewer applications of the recursive step, and consider an element obtained with k + 1 applications of the recursive step. Because the final application of the recursive step to an element (a , b) must be applied to an element obtained with fewer applications of the recursive step, we know that a :::: 2b. Add 0 :::: 2, 1 :::: 2, and 2 :::: 2, respectively, to obtain a :::: 2(b + 1), a + 1 :::: 2(b + 1 ), and a + 2 :::: 2(b + 1 ), as de­ sired. c) This holds for the basis step, because 0 :::: O. If this holds for (a . b), then it also holds for the elements obtained

S-32

Answers to Odd-Numbered Exercises

from (a , b) in the recursive step, because adding 0 � 2, 1 � 2, and 2 � 2, respectively, to a � 2b yields a � 2 (b + 1 ), a + I � 2(b + 1 ), and a + 2 � 2(b + I ). 29. a) Define S by ( 1 , 1 ) E S, and if (a , b) E S, then (a + 2, b) E S, (a , b + 2) E S, and (a + I , b + 1) E S. All elements put in S satisfY the condition, because ( 1 , 1 ) has an even sum of coor­ dinates, and if (a , b) has an even sum of coordinates, then so do (a + 2, b), (a , b + 2), and (a + 1 , b + I ) . Conversely, we show by induction on the sum of the coordinates that if a + b is even, then (a , b) E S. If the sum is 2, then (a , b) = ( 1 , 1 ), and the basis step put (a , b) into S. Otherwise the sum is at least 4, and at least one of (a - 2, b), (a , b - 2), and (a - I , b - 1 ) must have positive integer coordinates whose sum is an even number smaller than a + b, and therefore must be in S. Then one application of the recursive step shows that (a , b) E S. b) Define S by ( 1 , 1 ), ( 1 , 2), and (2, 1 ) are in S, and if (a , b) E S, then (a + 2, b) and (a , b + 2) are in S. To prove that our definition works, we note first that ( 1 , 1 ), ( 1 , 2), and (2, 1) all have an odd coordinate, and if (a , b) has an odd coordinate, then so do (a + 2, b) and (a , b + 2). Con­ versely, we show by induction on the sum of the coordinates that if (a , b) has at least one odd coordinate, then (a , b) E S. If (a , b) = ( 1 , I ) or (a , b) = ( 1 , 2) or (a , b) = (2, 1 ), then the basis step put (a , b) into S. Otherwise either a or b is at least 3, so at least one of (a - 2, b) and (a , b - 2) must have positive integer coordinates whose sum is smaller than a + b, and therefore must be in S. Then one application of the recursive step shows that (a , b) E S. c) ( 1 , 6) E S and (2, 3) E S, and if (a , b) E S, then (a + 2, b) E S and (a , b + 6) E S. To prove that our definition works, we note first that ( 1 , 6) and (2 , 3) satisfY the condition, and if (a , b) satisfies the condition, then so do (a + 2, b) and (a , b + 6). Conversely we show by induction on the sum of the coor­ dinates that if (a , b) satisfies the condition, then (a , b) E S. For sums 5 and 7, the only points are ( 1 , 6), which the basis step put into S, (2, 3) , which the basis step putinto S, and (4, 3) = (2 + 2, 3), which is in S by one application of the recursive definition. For a sum greater than 7, either a :::: 3, or a � 2 and b :::: 9, in which case either (a - 2, b) or (a , b - 6) must have positive integer coordinates whose sum is smaller than a + b and satisfY the condition for being in S. Then one application of the recursive step shows that (a , b) E S. 31. If x is a set or a variable representing a set, then x is a well-formed formula. If x and y are well-formed formu­ lae, then so are X, (x U y), (x n y), and (x - y). 33. a) If x E D = to, 1 , 2, 3 , 4, 5 , 6, 7, 8, 9}, then m (x) = x; if s = tx, where t E D * and x E D , then m (s) = min(m (s), x) . b) Let t = wx, where w E D * and X E D . If w = A, then m (st) = m (sx) = min(m (s), x) = min(m (s), m (x» by the recursive step and the basis step of the definition of m . Otherwise, m (st) = m «sw)x) = min(m (sw), x) by the def­ inition of m . Now m (sw) = min(m (s), m (w» by the in­ ductive hypothesis of the structural induction, so m (st) = min(min(m (s), m (w » , x) = min(m (s), min(m (w), x» by the meaning of min. But min(m (w ), x) = m (wx) = m (t) by the recursive step of the definition of m . Thus, m (st) = min(m (s), m (t» . 35. A R = A and (ux) R = x u R for x E :E , u E :E * . 37. w o = A and w n + 1 = ww n • 39. When the

S-32

string consists of n Os followed by n I s for some non­ i negative integer n 4 1 . Let P (i) be "/(w ) = i · /(w)." P(O) o is true because I(w ) = 0 = 0 · /(w). Assume P(i) is true. i i Then l(wH I ) = I(ww ) = I(w ) + I(w ) = I(w) + i · /(w) = (i + I ) · I(w). 43. Basis step: For the full binary tree con­ sisting of just a root the result is true because n(T) = I and h ( T ) = 0, and 1 :::: 2 . 0 + 1 . Inductive step: Assume that n ( TI ) :::: 2h(TI ) + I and n (T2 ) :::: 2h ( T2 ) + 1 . By the recursive definitions of n ( T ) and h ( T ) , we have n ( T ) = 1 + n(TI ) + n ( T2 ) and h ( T ) = I + max(h ( Td , h ( T2 » . Therefore n ( T ) =

1 + n ( TI ) + n ( T2 ) :::: 1 + 2h ( TI ) + 1 + 2h(T2 ) + I :::: 1 + 2 · max(h ( TI ) , h ( T2 » + 2 = I + 2 (max(h ( Td , h(T2 » + I ) = 1 + 2h ( T ) . 45. Basis step: ao . o = 0 = 0 + O. Inductive step: Assume that am ', n ' = m' + n' whenever (m', n') is less than (m , n) in the lexicographic ordering of N x N. If n = 0 then am , n = am- I , n + I = m - I + n + I = m + n . If n > 0, then am , n = am , n - I + 1 = m + n 1 + 1 = m + n. 47. a) Pm.m = Pm because a number exceeding m cannot be used in a partition of m . b) Because there is only one way to partition 1 , namely, 1 = I , it follows that Pl , n = 1 . Because there is only one way to partition m into I s, Pm, l = 1 . When n > m it follows that Pm. n = Pm,m because a number exceed­ ing m cannot be used. Pm ,m = 1 + Pm ,m- I because one extra partition, namely, m = m , arises when m is allowed in the par­ tition. Pm , n = Pm, n - I + Pm - n , n ifm > n because a partition of m into integers not exceeding n either does not use any n s and -

hence, is counted in Pm, n - I or else uses an n and a partition of m - n , andhence, is counted in Pm _ n , n ' c) Ps = 7, P6 = 1 1 49. Let P (n ) be "A(n , 2 ) = 4." Basis step: P ( I ) is true be­ cause A( 1 , 2 ) = A(O, A( I , 1 » = A(O, 2 ) = 2 · 2 = 4 . Induc­ tive step: Assume that P (n ) is true, that is, A(n , 2 ) = 4 . Then A(n + 1 , 2) = A(n , A(n + 1 , 1 » = A(n , 2 ) = 4. 5 1 . a) 1 6 b) 65,536 53. Use a double induction argument to prove the stronger statement: A(m , k) > A(m , I) when k > I . Ba­ sis step: When m = 0 the statement is true because k > 1 implies that A(O, k) = 2k > 21 = A(O, I). Inductive step: Assume that A(m , x) > A(m , y) for all nonnegative inte­ gers x and y with x > y. We will show that this implies that A(m + 1 , k) > A(m + 1 , I) if k > I. Basis steps: When 1 = O and k > 0, A(m + I , I) = o and either A(m + I , k) = 2 or A(m + I , k) = A(m , A(m + I , k - I » . If m = 0, this is 2A( I , k - 1 ) = 2 k . If m > 0, this is greater than 0 by the inductive hypothesis. In all cases, A(m + I , k) > 0, and in fact, A(m + 1 , k) :::: 2. If 1 = 1 and k > I , then A(m + I , I) = 2 and A(m + 1 , k) = A(m , A(m + I , k - 1 » , with A(m + I , k - 1 ) :::: 2 . Hence, by the inductive hypoth­ esis, A(m , A(m + 1 , k - I » :::: A(m , 2 ) > A(m , I ) = 2. In­ ductive step: Assume that A(m + l , r) > A(m + l , s) for all r > s , s = 0, 1 , . . . , I. Then if k + 1 > 1 + 1 it follows that A(m + 1 , k + I ) = A(m , A(m + 1 , k» > A(m , A(m + 1 , k» = A(m + 1 , 1 + 1 ) . 55. From Exercise 54 it fol­ lows that A(i, j) :::: A(i - I , j ) :::: :::: A(O, j) = 2j :::: j . 57. Let P (n ) be "F(n ) i s well-defined." Then P(O) i s true be­ cause F(O) is specified. Assume that P (k) is true for all k < n . Then F(n) i s well-defined at n because F(n) i s given in terms of F (O), F ( I ), . . . , F(n - 1 ). So P (n ) is true for all integers n . 59. a) The value o f F ( 1 ) i s ambiguous. b) F(2) i s not de­ fined because F(O) is not defined. c) F(3) is ambiguous . . .

5-3 3

Answers to Odd-Numbered Exercises

(�)

and F (4) is not defined because F makes no sense. d) The definition of F ( I ) is ambiguous because both the second and third clause seem to apply. e) F (2) cannot be computed because trying to compute F (2) gives F (2) = 1 + c) 3 d) 3 F(F( I » = 1 + F(2). 6 1 . a) 1 b) 2 65. f2* (n ) = e) 4 t) 4 g) 5 63. fo* (n) = fn /al flog log n 1 £or n � 2, fz* ( l ) = 0

Section 4.4

1. First, we use the recursive step to write 5! = 5 · 4 L We then use the recursive step repeatedly to write 4 ! = 4 . 3 ! , 3 ! = 3 · 2 ! , 2 ! = 2 · I ! , and I ! = 1 · O L Inserting the value of O! = 1 , and working back through the steps, we see that I ! = 1 . 1 = 1 , 2 ! = 2 · 1 ! = 2 · 1 = 2, 3 ! = 3 · 2 ! = 3 · 2 = 6, 4! = 4 · 3 ! = 4 · 6 = 24, and 5 ! = 5 · 4! = 5 · 24 = 1 20. 3. First, because n = 1 1 is odd, we use the else clause to see that mpower(3 , 1 1 , 5) = (mpower (3 , 5 , 5)2 mod 5 · 3 mod 5) mod 5 . We next use the else clause again to see that mpower (3 , 5 , 5) = (mpower (3 , 2, 5)2 mod 5 · 3 mod 5) mod 5 . Then we use the else if clause to see that mpower (3 , 2, 5) = mpower (3 , 1 , 5)2 mod 5 . Us­ ing the else clause again, we have mpower (3 , 1 , 5) = (mpower (3 , 0, 5)2 mod 5 · 3 mod 5) mod 5 . Finally, us ng the if clause , we see that mpower (3 , 0, 5) = 1 . Workmg backward it follows that mpower (3 , 1 , 5) = ( 1 2 mod 5 · 3 mod 5) mod 5 = 3 , mpower (3, 2, 5) = 3 2 mod 5 = 4, mpower (3 , 5, 5) = (42 mod 5 · 3 mod 5) mod 5 = 3, and fi­ nally mpower (3 , 1 1 , 5) = (3 2 mod 5 · 3 mod 5) mod 5 = 2. We conclude that 3 1 1 mod 5 = 2. 5. With this input, the algorithm uses the else clause to find that gcd(8, l 3 ) = gcd( l 3 mod 8, 8) = gcd(5 , 8). It uses this clause again to find that gcd(5 , 8) = gcd(8 mod 5 , 5) = gcd(3 , 5), then to get gcd(3 , 5) = gcd(5 mod 3, 3) = gcd(2, 3), then gcd(2, 3) = gcd(3 mod 2, 2) = gcd( l , 2), and once more to get gcd( I , 2) = gcd(2 mod 1 , 1 ) = gcd(O, 1 ). Finally, to find gcd(O, 1 ) it uses the first step with a = 0 to find that gcd(O, 1) = 1 . Consequently, the algorithm finds that gcd(8, l 3 ) = 1 . 7. procedure mult (n : positive integer, x: integer) if n = 1 then mult (n , x) := x else mult (n , x) := x + mult (n - 1 , x) 9. procedure sum of odds (n: positive integer) if n = 1 then sum ofodds (n) := I else sum of odds(n) :=sum of odds (n - 1 ) + 2 n - 1 1 1. procedure smallest (a I , . . . , an : integers) if n = 1 then smallest (a i , . . . , an ) = al



else smallest (a l , . . . , an ) : = min(smallest (a l , . . . , a n - I ), an ) 13. procedure modfactorial(n , m : positive integers) if n = 1 then modfactorial(n , m) := 1 else modfactorial(n , m) := (n · modfactorial(n - 1, m » mod m

15.

procedure gcd(a ,

b: nonnegative integers)

{a < b assumed to hold} if a = 0 then gcd(a , b) := b

S-33

else if a = b - a then gcd(a , b) := a else if a < b - a then gcd(a , b) := gcd(a , b - a) else gcd(a , b) : = gcd(b - a , a) 17. procedure multiply (x , y : nonnegative integers) if y = 0 then multiply (x , y) := 0 else if y is even then multiply (x , y) := 2 . multiply (x , y /2) else multiply (x , y) := 2 . multiply (x , (y- I )/2) + x 19. We use strong induction on a . Basis step: If a = 0, we know that gcd(O, b) = b for all b > 0, and that is precisely what the if clause does. Inductive step: Fix k > 0, assume

the inductive hypothesis-that the algorithm works correctly for all values of its first argument less than k-and consider what happens with input (k, b), where k < b. Because k > 0, the else clause is executed, and the answer is whatever the algorithm gives as output for inputs (b mod k, k). Because b mod k < k, the input pair is valid. By our inductive hy­ pothesis, this output is in fact gcd(b mod k, k), which equals gcd(k, b) by Lemma 1 in Section 3.6. 2 1 . If n = 1 , then nx = x, and the algorithm correctly returns x . Assume that the algorithm correctly computes kx . To compute (k + l )x it recursively computes the product of k + I - 1 = k and x , and then adds x . B y the inductive hypothesis, i t computes that product correctly, so the answer returned is kx + x = (k + I )x , which is correct. 23. procedure square (n : nonnegative integer) if n = 0 then square (n) := 0 else square (n) := square (n - 1 ) + 2(n - 1 ) + 1 Let p en) be the statement that this algorithm correctly com­ putes n 2 • Because 02 = 0, the algorithm works correctly (us­ ing the if clause) if the input is o. Assume that the algo­ rithm works correctly for input k. Then for input k + 1 , it gives as output (because of the else clause) its output when the input is k, plus 2(k + 1 - 1 ) + 1 . By the inductive h�­ pothesis, its output at k is k2 , so its output at k + .1 IS k2 + 2(k + 1 - 1 ) + 1 = k2 + 2k + 1 = (k + 1 )2 , as desired. 25. n multiplications versus 2 n 27. O (log n) versus n 29. procedure a(n: nonnegative integer) if n = o then a(n) := 1 else if n = 1 then a(n) := 2 else a(n) := a(n - 1 ) * a(n - 2)

3 1 . Iterative 33. procedure iterative(n : nonnegative integer) . if n = 0 then z := 1 else if n = 1 then z := 2

else

begin

x := 1 y := 2 z := 3 for i := I

begin

to n - 2

w := x + y + z x := y y := z z := w

end

S-34

Answers to Odd-Numbered Exercises

end

{z is the n th term of the sequence}

35. We first give a recursive procedure and then an iterative procedure. procedure r(n : nonnegative integer) if n < 3 then r(n) : = 2n + 1

else r(n) = r(n -

1 ) . (r(n - 2))2 . (r(n - 3))3

procedure i(n : nonnegative integer) if n = 0 then z := 1 else if n = 1 then z := 3 else begin x := 1 y := 3 z := 5 for i := 1

.

begin

to n - 2

w := z * y2 * x 3 x := y y := z z := w

{z is the nth term of the sequence}

The iterative version is more efficient.

procedure reverse (w : bit string)

n := length(w) if n ::::: 1 then reverse(w) : = w else reverse(w) : = substr (w , n , n)reverse (substr (w , 1 , n - 1 )) {substr (w , a , b) is the substring of w consisting of

the symbols in the ath through bth positions} 39. The procedure correctly gives the reversal of A as A (ba­ sis step), and because the reversal of a string consists of its last character followed by the reversal of its first n - 1 characters (see Exercise 35 in Section 4.3), the algorithm behaves correctly when n > 0 by the inductive hypothesis. 4 1 . The algorithm implements the idea of Example 13 in Sec­ tion 4. 1 . If n = 1 (basis step), place the one right triomino so that its armpit corresponds to the hole in the 2 x 2 board. If n > 1 , then divide the board into four boards, each of size l 2 n - 1 x 2 n - , notice which quarter the hole occurs in, position one right triomino at the center of the board with its armpit in the quarter where the missing square is (see Figure 8 in Sec­ tion 4. 1 ), and invoke the algorithm recursively four times­ once on each of the 2 n - 1 x 2 n - 1 boards, each of which has one square missing (either because it was missing to begin with, or because it is covered by the central triomino). 43.

/� ;� M\ h

45.

po

J j\ /\ /\ /\

procedure A(m , n : nonnegative integers) if m = 0 then A(m , n) : = 2n else if n = 0 then A(m , n) := 0 else if n = 1 then A(m , n) : = 2 else A(m , n) := A(m - 1 , A(m , n - 1 ))

g

/\

\)

/

\ )\

bd

\

end end 37.

S-34

hz

fg

abd

\

hpz

�� / / �/ a

ko

pz

abdfghkopz

47. Let the two lists be 1 , 2, . . . , m - 1 , m + n - 1 and m , m + 1 , . . . , m + n - 2, m + n , respectively. 49. I f n = 1 ,

then the algorithm does nothing, which i s correct because a list with one element is already sorted. Assume that the algo­ rithm works correctly for n = 1 through n = k. If n = k + 1 , then the list i s split into two lists, L I and L 2 . By the induc­ tive hypothesis, mergesort correctly sorts each of these sub­ lists; furthermore, merge correctly merges two sorted lists into one because with each comparison the smallest element in L I U L 2 not yet put into L is put there. 5 1 . 0 (n) 53. 6 55. 0 (n 2 ) S ection 4.5

1. Suppose that x = O . The program segment first assigns the value 1 to y and then assigns the value x + y = 0 + 1 = 1 to z. 3. Suppose that y = 3 . The program segment assigns the value 2 to x and then assigns the value x + y = 2 + 3 = 5 to z. Be­ cause y = 3 > 0 it then assigns the value z + 1 = 5 + 1 = 6 to z. 5. (p/\ condition l ){Sdq

(p/\ -.condition l /\ condition2){S2 }q

(p /\ -.condition 1 /\ -.condition2 . . . /\ -.condition(n - 1){Sn }q p {if condition 1 then S 1 ; else if condition2 then S2 ; . . . ; else Sn }q 7. We wi�l show $a� p ; ''power = X i - I and i ::::: n + 1" is a loop invariant. Note that p is true initially, because before the l lqop starts , i = 1 and power = 1 = x o = X - I . Next, we must show that if p is true and i ::::: n after an execution of the loop, then p remains true after one more execution. The loop incre­ ments i by 1 . Hence, because i ::::: n before this pass, i ::::: n + 1 after this pass. Also the loop assigns power · x to power. By

S-35

the inductive hypothesis we see that power is assigned the value X i-I . X = X i . Hence, p remains true. Furthermore, the loop terminates after n traversals of the loop with i = n + I because i is assigned the value 1 prior to entering the loop, is incremented by I on each pass, and the loop terminates when i > n . Consequently, at termination power = x n , as desired. 9. Suppose that p is "m and n are integers." Then if the con­ dition n < 0 is true, a = -n = I n l after S I is executed. If the condition n < 0 is false, then a = n = In I after S I is executed. Hence, p{Sdq is true where q is p /\ (a = I n D. Because S2 assigns the value 0 to both k and x, it is clear that q{S2 }r is true where r is q /\ (k = 0) /\ (x = 0). Suppose that r is true. Let P(k) be "x = m k and k :::: a ." We can show that P (k) is a loop invariant for the loop in S3 . P (O) is true because before the loop is entered x = 0 = m · 0 and 0 :::: a . Now assume P(k) is true and k < a. Then P (k + 1 ) is true because x is as­ signed the value x + m = mk + m = m (k + 1 ). The loop ter­ minates when k = a, and at that point x = m a . Hence, r{S3 }s is true where s is "a = I n l and x = m a ." Now assume that s is true. Then if n < 0 it follows that a = - n , so x = - m n . I n this case S4 assigns - x = m n t o product. I f n > 0 then x = ma = m n , so S4 assigns mn to product. Hence, S{S4 }t is true. I I . Suppose that the initial assertion p is true. Then because p{S}qo is true, qo is true after the segment S is exe­ cuted. Because qo -+ q l is true, it also follows that q l is true after S is executed. Hence, p{S}q l is true. 13. We will use the proposition p, "gcd(a , b) = gcd(x , y) and y :::: 0," as the loop invariant. Note that p is true before the loop is entered, because at that point x = a, y = b, and y is a positive inte­ ger, using the initial assertion. Now assume that p is true and y > 0; then the loop will be executed again. Inside the loop, x and y are replaced by y and x mod y, respectively. By Lemma I of Section 3 .6, gcd(x , y) = gcd(y, x mod y). Therefore, af­ ter execution of the loop, the value of gcd(x , y) is the same as it was before. Moreover, because y is the remainder, it is at least O . Hence, p remains true, so it is a loop invariant. Furthermore, if the loop terminates, then y = O. In this case, we have gcd(x , y) = x, the final assertion. Therefore, the pro­ gram, which gives x as its output, has correctly computed gcd(a , b). Finally, we can prove the loop must terminate, be­ cause each iteration causes the value of y to decrease by at least 1 . Therefore, the loop can be iterated at most b times. Supplementary Exercises

1. Let P (n ) be the statement that this equation holds. Basis step: P ( I ) says 2/3 = 1 - ( 1 /3 1 ), which is true. Inductive step: Assume that P (k) is true. Then 2/3 + 2/9 + 2/27 + . . . + 2/3 n + 2/3 n +1 = 1 - I /3 n + 2/3 n +1 (by the inductive +l hypothesis), and this equals 1 - I /3 n , as desired. 3. Let P (n) be "1 . 1 + 2 · 2 + . . . + n · 2n - 1 = (n - I )2 n + I ." Basis step: P ( I ) is true because 1 . 1 = 1 = ( 1 - 1 )2 1 + 1 . Inductive step: Assume that P (k) is true. Then 1 . 1 + 2 . 2 + . . . + k · 2k - 1 + (k + 1 ) · 2k = (k - I )2k + 1 + (k + I )2 k = 2k · 2k + 1 = [(k + 1 ) - I ]2k+ 1 + 1 . 5. Let P (n ) be " 1 / ( 1 · 4) + . . . + I / [(3n - 2)(3n + 1 )] = n/(3n + 1 ) ." Basis step: P ( I ) is true because 1 /( 1 · 4) = 1 /4. Inductive

Answers to Odd-Numbered Exercises

8-35

step: Assume P (k) is true. Then 1 /( 1 · 4) + . . . + I /[(3k - 2)(3k + 1 )] + I / [(3k + I )(3k + 4)] = k/(3k + 1 ) + I / [(3k + I )(3k + 4)] = [k(3k + 4) + I ] /[(3k + I )(3k + 4)] = [(3k + I )(k + I )] / [(3k + I )(3k + 4)] = (k + I )/(3k + 4). 7. Let P (n ) be "2n > n 3 ." Basis step: P ( I O) is true be­ cause 1 024 > 1 000 . Inductive step: Assume P (k) is true. Then (k + 1 )3 = k3 + 3 � + 3k + 1 :::: k3 + 9k2 :::: k3 + 3 k3 = 2k < 2 . 2 k = 2k+l . 9. Let P (n ) be "a - b is a factor of a n - b n ." Basis step: P ( I ) is trivially true. Assume P (k) is true. Then a k+1 - bk+1 = a k+1 - ab k + abk - bk+ I = a(a k - bk ) + bk (a - b). Then because a - b is a fac­ tor of a k - bk and a - b is a factor of a - b, it follows that a - b is a factor of a k+1 - bk+l . 1 1 . Let P (n ) be "a + (a + d) + . . . + (a + nd) = (n + I )(2a + nd)/2." Ba­ sis step: P ( I ) is true because a + (a + d) = 2a + d = 2(2a + d)/2. Inductive step: Assume that P (k) is true. Then a + (a + d) + . . . + (a + kd) + [a + (k + I )d] = (k + I )(2a + kd)/2 + a + (k + I )d = ! (2ak + 2a + k2 d + kd + 2a + 2kd + 2d) = ! (2ak + 4a + k2 d + 3kd + 2d) = 13. Basis step: This is true for ! (k + 2)[2a + (k + 1 )d] . n = 1 because 5/6 = 1 0/ 1 2. Inductive step: Assume that the equation holds for n = k, and consider n = k + 1 . 5 i+4 = "k 1 i+4 + Then "k+1 2) L...d = i(i+I)(i+2) (k+ I)(kk++2)(k+3) = L...d = 1 i(i+I)(i+ . . k(3k+7) + k+25 2) (k+I)(k+ 2(k+I)(k+ )(k+3) (bY the Inductlve hypothe SlS· ) = (3 +7) (k+ li(k+2) . e � + !t�) = 2(k+ I)(k�2)(k+3 ) . [k(3k + 7) (k + 3) + 2(k + 5)] = 2(k+l)(k�2)(k+ 3 ) ' (3k3 + 1 6� + 23k + 1 0) = 2 2(k+I)(k�2)(k+3) . (3k + 1 O)(k + 1 ) = 2(k+2�(k+3) . (3k + 1)(3(k+ 1)+7) ' I O)(k + 1 ) = 2«(k+ k+ 1)+I)«k+ 1)+2) , as desr' red. 15 . BaSls step..

The statement is true for n = 1 because the derivative of g(x) = xtr is x . tr + tr = (x + I )tr by the product rule. Inductive step: Assume that the statement is true for n = k, i.e., the kth derivative is given by g(k) = (x + k)tr . Dif­ ferentiating by the product rule gives the (k + I )st deriva­ tive: g(k+I) = (x + k)tr + tr = [x + (k + I )]tr , as desired.

17. We will use strong induction to show that In is even if n == 0 (mod 3) and is odd otherwise. Basis step: This follows because 10 = 0 is even and II = 1 is odd. Inductive step: Assume that if j :::: k, then .N is even if j == 0 (mod 3) and is odd otherwise. Now suppose k + 1 == 0 (mod 3) . Then 1k+1 = Ik + Ik-I is even because Ik and Ik-I are both odd. If k + 1 == 1 (mod 3), then Ik+1 = fi + fi -I is odd because Ik is even and fi-I is odd. Finally, if k + 1 == 2 (mod 3), then 1k+1 = fi + Ik- I is odd because Ik is odd and Ik- I is even. 19. Let P (n ) be the statement that lk ln + lk+ dn+1 = fn +k+1 for every nonnegative integer k. Basis step: This consists of showing that P (O) and P ( I ) both hold. P (O) is true be­ cause filo + 1k+1 II = fi+1 . 0 + 1k+1 . 1 = Ji . Because fi ji + Ik+l h = Ik + 1k+1 = fi+2 , it follows that P ( I ) is true. Inductive step: Now assume that P (j) holds. Then, by the inductive hypothesis and the recursive definition of the Fibonacci numbers, it follows that 1k+1 .N+1 + Ik + 2 .N + 2 =

Ik (.N-1 + .N ) + Ik+l (.N + .N+d

=

(fk/.j-! + Ik + I .N ) +

(fk/j + Ik+l .N+I ) = .N-I+k+1 + .N+k+1 = .N+k+2' This shows that P (j + 1 ) is true. 2 1 . Let P (n ) be the statement I� + If + . . . + I; = In ln+1 + 2. Basis step: P (O) and

S-36

Answers to Odd-Numbered Exercises

P(I) both hold because 15 = 22 = 2 . 1 + 2 = loft + 2 and 15 + If = 22 + 1 2 = 1 · 3 + 2 = 1 1 /3 + 2. Inductive step: Assume that P(k) holds. Then by the inductive hy­ pothesis 15 + If + . . . + Ii + li+ 1 = hh + 1 + 2 + Ii + I = h+ l (lk + IH I ) + 2 = h+ l h+2 + 2. This shows that P(k + 1) holds. 23. Let P(n) be the statement that the identity holds for the integer n . Basis step: P(1) is obviously true. Inductive step: Assume that P(k) is true. Then cos(k + l)x + i sin(k + l )x = cos(kx + x) + i sin(kx + x) = cos kx cos x - sin kx sin x + i(sin kx cos x + cos kx sinx) = cos x (cos kx + i sin kx)(cos x + i sin x) = (cos x + i sinx)k (cos x + i sin x) = (cos x + i sin xl+ l . It follows that P(k + 1 ) i s true. 25. Rewrite the right-hand side as 2 n + l (n 2 _ 2n + 3) - 6. For n = I we have 2 = 4 . 2 - 6. Assume that the equation holds for n = k, and consider n = k + 1 . Then :E��: j 2 2j = :E � = I j 2 2j + (k + 1 )2 2k + I = 2k + l (k2 _ 2k + 3) - 6 + (� + 2k + 1 )2k+ 1 (by the inductive hypo­ thesis) = 2H I (2� + 4) - 6 = 2H2 (� + 2) - 6 = 2H2 [(k + 1 )2 - 2(k + 1) + 3] - 6. 27. Let P(n) be the statement that this equation holds. Basis step: In P(2) both sides reduce to 1 /3. Inductive step: Assume that P(k) is true. Then :E��: I /U 2 - 1 ) = ( :E� = I I /U 2 - 1 ») + 1/[(k + 1)2 - 1 ] = (k - 1 )(3k + 2)/[4k(k + I )] + l/[(k + 1)2 - 1 ] by the inductive hypothesis. This simplifies to (k - I )(3k + 2)/[4k(k + I )] + 1 /(� + 2k) = (3k3 + 5�)/[4k(k + 1)(k + 2)] = { [(k + I) - 1 ][3(k + 1 ) + 2]}/[4(k + 1 )(k + 2)], which is exactly what P(k + 1) asserts. 29. Let P(n) be the as­ sertion that at least n + 1 lines are needed to cover the lattice points in the given triangular region. Basis step: P(o) is true, because we need at least one line to cover the one point at (0, 0). Inductive step: Assume the induc­ tive hypothesis, that at least k + 1 lines are needed to cover the lattice points with x � 0, y � 0, and x + y � k. Con­ sider the triangle of lattice points defined by x � 0, y � 0, and x + y � k + 1 . By way of contradiction, assume that k + 1 lines could cover this set. Then these lines must cover the k + 2 points on the line x + y = k + 1 . But only the line x + y = k + 1 itself can cover more than one of these points, because two distinct lines intersect in at most one point. Therefore none of the k + 1 lines that are needed (by the inductive hypothesis) to cover the set of lattice points within the triangle but not on this line can cover more than one of the points on this line, and this leaves at least one point uncovered. Therefore our assumption that k + I lines could cover the larger set is wrong, and our proof is complete. 3 1 . Let P(n) be Bk = MAk M- I . Basis step: Part of the given conditions. Inductive step: Assume the inductive hypothesis. Then Bk+ 1 = BBk = MAM- I Bk = MAM- l MAk M- 1 (by the inductive hypothesis) = MAIAk M- I = MAAk M- I = MAk + l M- I . 33. We prove by mathematical induction the following stronger statement: For every n � 3, we can write n ! as the sum of n of its distinct positive divisors, one of which is 1 . That is, we can write n ! = a l + a2 + . . . + an , where each ai is a divisor of n !, the divisors are listed in strictly decreasing order, and an = 1 . Basis step: 3 ! = 3 + 2 + 1 . Inductive step: Assume that we can write k! as a sum of the desired form,

S-36

say k! = a l + a2 + . . . + ak , where each ai is a divisor of n ! , the divisors are listed in strictly decreasing order, and an = 1 . Consider (k + I ) ! . Then we have (k + I ) ! = (k + l)k! = (k + 1 )(a l + a2 + . . . + ak ) = (k + l)a l + (k + l)a2 + . . . + (k + l)ak = (k + l)a l + (k + l)a2 + . . . + k · ak + ak . Be­ cause each ai was a divisor of k!, each (k + l)ai is a divisor of (k + I)!. Furthermore, k · ak = k, which is a divisor of (k + I)!, and ak = 1 , so the new last summand is again 1 . (Notice also that our list of summands is still in strictly de­ creasing order.) Thus we have written (k + I)! in the desired form. 35. When n = 1 the statement is vacuously true. As­ sume that the statement is true for n = k, and consider k + I people standing in a line, with a woman first and a man last. If the kth person is a woman, then we have that woman standing in front of the man at the end. If the kth person is a man, then the first k people in line satisfy the conditions of the inductive hypothesis for the first k people in line, so again we can conclude that there is a woman directly in front of a man somewhere in the line. 37. Basis step: When n = I there is one circle, and we can color the inside blue and the outside red to satisfy the conditions. Inductive step: Assume the inductive hypothesis that if there are k circles, then the regions can be 2-colored such that no regions with a common boundary have the same color, and consider a situation with k + 1 circles. Remove one of the circles, producing a picture with k circles, and invoke the inductive hypothesis to color it in the prescribed manner. Then replace the removed circle and change the color of every region inside this circle. The resulting figure satisfies the condition, because if two regions have a common boundary, then either that boundary involved the new circle, in which case the regions on either side used to be the same region and now the inside portion is different from the outside, or else the boundary did not involve the new circle, in which case the regions are colored differently because they were colored differently before the new circle was restored. 39. If n = 1 then the equation reads 1 . 1 = 1 . 2/2, which is true. Assume that the equation is true for n and consider it for n + 1 . Then :EJ !: (2j - 1) ( :E � !� n = :EJ =I (2j - 1) ( :E�!� n + [2(n + 1 ) - 1 ] · n l l = :EJ =I (2j - 1) ( nl l + :E �=j i) + 2::/ = ( n l l :EJ =I (2j - 1 ») + ( :EJ =I (2j - 1) ", n . ! n(n+ l ) ( _1_ . 2n+ l 2n+ 1 �k=J k ) + n+ l = n+ 1 n 2 ) + 2 + n+ 1 (by the indI )2+(4n+2) = 2(n+ I )'+n(n+ l )' = uctive hypothesis) = 2n2+n(n+ 2(n+ l ) 2(n+ l) (n+ l n+2) 4 1 . a) 92 b) 91 c) 91 d) 91 e) 91 � 1) 9 1 43. The basis step is incorrect because n =F 1 for the sum shown. 45. Let P(n) be ''the plane is divided into n 2 - n + 2 regions by n circles if every two of these cir­ cles have two common points but no three have a com­ mon point." Basis step: P(1) is true because a circle divides the plane into 2 = 1 2 - 1 + 2 regions. Inductive step: Assume that P(k) is true, that is, k circles with the specified properties divide the plane into k2 - k + 2 regions. Suppose that a (k + l )st circle is added. This circle intersects each of the other k circles in two points, so these points of intersection form 2k new arcs, each of which splits an old

S-3 7

Answers to Odd-Numbered Exercises

region. Hence, there are 2k regions split, which shows that there are 2k more regions than there were previously. Hence, k + I circles satisfying the specified properties divide the plane into k2 - k + 2 + 2k = (k2 + 2k + 1) - (k + 1) + 2 = (k + 1 )2 - (k + 1) + 2 regions. 47. Suppose "fi were ra­ tional. Then "fi = alb, where a and b are positive inte­ gers. It follows that the set S = {n "fi l n E N} n N is a nonempty set of positive integers, because b "fi = a belongs to S. Let t be the least element of S, which exists by the well­ ordering pr�erty. Then t = s "fi for some integer s . We have t - s = s -J2 - s = s( "fi - 1), so t - s is a positive integer because "fi > 1 . Hence, t - s belongs to S. This is a con­ tradiction because t - s = s "fi - s < s . Hence, "fi is irra­ tional. 49. a) Let d = gcd(al ' a2 , . . . , an ). Then d is a divi­ sor of each aj and so must be a divisor ofgcd(an _ 1 , an ). Hence, d is a common divisor of ai , a2 , . . . , an- 2 , and gcd(a�_ I ' an ). To show that it is the greatest common divisor of these num­ bers, suppose that c is a common divisor of them. Then c is a divisor of aj for i = 1 , 2, . . . , n - 2 and a divisor of gcd(an _ h an ), so it is a divisor of an - I and an . Hence, c is a common divisor of ai , a2 , . . . , an- I . and an . Hence, it is a divisor of d, the greatest common divisor of a I , a2 , . . . , an . It follows that d is the greatest common divisor, as claimed. b) If n = 2, apply the Euclidean algorithm. Otherwise, ap­ ply the Euclidean algorithm to an - I and an , obtaining d = gcd(an _ I ' an ), and then apply the algorithm recursively to a" a2 , . . . , an - 2 , d. 51. f(n) = n 2 . Let P (n ) be "f(n) = n 2 ." Basis step: P ( I ) is true because f( l ) = 1 = 1 2 , which follows from the definition of f. Inductive step: Assume f(n) = n 2 . Then f(n + 1) = f«n + 1) - 1) + 2(n + 1 ) - 1 = f(n) + 2n + 1 = n 2 + 2 n + 1 = (n + 1 )2 . 53. a) A, 0, 1 , 00, 0 1 ,

1 1 , 000, 00 1 , 01 1 , 1 1 1 , 0000, 000 1 , 001 1 , 0 1 1 1 , 1 1 1 1 , 00000, 0000 1 , 000 1 1 , 00 1 1 1 , 01 1 1 1 , 1 1 1 1 1 b) S = {a,8 I a is a string of m Os and ,8 is a string of n I s, m :::: 0, n :::: O } 55. Apply the first recursive step to A to get 0 E B . Apply the second recursive step to this string to get 00 E B . Ap­

ply the first recursive step to this string to get (O()) E B . By Exercise 58, «())) i s not in B because the number of left parentheses does not equal the number of right parentheses.

57. A, 0, «()), 00 59. a) 0 b) -2 c) 2 d) 0

8-37

63. Ifx ::::: y initially, then x :.= y is not executed, so x ::::: y is a true final assertion. If x > y initially, then x : = y is executed, so x ::::: y is again a true final assertion. 65. procedure zerocount (al , a2 , . . . , an : list of integers) if n = 1 then if a l = 0 then zerocount (a l , a2 , . . . , an ) := I

else zerocount (al , a2 , . . . , an ) := 0 else if an = 0 then zerocount (a l , a2 , . . . , an ) := zerocount (a l , a2 , . . . , an - I ) + 1 else zerocount (a l , a2 , . . . , an ) := zerocount (al , a2 , . , an - I ) 67. We will prove that a(n) is a natural number and a(n) ::::: n . This i s true for the base case n = 0 because a(O) = O . Now assume that a(n - 1 ) is a natural number and a(n - I ) ::::: n - 1 . Then a(a(n - 1 » is a applied to a natural number less than or equal to n - 1 . Hence, a(a(n - 1 » is also a natural number minus than or equal to n - I . Therefore, n - a(a(n - 1 » is n minus some natural number less than or equal to n - 1 , which is a natural number less than or equal to n . 69. From Exercise 68, a(n) = L(n + l )tLJ and a (n - I) = LntLJ . Because tL < 1 , these two values are equal or they differ by 1 . First suppose that tLn - LtLn J < I - tL. This is equivalent to tL(n + 1 ) < 1 + LtLn J . If this is true, then LtL(n + I )J = LtLnJ . On the other hand, if tLn LtLnJ :::: 1 - tL, then tL(n + 1) :::: 1 + LtLnJ , so LtL(n + I )J = LtLnJ + 1 , as desired. 71. f(O) = 1 , m (O) = 0; f( l ) = 1 , m ( l ) = 0 ; f(2) = 2 , m(2) = 1 ; f(3) = 2 , m(3) = 2 ; f(4) = 3, m(4) = 2; f(5) = 3, m(5) = 3; f(6) = 4, m(6) = 4; f(7) = 5, m(7) = 4; f(8) = 5, m(8) = 5; f(9) = 6, m(9) = 6 73. The last occurrence of n is in the position for which . .

the total number of 1 's, 2's, . . . , n ' s all together is that posi­ tion number. But because ak is the number of occurrences of k, this is just L�= I ak , as desired. Because f(n) is the sum of the first n terms of the sequence, f(f(n» is the sum of the first f(n) terms of the sequence. But because f(n) is the last term whose value is n, this means that the sum is the sum of all terms of the sequence whose value is at most n . Because there are ak terms of the sequence whose value is k, this sum is L�= I k . at, as desired

61. procedure generate(n: nonnegative integer) if n is odd then begin

S := S(n - 1 ); T : = T (n en d else if n = 0 then

CHAPTER S

- 1)

begin

S := 0; T := {A} end else

begin

TI := T (n - 2); S I := S(n - 2) T := TI U {(x) I x E TI U S I and l(x) = n - 2 } S : = S I U {xy I x E TI and Y E TI U S I and l(xy) = n } end {T U S i s the set o f balanced strings of length at most n }

Section 5.1

3. a) 4 1 0 b) 5 1 0 5. 42 7. 263 9. 676 1 1. 28 13. n + 1 (counting the empty string) 15. 475,255 (counting the empty string) 17. 1 ,32 1 ,368,96 1 19. a) Seven: 56, 63, 70, 77, 84, 9 1 , 98 b) Five: 55, 66, 77, 88, 99 c) One: 77 21. a) 128 b) 450 c) 9 d) 675 � � Q � � m � � ll � � � � c) 27 25. 3 50 27. 52,457,600 29. 20,077,200 31. a) 37,822,859,36 1 b) 8,204,7 1 6,800 c) 40, 1 59,050, 880 d) 1 2, 1 13,640,000 e) 1 7 1 ,004,205,2 1 5 Q 72,043, 541 ,640 g) 6,230,72 1 ,635 h) 223,149,655 . 33. a) 0 b) 120 c) 720 d) 2520 35. a) 2 if n = I , 2 if n = 2, 0 1. a) 5850

b) 343

S-38

Answers to Odd-Numbered Exercises

if n :::: 3 b) 2 n - 2 for n > 1 ; 1 if n = 1 c) 2(n - 1 ) 37. (n + l)m 39. If n is even, 2n /2 ; if n is odd, 2(n + I )/2 4 1 . a) 240 b) 480 c) 360 43. 352 45. 1 47 47. 33 49. a) 9,920, 67 1 , 339,26 1 , 325 , 54 1 , 376 � 9.9 x 1 02 1 b) 6,64 1 , 5 14,96 1 , 387,068,437, 760 � 6.6 x 1 02 1 c) 9,920, 67 1 , 339.26 1 32554 1 376 seconds, which is about 3 14,000 years 51. 7, 1 04,000,000,000 53. 1 8 55. 1 7 57. 22 59. Let P(m ) be the sum rule for m tasks. For the ba­ sis case take m = 2. This isjust the sumrule for two tasks. Now assume that P(m) is true. Consider m + 1 tasks, TI , T2 , . . . , Tm , Tm +l , which can be done in n " n 2 , . . . , nm , nm +1 ways, respectively, such that no two of these tasks can be done at the same time. To do one of these tasks, we can either do one of the first m of these or do task Tm +l . By the sum rule for two tasks, the number of ways to do this is the sum of the number of ways to do one of the first m tasks, plus nm +l . By the inductive hypothesis, this is n 1 + n 2 + . . . + nm + nm +1 ' as desired. 6 1 . n(n - 3)/2 S ection 5.2 1.

Because there are six classes, but only five weekdays, the pi­ geonhole principle shows that at least two classes must be held on the same day. 3. a) 3 b) 1 4 5. Because there are four possible remainders when an integer is divided by 4, the pi­ geonhole principle implies that given five integers, at least two have the same remainder. 7. Let a , a + 1 , . . . , a + n - 1 be the integers in the sequence. The integers (a + i) mod n, i = 0, 1 , 2, . . . , n - 1 , are distinct, because 0 < (a + j) - (a + k) < n whenever 0 :s k < j :s n - 1 . Because there are n possible values for (a + i ) mod n and there are n different integers in the set, each of these values is taken on exactly once. It follows that there is exactly one integer in the se­ quence that is divisible by n. 9. 495 1 1 1 . The midpoint of the segment joining the points (a, b , c) and (d, e, f) is «a + d)/2, (b + e)/2, (c + /)/2). It has integer coefficients if and only if a and d have the same parity, b and e have the same parity, and c and / have the same parity. Because there are eight possible triples of parity [such as (even, odd, even)], by the pigeonhole principle at least two of the nine points have the same triple of parities. The midpoint of the segment join­ ing two such points has integer coefficients. 13. a) Group the first eight positive integers into four subsets of two inte­ gers each so that the integers of each subset add up to 9: { I , 8}, {2, 7}, {3, 6}, and {4, 5 } . If five integers are selected from the first eight positive integers, by the pigeonhole principle at least two of them come from the same subset. Two such integers have a sum of 9, as desired. b) No. Take { I , 2, 3 , 4}, for ex­ ample. 15. 4 1 7. 2 1 ,25 1 19. a) Ifthere were fewer than 9 freshmen, fewer than 9 sophomores, and fewer than 9 ju­ niors in the class, there would be no more than 8 with each of these three class standings, for a total of at most 24 students, contradicting the fact that there are 25 students in the class. b) If there were fewer than 3 freshmen, fewer than 19 sopho­ mores, and fewer than 5 juniors, then there would be at most 2 freshmen, at most 1 8 sophomores, and at most 4 juniors, for a total of at most 24 students. This contradicts the fact that

S-38

there are 25 students in the class. 21. 4, 3, 2, 1 , 8, 7, 6, 5, 1 2, 1 1 , 1 0, 9, 1 6, 15, 14, 1 3 23. procedure long(a l , . . . , an : positive integers) {first find longest increasing subsequence} max := 0; set := 00 . . . 00 { n bits} for i := 1 to 2n

begin

last := 0; count := 0, OK := true for j := 1 to n begin if setU) = 1 then begin if aj > last then last := aj

count : = count + 1

end else OK := false end if count > max then begin

max := count best := set

end

set := set + 1 (binary addition) end {max is length and best indicates the sequence}

{repeat for decreasing subsequence with only changes being aj < last instead of aj > last and last := 00 instead of last := O} 25. By symmetry we need prove only the first statement. Let A be one of the people. Either A has at least four friends, or A has at least six enemies among the other nine people (because 3 + 5 < 9). Suppose, in the first case, that B , C , D , and E are all A 's friends. If any two of these are friends with each other, then we have found three mutual friends. Otherwise { B , C , D , E } is a set of four mutual enemies. In the second case, let { B , C , D , E, F, G } be a set of enemies of A. By Example 1 1 , among B , C , D, E, F, and G there are either three mutual friends or three mutual enemies, who form, with A, a set of four mutual enemies. 27. We need to show two things: that if we have a group of n people, then among them we must find either a pair of friends or a subset of n of them all of whom are mutual enemies; and that there exists a group of n - 1 people for which this is not possible. For the first statement, if there is any pair of friends, then the condition is satisfied, and if not, then every pair of people are enemies, so the second condition is satisfied. For the second statement, if we have a group of n -'- I people all of whom are enemies of each other, then there is neither a pair of friends nor a subset of n of them all of whom are mutual enemies. 29. There are 6,432,8 1 6 possibilities for the three initials and a birthday. So, by the generalized pigeonhole principle, there are at least r36, 000, 000/6,432, 8 1 61 = 6 people who share the same initials and birthday. 3 1 . 1 8 33. Because there are six computers, the number of other computers a computer is connected to is an integer between 0 and 5, inclusive. How­ ever, 0 and 5 cannot both occur. To see this, note that if some computer is connected to no others, then no computer is con­ nected to all five others, and if some computer is connected to all five others, then no computer is connected to no others.

Answers to Odd-Numbered Exercises

S-3 9

Hence, by the pigeonhole principle, because there are at most five possibilities for the number of computers a computer is connected to, there are at least two computers in the set of six connected to the same number of others. 35. Label the com­ puters C I through C 1 00 , and label the printers PI through P20 . Ifwe connect Ck to Pk for k = 1 , 2, . . . , 20 and connect each of the computers C2 1 through C IOO to all the printers, then we have used a total of20 + 80 . 20 = 1 620 cables. Clearly this is sufficient, because if computers C I through C20 need printers, then they can use the printers with the same subscripts, and if any computers with higher subscripts need a printer instead of one or more of these, then they can use the printers that are not being used, because they are connected to all the printers. Now we must show that 1 6 1 9 cables is not enough. Because there are 1 6 1 9 cables and 20 printers, the average number of com­ puters per printer is 1 6 1 9/20, which is less than 8 1 . Therefore some printer must be connected to fewer than 8 1 computers. That means it is connected to 80 or fewer computers, so there are 20 computers that are not connected to it. If those 20 com­ puters all needed a printer simultaneously, then they would be out of luck, because they are connected to at most the 1 9 other printers. 37. Let aj be the number of matches com­ pleted by hour i. Then 1 � a l < a2 < . . . < a75 � 125. Also 25 � a l + 24 < a2 + 24 < . . . < a75 + 24 � 149. There are 1 50 numbers a I , . . . , a75 , a l + 24, . . . , a75 + 24. By the pi­ geonhole principle, at least two are equal. Because all the aj S are distinct and all the (aj + 24)s are distinct, it follows that aj = aj + 24 for some i > j . Thus, in the period from the U + l )st to the ith hour, there are exactly 24 matches. 39. Use the generalized pigeonhole principle, placing the l S I objects f(s) for s E S in I T I boxes, one for each element of T . 4 1 . Let dj be jx - NUx), where NUx) is the in­ teger closest to jx for 1 � j � n . Each dj is an irrational number between - 1 12 and 1 /2. We will assume that n is even; the case where n is odd is messier. Consider the n in­ tervals {x I jln < x < U + l )ln}, {x I -U + l )ln < x < - j In} for j = 0, 1 , . . . , (nI2) - 1 . If dj belongs to the inter­ val {x 1 0 < x < l i n } or to the interval {x I - l in < x < O} for some j, we are done. If not, because there are n - 2 intervals and n numbers dj , the pigeonhole principle tells us that there is an interval {x I (k - 1 )ln < x < kin } con­ taining dr and ds with r < s. The proof can be finished by showing that (s - r)x is within 1 I n of its nearest integer. 43. a) Assume that ik � n for all k. Then by the generalized pigeonhole principle, at least r(n 2 + l )/nl = n + 1 of the numbers i I , i2 , . . " in 2 + 1 are equal. b) If akj < akj+ I ' then the subsequence consisting of akj followed by the increasing subsequence oflength ikj+1 starting at akj+1 contradicts the fact that hj = hj+l • Hence, akj > akj+! . c) If there is no increas­ ing subsequence of length greater than n, then parts (a) and (b) apply. Therefore, we have ak.+1 > ak. > . . . > ak2 > ak p a decreasing sequence of length n + 1 . Section 5.3

l . abc, acb, bac, bca, cab, cba c) 8 d) 6720 e) 40,320

3. 720 5. a) 120 b) 720 t) 3,628,800 7. 1 5 , 1 20

S-39

1 320 1 1 . a) 2 1 0 b) 386 c) 848 d) 252 13. 2(n !)2 1 5. 65,780 1 7. 2 1 00 - 505 1 19. a) 1 024 b) 45 c) 1 76 d) 252 21. a) 1 20 b) 24 c) 1 20 d) 24 e) 6 t) 0 23. 609,638,400 25. a) 94, 109,400 b) 94 1 ,094 c) 3,764,376 d) 90,345,024 e) 1 14,072 t) 2328 g) 24 27. a) 1 2,650 b) 79,727,040 i) 3,764,376 j) 1 09,440 b) 303,600 29. a) 37,927 b) 1 8,9 1 5 3 1 . a) 122,523,030 b) 72,930,375 c) 223 , 1 49,655 d) 1 00,626,625 33. 54,600 35. 45 37. 9 1 2 39. 1 1 ,232,000 4 1 . 13 43. 873 9.

S ection 5.4

x 4 + 4x 3 y + 6x 2y2 + 4xy 3 5+ y4 3. x6 + 6x 5 y + 1 5x 4 2 y + 2 0x 3 y 3 + 1 5x 2y4 + 6xy + y6 5. 1 0 1 7. -2 IO C:) = 9. -2 101 39g e:) 1 1 . ( _ 1 )a 1 · p(X = r) = p(X 2: a) 33. a) 1 0/ 1 1 b) 0.9984 -35. a) Each of the n ! permutations occurs with probability l/n!, so E(X) is

31. E(X)/a

the number of comparisons, averaged over all these permu­ tations. b) Even if the algorithm continues n - 1 rounds, X will be at most n(n - 1 )/2. It follows from the formula for expectation that E(X) ::; n(n - 1 )/2. c) The algorithm proceeds by comparing adjacent elements and then swap­ ping them if necessary. Thus, the only way that inverted el­ ements can become uninverted is for them to be compared and swapped. d) Because X(P) 2: I(P) for all P, it fol­ lows from the definition of expectation that E(X) 2: E(I) . e) This summation counts 1 for every instance of an in­ version. t) This follows from Theorem 3 . g) By Theo­ rem 2 with n = 1 , the expectation of hk is the probabil­ ity that ak precedes aj in the permutation. This is clearly 1 /2 by symmetry. b) The summation in part (f) consists of C(n, 2) = n(n - 1 )/2 terms, each equal to 1 /2, so the sum is n(n - 1 )/4. i) From part (a) and part (b) we know that E(X), the object of interest, is at most n(n - 1 )/2, and from part (d) and part (h) we know that E(X) is at least n(n - 1 )/4, both of which are E>(n 2 ) . 37. 1 39. V(X +

Y) = E«X + y)2 ) E(X + y)2 = E(X 2 + 2XY + y 2 ) [E(X) + E(y)] 2 = E(X 2 ) + 2E(XY) + E(y 2 ) - E(X)2 2E(X)E(Y) - E(y)2 = E(X2) - E(X)2 + 2[E(XY) E(X)E(Y)] + E(y 2 ) - E(y)2 = V(X) + 2 Cov(X , Y) + V(Y) 41. [(n - 1 )/ n r 43. (n - l)m /n m - I _

_

1 /2n , because that happens precisely when the player gets n 1 tails followed by a head. The expected value of the winnings is therefore the sum of2 n times 1/2n as n goes from 1 to infin­ ity. Because each ofthese terms is 1 , the sum is infinite. In other words,.one should be willing to wager any amount of money and expect to come out ahead in the long run. b) $9, $9 2 1 . a) 1 /3 when S = { I , 2, 3 , 4, 5 , 6, 7, 8, 9, 10, 1 1 , 12}, { I , 2, 3 , 4, 5 , 6, 7, 8, 9}, and B = { I , 2, 3 , 4}; 1 / 1 2 when { 1 , 2, 3 , 4, 5 , 6, 7, 8, 9, 1 0, 1 1 , 12}, A = {4, 5 , 6, 7, 8, 9, 10, 1 1 , 12}, and B = { I , 2, 3 , 4} b) 1 when S = { 1 ,2, 3 , 4, 5 , 6, 7, 8, 9, 10, I I , 12}, A = {4, 5 , 6, 7, 8, 9, 10, I I , 12}, and B = { I , 2, 3 , 4} ; 3/4 when S = { I , 2, 3 , 4, 5 , 6, 7, 8, 9, 10, 1 1 , 12}, A = { I , 2, 3 , 4, 5 , 6, 7, 8, 9}, and B = { l , 2, 3 , 4} 23. a) p(EI n E 2 ) = p(EI )p(E2 ), p(EI n E3) = p(EI) A S

=

=

p(E3), p(E2 n E3) = P(E2 )p(E3), p(E I n E2 n E3) = p(E I) b) Yes P(E2 )P(E3) c) Yes; yes d) Yes; no e) 2n - n - 1 25. V(aX + b) = E«aX + b)2 ) - E(aX + b)2 = E(a 2 X 2 + 2abX + b2 ) - [aE(X) + b] 2 = E(a 2 X 2 ) + E(2abX) + E(b2 ) - [a 2 E(X)2 + 2abE(X) + b2 ] = a 2 E(X 2 ) + 2abE(X) + b2 - a 2 E(X)2 - 2abE(X) ­ b2 = a 2 [E(X 2 ) - E(X)2 ] = a 2 V(X) 27. To count every

element in the sample space exactly once, we must include every element in each of the sets and then take away the double counting of the elements in the intersections. Thus

p(EI U E2 U . . . U Em) = p(EI) + P(E2 ) + . . . + p(Em) ­ p(EI n E2 ) - p(EI n E3) - . . . - p(EI n Em) - P(E2 n E3) - P(E2 n E4) - . . . - P(E2 n Em) - . . . - p(Em- 1 n Em) = qm - (m(m - 1)/2)r, because C(m , 2) terms are be­ ing subtracted. But p(EI U E2 U · · · U Em) = 1 , so we have qm - [m(m - 1 ) /2 ]r = 1 . Because r 2: 0, this equation tells us that qm 2: 1 , so q 2: l / m . Because q ::; 1, this equation also implies that [m(m - 1 )/2]r = qm - 1 ::; m - 1 , from which it follows that r ::; 2/ m . 29. a) We purchase the

cards until we have gotten one of each type. That means we have purchased X cards in all. On the other hand, that also means that we purchased X 0 cards until we got the first type we got, and then purchased X I more cards un­ til we got the second type we got, and so on. Thus, X is the sum of the X / s. b) Once j distinct types have been obtained, there are n - j new types available out of a to­ tal of n types available. Because it is equally likely that we get each type, the probability of success on the next pur­ chase (getting a new type) is (n - j )/ n . c) This follows immediately from the definition of geometric distribution, the definition of Xj , and part (b). d) From part (c) it follows that E(Xj ) = n/(n - j). Thus by the linearity of expectation and part (a), we have E(X) = E(Xo) + E(Xt} + . . . + E(Xn - l ) e) About = !!. + ...!LI + . . . + !!.I = n (! + _1_I + . . . + !). I n nn n224.46 3 1 . 24 . 1 34/(52 . 5 1 . 50 . 49)

Supplementary Exercises

b) 4/C(52, 1 3) 1. 1 / 1 09, 668 3. a) 1 /C(52, 13) c) 2,944, 656/C(52, 13) d) 3 5 , 335, 872/C(52, 1 3) 5. a) 9/2 b) 2 1 /4 7. a) 9 b) 2 1 /2 9. a) 8 b) 49/6 b) p( 1 - p)k - I , where p = n /2n -1 1 1. a) n /2 n - 1 m m . n )- I 15. a) 2/3 b) 2/3 I ( ( -�):!�( ) 1 n c) 2n - / n 13. 1 7. 1 /32 1 9. a) The probability that one wins 2n dollars is

CHAPTER 7 Section 7.1

l . a) 2, 12, 72, 432, 2592 b) 2, 4, 16, 256, 65, 536 c) 1 , 2, 5 , 1 1 , 26 d) I, 1 , 6, 27, 204 e) 1 , 2, 0, 1 , 3

S-44

Answers to Odd-Numbered Exercises

3. a) 6, 17, 49, 143, 421 b) 49 = 5 . 17 - 6 · 6, 143 = 5 . 49 -n -16 · 17, 421n - l= 5 · 143n -2- 6 · 49 n -2c) 5an -n I-2- 6an - 2 = 5(2 + 5 . 3 ) - 6(2 + 5 · 3 ) 2 (10 - 6) + 3 n -2 (75 - 30) = 2n -2 . 4 + 3 n -2 . 9 . 5 = 2n + 3 n . 5 = an 5. a) Yes b) No c) No d) Yes e) Yes t) Yes g) No b) No 7. a) an - I + 2an -2 + 2n - 9 = -(n - 1) + 2 + 2 b) an I + [ -(n - 2) + 2] + 2n - 9 =n -n + 2 = an 2 n_ 2 + 2n - 9 = 5(- I) - 1 - n(n- -2 1) + 2 + 2[5(- I)n --2 (n - 2) + 2] + 2n - 9 = 5( _ l) (_ 1 + 2) - n + 2 = an c) an - I + 2an -2 + 2n - 9 = 3(- l)n - I +2n - I - (n - 1) + 2 + 2[3(- l)n -2 + n2-2n -2 - (n - 2) + 2] + 2n - 9 = 3(_ I)n -2 (- 1 + 2) + 2 (2 + 2) - n + 2 an d) an I + 2an -2 + 2n - 9 = 7 · 2n - 1 -n (n - 1) + 2 + 2[7 · 2n --2 _ (n - 2) + 2] + 2n - 9 = 2 -2 (7 . 2 + 2 . 7) - n + 2 = an 9. a) an = 2 . 3 n b) an = 2 n + 3 c) an = 1 + n(n + 1)/2 d) an = n 2 + 4n + 4 e) an = 1 t) an = (3 n + 1 - 1)/2 n 1 1 . a) an = 3an_ 1 b) an = 2 n ! g) an = 5 n ! b) 5,904,900 13. a) an = n + an - I , ao 0 b) a\ 2 = 78 c) an = n(n + 1)/2 15. B (k) = [1 + (0.07/12)] B (k - 1) 100, with B(O) = 5000 17. Let P(n) be "Hn = 2n - I." Basis step : P(1) is true because HI = 1 . Inductive step: n Assume that Hn = 2 - 1 . Then because Hn 1 = 2Hn + 1, + it follows that Hn 1 = 2(2 n - 1) + 1 = 2 n + 1 - 1. 19. a) an = + 2an_ 1 + an - s for n � 5 b) ao = 1 , a l = 2, a3 = 8, a4 = 16 c ) 1217 21. 9494 23. a) an = an - I + an -2 + 2n -2 for n � 2 c) 94 b) ao = 0, a l = 0 25. a) an = an - I + an -2 + an - 3 for n � 3 b) ao = 1, a l = 2, a2 = 4 c) 81 27. a) an = an - I + an - 2 for n � 2 b) ao = 1 , a l = 1 c) 34 29. a) an = 2an_ 1 + 2an -2 for n � 2 b) ao = 1, a l = 3 c) 448 31. a) an = 2an -1 + an_ 2 for n � 2 b) ao = l , a l = 3 c) 239 33. a) an = 2an_ 1 for n � 2 b) a l = 3 c) 96 35. a) an = an - I + an -2 for n � 2 b) ao = 1, a l = 1 c) 89 37. a) Rn = n + Rn - t . Ro = 1 b) Rn = n(n + 1)/2 + 1 3 9 . a) Sn = Sn - I + (n 2 - n + 2)/2, So = 1 b) Sn = (n 3 + 5n + 6)/6 4 1 . 64 43. a) an = 2an - 1 + 2an -2 b) ao = 1 , a l = 3 c) 1224 45. Clearly, S(m , 1) = 1 for m � 1 . If m � n, =

a

=

=

then a function that i s not onto from the set with m el­ ements to the set with n elements can be specified by picking the size of the range, which is an integer be­ tween and n inclusive, picking the elements of the range, which can be done in C(n , k) ways, and picking an onto function onto the range, which can be done in S(m , k) ways. Hence, there are C(n , k)S(m , k) func­ tions that are not onto. But there are nm functions altogether,

1

-1

L�::

47. a) C s = so S(m , n) = nm - L �:: C(n , k)S(m , k). COC4 + C I C3 + C2 C 2 + C3CI + C4Co = 1 · 14 + 1 · 5 + 2 · 2 + 5 . 1 + 14 · 1 = 42 b) C(10, 5)/6 = 42 49. J(I) =

1, J(2) = 1, J(3) = 3, J(4) = 1, J(5) = 3, J(6) = 5, J(7) = 7, J(8) = 1, J(9) = 3, J( IO) = 5, J(I I) = 7, J(12) = 9 , J(13) = 1 1, J(14) = 13, J(15) = 15, J(16) = 1 51. First,

suppose that the number of people is even, say 2n . After going around the circle once and returning to the first person, because the people at locations with even numbers have been eliminated, there are exactly n people left and the person currently at location i is the person who was originally at lo­ cation i Therefore, the survivor [originally in location

2 - 1.

S-44

J(2n)]

J(n); J(2n) 2J(n) - 1. 2 1,

is now in location this was the person who was at location Hence, = Similarly, when there are an odd number of people, say n + then after going around the circle once and then eliminating person there are n people left and the person currently at location i is the person who was at location i + Therefore, the survivor will be the player currently occupying location (n), namely, the person who was originally at location + Hence, + = + The base case is = These nine moves solve the puzzle: Move disk from peg to peg move disk from peg to peg move disk from peg to peg move disk from peg to peg move disk from peg to peg move disk from peg to peg move disk from peg to peg move disk from peg to peg move disk from peg to peg To see that at least nine moves are required, first note that at least seven moves are required no matter how many pegs are present: three to unstack the disks, one to move the largest disk and three more moves to restack them. At least two other moves are needed, because to move disk from peg to peg the other three disks must be on pegs and so at least one move is needed to restack The base cases are them and one move to unstack them. obvious. If n > the algorithm consists of three stages. In the first stage, by the inductive hypothesis, - k) moves are used to transfer the smallest n k disks to peg Then using the usual three-peg Tower of Hanoi algorithm, it takes moves to transfer the rest of the disks (the largest k disks) to peg avoiding peg Then again by the inductive hypothesis, it takes - k) moves to transfer the small­ est n k disks to peg all the pegs are available for this, because the largest disks, now on peg do not interfere. This establishes the recurrence relation. 59. First note that = [which follows because the sum is telescoping and = By Exercise this is the sum of - I for this range of values of j . Therefore, the sum I is I i - , except that if n is not a triangular number, then the last few values when i = k are missing, and that is what the final term in the given expression accounts for. 6 1 . By Exercise n is no larger than i 2 . It can be shown that this sum equals (k + + so it is no greater than (k + . Because n > k(k the quadratic formula can be used to show that k < + ..tIn for all n > Therefore, n is bounded above by ( 1 + ..tIn + < for all n > . . Hence, n is o a) b) c) d) +

2J(n) - 1.

1,

1.

2

J 2J(n) 1. J(2n 1) 2J(n) 1. J(l) 1. 53. 73, 977, 3617 55. 1 1 2; 2 1 3; 1 2 3; 3 1 2; 4 1 4; 3 2 4; 1 3 2; 2 3 4; 1 2 4. 4,

4

1 3, 1,

2

4

57.

R(n

-

2k - 1

4,

-

R(n 4;

2.

2.

R(n) L� = I [RU) - RU - 1)] R(O) 0]. 2'" L � 2;

4,

58,

=

L�= ;-I 1)2k - 2k+I 1 1, - 1)/2, 1 1. 1)2 1 +J2Ii 8.jii2 J21i 2 n -1 nR(-2 ) (.jii2 J21i). 63.2 0 0 2 2 -2 65. an - 2Van V an = an - 2(an - an I ) + (Van - Van - I ) = -an + 2an_ 1 + [(an - an - I ) - -(an - I - an -2 )] = -an + 2an_ 1 + (an - 2an -1 + an -2 ) an -2 2 67. an = an - I + an -22 = (an - Van) + (an - 2Van + V an) = 2an - 3Van + V an , or an = 3Van - V 2 an 59, R( ) 1 )2k R( )

=

S ection 7.2 1. a) Degree t) Degree

2

b) No c) Degree 4 d) No g) No 3. a) an = 3 · 2n b) an = 2

3

e) No

c) an

=

Answers to Odd-Numbered Exercises S-45

S-45

3 · 2n - 2 . 3 n d) an = 6 · 2n - 2 · n2 n e) an = n( _2)n -1 1) an = 2n - (_2)n g) an = (1 /2)n + 1 - (- 1 /2)n + 1 + +l 5. an = Js ( 1 +2J5 r - Js (, -2J5 r 1 7. [2n+ 1 + (- I )n ]/3 9. a) Pn = 1 .2Pn - 1 + 0.45Pn - 2 , Po = 100,000, PI = 120,000 b) Pn (250,000/3)(3/2)n + (50,000/3)(-3/ 10)n 1 1 . a) Basis step: For n = 1 we have 1 = 0 + 1 , and for n = 2 we have 3 = 1 + 2. Inductive step : Assume true for k :::: n . Then L n+ 1 = Ln + L n-I = In - I + fn + 1 + 1n - 2 + fn = (In - I + fn - 2 ) + ( In + 1 + fn ) = fn + 1n +2 . b) Ln = ( ' +2J5 r + ( , -2J5 r 13. an = 8( _ l )n - 3( _2)n + 4 . 3 n 15. an = 5 + 3( _2)n - 3n 17. Let an = C(n , 0) + C(n 1 , 1) + . . . + C(n - k, k) where k = Ln/2J . First, assume that n is even, so that k = n /2, and the last term is C(k, k). By Pascal's Identity we have an = 1 + C(n 2, 0) + C(n - 2, 1) + C(n - 3, 1) + C(n - 3, 2) + . . . + C(n -' k, k - 2) + C(n - k, k - 1 ) + 1 = 1 + C(n - 2, 1 ) + C(n - 3, 2) + . . . + C(n - k, k - 1) + C(n - 2, 0) + C(n 3, 1) + . . . + C(n - k, k - 2) + 1 = an - I + an - 2 because L(n - 1)/2J = k - 1 = L(n - 2)/2J . A similar calculation works when n is odd. Hence, { an } satisfies the recurrence re­ lation an = an - I + an - 2 for all positive integers n , n 2: 2. Also, a l = C( 1 , 0) = 1 and a2 = C(2, 0) + C(1 , 1) = 2, which are h and h. It follows that an = fn+ I for all posi­ tive integers n . 19. an = (n 2 + 3n + 5)( _ l)n 2 1 . (a l 0 + a l, l n + al ,2n 2 + a l,3 n 3 ) + (a2,O + a2, I n + a2,2 n 2 )( _2)'n + (a3,O + a3, I n)3 n + a4, o(-4l 23. a) 3an- 1 + 2n = 3( _2)n + 2n = 2n ( -3 + 1) = _2n + 1 = an b) an = a3 n - 2n+ 1 n n I 1 + + 25. a) A = - I , B = -7 c) an = 3 - 2 b) an = a2 n - n - 7 27. a) P3 n 3 + c) an = 1 1 · 2 n - n - 7 b) n 2 Po( _2)n c) n 2 (pl n + Po)2n P2 n 2 + 2P I n + Po 2 2 d) (P2 n + P I n + Po W e) n (p2n + P I n + Po)( _2)n 3 4 2 2 g) Po 1) n (p4n + P3n + P2 n + P I n + Po)2n 29. a) an = a2n + 3 n+ 1 b) an = - 2 · 2n + 3 n+ 1 31. an = a2n + {33 n - n . 2n + 1 + 3n/2 + 2 1 /4 33. an = (a + {3n + n 2 + n 3 /6)2n 35. an = -4 · 2 n - n 2 /4 - 5n/2 + 1/8 + n 37. an = n(n + l )(n + 2)/6 39. a) 1 , - I , i, (39/8)3 -i b) an = � - �(_ I )n + 2ti i n + 24"i (_i)n 41. a) Using the formula for fn , we see that I fn - Js ( I +/'5 r I =

I Js (,-l)" I

1 / J5 < 1 /2. This means that In is the b) Less when n is even; integer closest to Js ( 1 +2J5 r . 43. an = fn - I + 2 1n - 1 greater when n is odd 45. a) an = 3an _ 1 + 4an -2 , ao = 2, a l = 6 b) an = [4"+ 1 + (- I)n ]/5 47. a) an = 2an+ 1 + (n - 1 ) 10,000 49. an = b) an = 70,000 . 2n- 1 - 10, 000n - 10,000 5n 2 / 1 2 + 13n/ 12 + 1 51. See Chapter 1 1 , Section 5 in [Ma93] . 53. 6n . 4n - 1 /n
best then best := sum

end end

{best is the maximum possible sum of numbers

in the list} b) O(n 2 ) c) We divide the list into a first half and a second half and apply the algorithm recursively to find the largest sum of consecutive terms for each half. The largest sum of consec­ utive terms in the entire sequence is either one of these two numbers or the sum of a sequence of consecutive terms that crosses the middle of the list. To find the largest possible sum of a sequence of consecutive terms that crosses the middle of the list, we start at the middle and move forward to find the largest possible sum in the second half of the list, and move backward to find the largest possible sum in the first half of the list; the desired sum is the sum of these two quantities. The final answer is then the largest of this sum and the two answers obtained recursively. The base case is that the largest sum of a sequence of one term is the larger of that number and o. d) 1 1 , 9, 14 e) S (n) = 2 S (n/2) + n , C(n) = 2C(n /2) + n + 2, S ( 1 ) = 0, C(1) = 1 1) O (n log n), better than O(n 2 ) 25. ( 1 , 6) and (3, 6) at distance 2 27. The algorithm is essentially the same as the algorithm given in Example 12. The central strip still has width 2d but we need to consider just two boxes of size d x d rather than eight boxes of size (d/2) x (d/2). The recurrence relation is the same as the recurrence relation in Example 12, except that the coefficient 7 is replaced by 1. 29. With k = 10gb n , it follows that f(n) = a k f( I ) + LS :� aj c(n/bj)d =

a k f(l ) + L�:� cnd = a k f( l ) + kcnd = alol!J, n f( l ) + c(logb n)nd = n logb Q f( I) + cnd 10gb n = nd f( l ) + cnd 10gb n. 31. Let k = 10gb n where n i s a power o f b. Basis step : If n = 1 and k = 0, then c l nd + C2 nlol!J, a = C I + C2 = bdc/ (bd - a) + f( I) + bdc/(a - bd) = f(I ). Inductive step: Assume true for k, where n = bk • Then for n = bk +l , f(n) = af(n/b) + cnd = a{ [bdc/(bd - a) ](n/b)d + [ f( I) + bdc/ (a - bd)] . (n/b)logb Q)} + cnd = bdc/(bd - a)nda/bd + [f( 1 ) + bdc/(a - bd)]nlol!J, a + cnd = nd[ac/(bd - a) + c(bd - a)/(bd - a)] + [ /( 1) + bdc/(a - bdc)]nlogb a = 33. If [bdc/(bd - a)] nd + [f( I ) + bdc/(a - bd)] nJogb Q .

S-46

Answers to Odd-Numbered Exercises

a > bd, then 10gb a > d, so the second term dominates, giving O(n lDgJ, a) . 3 5. 0 (n l"84 5 ) 3 7. 0 (n 3 ) S ection 7.4

1. f(x) = 2(x 6 - 1 )/(x - 1) 3. a) f(x) = 2x( 1 - x 6 )/(1 x) b) x 3 /( 1 - x) c) x/( 1 - x 3 ) d) 2/( 1 - 2x) e) ( 1 + x f t) 2/( 1 + x) g) [ 1 /( 1 - x)] - x 2 h) x 3 /( 1 - x)2 5. a) 5/( 1 - x) b) 1 /( 1 - 3x) c) 2x 3 /( 1 - x) d) (3 - x)/ 7. a) ao = -64, ( 1 - x)2 e) ( 1 + x) 8 t) 1 /( I - x)5 al = 1 44, a2 = - 1 08, a3 = 27, and a" = 0 for all n � 4 b) The only nonzero coefficients are ao = 1 , a3 = 3, a6 = 3, a9 = 1. c) a" = 5" d) an = ( _ 3)n -3 for n � 3, and ao = al = a2 = 0 e) ao = 8, al = 3, a2 = 2, a" = 0 for odd n greater than 2 and a" = 1 for even n greater than 2 t) an = 1 if n is a positive multiple 4, an = - 1 if n < 4, and an = 0 otherwise g) a" = n - 1 for n � 2 and ao = al = 0 h) an = 2,, + I / n ! 9. a) 6 b) 3 c) 9 d) O e) 5 1 1. a) 1024 b) 1 1 c) 66 d) 292,864 e) 20,4 12 13. 10 15. 50 17. 20 19. f(x) = 1 /[( I - x)(I - x 2 ) ( 1 - x 5 )( 1 - x 1 0)] 21. 1 5 2 3 . a) x 4 ( 1 + x + x 2 + x 3 )2 / ( 1 - x) b) 6 25. a) The coefficient of xr in the power se­ ries expansion of 1 /[( 1 - x 3 )( 1 - x 4 )(1 - x 20)] b) 1 /( 1 x3 - X 4 - x 20) c) 7 d) 3224 27. a) 3 b) 29 c) 29 d) 242 2 9 . a) 1 0 b) 49 c) 2 d) 4 3 1. a) G(x) ao - alx - a2x 2 b) G(x 2 ) c) x4G(x) d) G(2x) e) J; G(t)dt 33. ak = 2 · 3 k - 1 t) G(x)/( 1 - x) 35. ak = 1 8 · 3 k - 12 · 2 k 37. ak = k2 + 8k + 20 + (6k 1 8)2 k 39. Let G(x) = L �o fkX k . After shifting indices of summation and adding series, we see that G(x) - xG(x) ­ x 2 G(x) = fo + (fi - fo)x + L �2 (fk - fk - I - fk _ 2 )X k = 0 + x + L �2 0x k . Hence, G(x) - xG(x) - x 2 G(x) = x . Solving for G(x) gives G(x) = x / ( 1 - x - x 2 ). B y the method of partial fractions, it can be shown that x / ( 1 - x x 2 ) = (1 /.J5)[ 1 /( 1 - ax) - 1 /( 1 - fJx)], where a = ( 1 + .J5)/2 and fJ = ( 1 - .J5)/2. Using the fact that 1 /( 1 - ax) = L�o a kx k , it follows that G(x) = ( 1 /.J5) . L�o (a k ­ fJ k )x k . Hence, jk = ( 1 /.J5) . (a k - fJ k ). 41. a) Let G(x) = L�o C" x" be the generating function for {C,, } . Then G(x)2 = L;o ( L�=o ICk C,, - k ) x " = L� I ( L�:� Ck C,,_ I _k )X ,, - 1 = Ln = 1 c" x n - . Hence, xG(x)2 = L� I C" x n , which implies that xG(x)2 - G(x) + 1 = O. Applying the quadratic for­ mula shows that G(x) = I ± J�-4x . We choose the minus sign in this formula because the choice of the plus sign leads to a division by zero. b) By Exercise 40, ( 1 - 4x)- 1 /2 = L� e,," ) x" . Integrating term by term (which is valid by a theorem from calculus) shows that J; ( 1 - 4t)-1 /2 dt = I 2,, ) ,, +1 OO , 2,, ) x n . B ecause rX ( 1 � oo Jo L .n =O ,, + ' ( L... ,, =O ,, + 1 ( " x 4= X � " x I J 4t)-1 /2 dt = - t = xG(x), equating coefficients shows that C" = II! , e:). 43 . Applying the Binomial Theorem to the equality ( I + x)m + " = ( 1 + xr(l + x)" , shows that L�!o" C(m + n, r )x r = L�=o C(m, r)x r . Lr =o C(n, r ) x r = L�:; [ L�=o C (m , r - k) C (n, k)] x r . Comparing coeffi­ cients gives the desired identity. 45. a) 2e b) e-X c) e3x d) x e + e e) (e - I )/x 47. a) a" = ( _ I )n b) an = •

S-46

3 · 2" c) a" = 3" - 3 · 2" d) a,, = (-2)n for n � 2, al = -3, ao = 2 e) a,, = (-2)" + n ! t) a,, = (-3)" + n ! · 2" for n � 2, ao = l , al = -2 g) a" = O if n is odd and a" = n !/(n/2)! if n is even 49 . a) a" = 6a,,_ 1 + 8,, -1 for n � I , ao = 1 b) The general solution of the associated linear ho­ mogeneous recurrence relation is a�h) = a6" . A particular solution is a�p) = ! . 8" . Hence, the general solution is an = a6" + ! . 8" . Using the initial condition, it follows that a = ! . Hence, a" = (6" + 8")/2. c) Let G(x) = L�o akx k . Using the recurrence relation for {ak }, it can be shown that G(x) - 6xG(x) = ( I - 7x)/(1 - 8x). Hence, G(x) = ( l 7x)/[( 1 - 6x)(1 - 8x)]. Using partial fractions, it follows that G(x) = ( 1 /2)/(1 - 6x) + ( 1 /2)/(1 - 8x). With the help of Table l , it follows that a" = (6" + 8")/2. 51. I � 2xr . X 3 2 2xr . . . 53. (I + x)( I + x) ( I + x) . . . 55. The gener­ ating functions obtained in Exercises 52 and 53 are equal beI x' . , x6 . . . = cause ( I + x )( 1 + x 2 )(1 + x 3 ) . . . = I,-='x2x . R R .�" " 57. a) G x (l) = L �o P (X = k) · � , x . 2xr b) G � (1) = 1; L�o P(X = I k = L�o P (X = k) = I k) . x k lx = 1 = L�o p(X = k) . k . X k- l lx =1 = L�o p(X = k) . k = E(X) c) G � ( l ) = � L�o p(X = k) . x k lx = 1 = L,f=o P(X = k) · k(k - 1 ) · X k-2 I x = 1 = L �o P (X = k) · (Jr - k) = V (X) + E(X)2 - E(X ). Combining this with part (b) gives the desired results. 59. a) G(x) = pm /(1 - qx)m b) V(x) = mq/p2 •

Section 7.S

1. a) 30 b) 29 c) 24 d) 1 8 3 . 1 % 5 . a) 300 b) 1 50 c) 1 75 d) 100 7. 492 9. 974 1 1. 55 13. 248 15. 50, 138 1 7. 234 19. I A I U A2 U A3 U A4 U A5 1 = l A d + IA2 1 + I A3 1 + IA4 1 + I A5 1 - I A , n A2 1 - I A , n A3 1 - I A , n A4 1 I A , n A5 1 - IA2 n A3 1 - IA2 n A4 1 - IA2 n A5 1 - IA3 n A4 1 '- I A3 n A5 1 - I A4 n A5 1 + IA I n A2 n A3 1 + IA I n A2 n A4 1 + I A I n A2 n A5 1 + I A I n A3 n A4 1 + I AI n A3 n A5 1 + l A , n � n A5 1 + I A2 n A3 n A4 1 + I A2 n A3 n A5 1 + I A2 n A4 n A5 1 + I A3 n A4 n A5 1 - I A I n A2 n A3 n A4 1 I A I n A2 n A3 n A5 1 - l A , n A2 n A4 n A5 1 - l A , n A3 n A4 n A5 1 - I A2 n A3 n A4 n A5 1 + lA , n A2 n A3 n A4 n A5 1 21 . I A I U A2 U A 3 U A4 U A 5 U A6 1 = I A I I + IA2 1 + I A3 1 + I A4 1 + I A5 1 + IA61 - I A I n A2 1 - I A I n A3 1 - I A I n A4 1 I A I n A5 1 - I A I n A6 1 - IA2 n A3 1 - I A2 n A4 1 - IA2 n A5 1 - IA2 n A6 1 - I A3 n A4 1 - I A3 n A5 1 - I A3 n A6 1 I A4 n A5 1 - I A4 n A6 1 - I A5 n A6 1 23. p(E, U E2 U E3) = p(E, ) + p(E2) + p(E3) - p( E , n E2) - p(E, n 25. 4972/71 ,295 E3) - P (E2 n E3) + p(EI n E2 n E3) 27. p(E I U E2 U E3 U E4 U E5) = p(E I ) + p(E2 ) + P (E3) + p(E4) + P (E5) - p(EI n E2) - p(E I n E3) - p(EI n E4) - p(E, n E5) - P (E2 n E3) - P (E2 n E4) - P (E2 n E5) - P (E3 n E4) - p(E3 n E5) - P (E4 n E5) + p(E I n E2 n E3) + p(E, n E2 n E4) + p(EI n E2 n E5) + p(E I n E3 n E4) + p(E I n E3 n E5) + p(E I n E4 n E5) + p(E2 n E3 n E4) + P(E2 n E3 n E5) + p(E2 n E4 n E5) + P (E3 n E4 n E5) 29. p ( U7=, Ei ) = L I �i�" p(Ei) -

Answers to Odd-Numbered Exercises 5-47

S-4 7

'L, 1 �i d) [x + (x + Y + Z)] 1 5. a) x

x

xLx

1 o

0 1

0 1

1 0 1 0

c) x y x + y 1 0 1 0

1 1 0 0

1 1 1 0

1 0 0 0

(x L y)

(x L y) L (x L y)

0 0 0 1

1 1 1 0

c) x b) {[(x l x) l (z l z)] l y } I { [(x I x) I (z I z)] I y } d) [x I (y I y )] I [x I (y I y )] 19. It is impossible to repre­

sent x using + and . because there is no way to get the value o if the input is 1 .

3. (xy) + (z + x) l. (x + y)y (X + y + z) + (X + y + Z) v --l_.......

x ---t_-'

SI S2 S3

J-----,

v --l_.......

W

y --l�../

v --l_....... z ---t_-' v --l_.......

x ----+l--...., )------, Y ------.! b j ------.! x ------.!--...., J---...., Y -.;;x:o---+I b j --f.>co---+I x --f.>co---+l--""'" )--_....J Y ------.! bj --Dco-----.! x -...Dc:o-----.!--......, )------' y -.;;x:o---+I

5 . (x + y + z) +

x ----+l--...., )------, y ------.!

bj ------.!

x -...{)C..------.!--......, J---...., y ----+I bj ----+l_ _ x --!)c:..----+l-..... )--_...J y ------.!

bj ------+I

y --l�../ v --l_....... X

z --I�../

13.

y

v --l_....... Z --I_---

w --l_....... x

y ---t----

YI -----,.'L_--'

w --l_....... x

z ---t_---

)--'-''''-----'

w --l_.......

y

Z --I----

x --lw--....

)--"-'--_...J y z --l-"'"

S6

HA = half adder FA = full adder

x --{)C..----.!--...., )-------' y --!)c:..----+I

X

S5

X5 Y5

b j --Dc..----.!

w

S4

X4 Y4

bj ----+I

Section 11.3

w

XI YI

Yo

11.

1 7. a) {[(x I x) I (y I y )] I [(x I x) I (y I y )]} I (z I z )

7.

So

X3 Y3

(x L x) L (y L y)

0 1 0 1

0 0 1 1

1 0 0 0

Xo

X2 Y2

b) x y xy x L x y L y 1 1 0 0

9.

S-73

Xo � ��------�_/ -----+1

d

S- 74

Answers to Odd-Numbered Exercises

8-74

15. a)

:�x

Section 11.4

1. a)

b) x

x

x +y

y

y ---.'----'

c) x

3. a)

y

xy

x

b)

x

Y

y

y

y

y

x -_�--" y ------�

:EEJ

Y

Y

5. a)

x x

1

yz

yz

x

x

-'"

y

\.

Circuit from 1 5 (d)

Sum = x Elh

b) xyz , x y z, xyz Circuit from 1 5 (c)

1 9. Co CI X3 Co CI X2 Co CI XI Co CI

Xo

Carry = xy

yz

7. a)

yz

y�

x

yz

b)

.Vz

yz

X

X

1

1

[EJ [EJ y

c)

EE :

x

x -----I�

1 7.

b) xy and xy

:E8

y ---.'----'

d)

y

y

Answers to Odd-Numbered Exercises

S- 75

c)

S-75

1 5. a)

v-

X 1X4X, x3x4'1', x3.{4·1'5 Xj.{4x, ·1'j·1'4x, .{ 1·1'4X, ·1'1x4·1', ·l' lx4x,

1

1

x

1

r

1

9. Implicants: xyz, xyz, x y z, xyz, xy, xz, yz; prime impli­ cants: xy, xz, yz; essential prime implicants: xy, xz, yz

x

1

1

1

1

x

b)

1 1 . The 3-cube on the right corresponds to w; the 3-cube given by the top surface of the whole figure represents x ; the 3-cube given by the back surface of the whole figure represents y; the 3-cube given by the right surfaces ofboth the left and the right 3-cube represents z. In each case, the opposite 3-face repre­ sents the complemented literal. The 2-cube that represents wz is the right face of the 3-cube on the right; the 2-cube that represents xy is bottom rear; the 2-cube that represents y z is front left.

c)

yz

1 3. a)

yz

1

1

1

1

1

1

1

1

yz

d) .

wx

IV:\'

1

1

w:\'

1

1

1

1

1

1

wx

1

b) Wxyz, Wxyz, Wxy z, Wxy z

S-76

Answers to Odd-Numbered Exercises

S- 76

29.

I

I

I

I

I

I

I

I

I

I

I

I

I

I

I

I

z -__oc�-.....-...... y --'DO--� x --'DO--� w -__oc�-..... -"

z -__-I>C�-+r-...... y -----� x ------.r.._./ z ------.r-...... y -----.r x ---to.f>C>----.L_./ z -----� w -----.r

t) I

I

I

I

I

I

I

I

I

I

I

I

I

I

I

I

1 7. a) 64 b) 6 1 9. Rows 1 and 4 are considered adj acent. The pairs of columns considered adjacent are: columns 1 and 4, 1 and 12, 1 and 1 6, 2 and 1 1 , 2 and 1 5 , 3 and 6, 3 and 1 0, 4 and 9, 5 and 8, 5 and 1 6, 6 and 1 5, 7 and 1 0 , 7 and 14, 8 and 1 3 , 9 and 12, 1 1 and 1 4, 1 3 and 1 6 .

21.

Smith --.r Jones --.r Adams --�_/ Smith --.,--", Jones --� Burton --�_/ Marcus --..,--....., Adams -----,� Burton

23. a) xz b) y c) xz + xz + yz d) xz + xy + y z 25. a) wxz + wxy + wyz + wxyz b) xyz + w yz + wxyz + wxyz + w xyz c) yz + wxy + wxy + w xyz d) wy + yz + xy + wxz + w xz 27. x (y + z)

3 1 . xz + xz 33. We use induction on n. If n = 1, then we are looking at a line segment, labeled 0 at one end and 1 at the other end. The only possible value of k is also 1 , and if the literal is x " then the subcube we have is the O-dimensional subcube consisting of the endpoint labeled 1 , and if the lit­ eral is x" then the subcube we have is the O-dimensional subcube consisting of the endpoint labeled O. Now assume that the statement is true for n ; we must show that it is true for n + 1 . If the literal xn + ) (or its complement) is not part of the product, then · by the inductive hypothesis, the prod­ uct when viewed in the setting of n variables corresponds to an (n - k)-dimensional subcube of the n -dimensional cube, and the Cartesian product of that subcube with the line seg­ ment [0, 1] gives us a subcube one dimension higher in our given (n + 1 )-dimensional cube, namely having dimension (n + 1) - k, as desired. On the other hand, if the literal xn + ) (or its complement) is part of the product, then the product of the remaining k - 1 literals corresponds to a subcube of dimension n - (k - 1) = (n + 1) - k in the n -dimensional cube, and that slice, at either the I -end or the O-end in the last variable, is the desired subcube.

Supplementary Exercises

1 . a) x = 0 , y = 0, z = 0; x = 1, y = 1, z = 1 b) x = 0, y = O, z = O; x = O, y = O, z = l ; x = O, y = l , z = O; x = 1 , Y = 0, z = 1 ; x = 1 , Y = 1 , z = 0; x = 1 , Y = 1 , z = 1 c) No values 3. a) Yes b) No c) No d) Yes 5. 2 2.- 1 7. a) If F (x ) , . . . , xn ) = 1 , then ( F + G )(X) , . . . , xn ) = F (x ) , . . . , xn ) + G (x) , . . . , xn ) = 1 by the dominance law. b) If ( F G )(x ) , . . . , xn ) = 1 , then Hence, F :::: F + G . F (x ) , . . . , xn ) · G (x) , . . . , xn ) = 1 . Hence, F (x) , . . . , xn ) = 1 . It follows that F G :::: F . 9. Because F (x ) , . . . , xn ) = 1 implies that F (x ) , . . . , xn ) = 1 , :::: is reflexive. Suppose that F :::: G and G :::: F . Then F (x) , . . . , xn ) = 1 if and only if G (x) , . . . , xn ) = 1 . This implies that F = G . Hence, :::: is antisymmetric. Suppose that F :::: G :::: H . Then if F (x ) , . . . , xn ) = 1 , it follows that G (x) , . . . , xn ) = 1 , which implies that H (x) , . . . , xn ) = 1 . Hence, F :::: H , so that 1 l . a) x = l , y = 0, z = 0 b) x = l , < is transltlve. y = 0, z = 0 c) x = 1 , y = 0, z = 0

Answers to Odd-Numbered Exercises

S- 77

1 3.

x 1 1 0 0

y 1 0 1 0

x0y 1 0 0 1

x EB y 0 1 1 0

(x EB y) 1 0 0 1

D------.

1 5. Yes, as a truth table shows 1 9. x ---+......

1 7. a) 6 b) 5 c) 5 d) 6

y -'-�"'I

\---.

Sum = x EB y

Carry = xy

2 1 . X3 + X2X , 23. Suppose it were with weights a and b. Then there would be a real number T such that xa + yb � T for ( 1 ,0) and (0, 1 ) , but with xa + yb < T for (0,0) and ( 1 , 1 ) . Hence, a � T, b � T, 0 < T, and a + b < T . Thus, a and b are positive, which implies that a + b > a � T, a contradic­ tion. CHAPTER 12 Section 12.1 I. a) sentence => noun phrase intransitive verb phrase => article adjective noun intransitive verb phrase => article adjective noun intransitive verb => . . . (after 3 steps) . . . => the happy hare runs. b) sentence => noun phrase intransitive verb phrase => article adjective noun intransitive verb phrase => article adjective noun intransitive verb adverb . . . (after 4 steps) . . . => the sleepy tortoise runs

quickly

c) sentence => noun phrase transitive verb phrase noun phrase => article noun transitive verb phrase noun phrase => article noun transitive verb noun phrase => article noun transitive verb article noun => . . . (after 4 steps) . . . => the tortoise passes the hare d) sentence => noun phrase transitive verb phrase noun phrase => article adjective noun transitive verb phrase noun phrase => article adjective noun transitive verb noun phrase => article adjective noun transitive verb article adjective noun => . . . (after 6 steps). . . => the sleepy hare passes the happy tortoise

S-77

3. The only way to get a noun, such as tortoise, at the end is to have a noun phrase at the end, which can be achieved only via the production sentence � noun phrase transitive verb phrase noun phrase. However, transitive verb phrase � transitive verb � passes, and this sentence does not contain passes.

.

5. a) S => I A => l OB => l O l A => I O I OB => 1 0 1 0 1 b) Because of the productions in this grammar, every 1 must be followed by a 0 unless it occurs at the end of the string. c) All strings consisting of a 0 or a 1 followed by one or more repetitions of 0 1 7 . S => OS I => OOS l 1 => OOOS I 1 1 => 000 1 1 1 9. a) S => OS => OOS => OOS 1 => OOS 1 1 => OOS I I I => OOS 1 1 1 1 => 00 1 1 1 1 b) S => OS => OOS => OO I A => OO I I A => OO I I I A => 00 1 1 1 1 1 1 . S => OSAB => OOSA B A B => OOAB A B => 00 AABB => OO I AB B => 00 1 1 B B => 00 1 1 2B => 00 1 1 22 13. a) S � 0, S � 1 , S � 1 1 b) S � I S, S � A c) S � OA I , A � l A , A � OA, A � A d) S � OA, A � I I A , A � A 1 5. a) S � OOS, S � A b) S � lOA, A � OOA , A � A c) S � AAS , S � B B S , AB � B A , B A � A B , S � A , A � 0 , B � 1 d) S � OOOOOOOOOOA , A � OA , A � A e) S � AS, S � ABS, S � A , AB � B A , B A � A B , A � 0, B � 1 t) S � A B S , S � A , AB � B A , B A � A B , A � 0, B � 1 g) S � AB S , S � T, S � U, T � AT, T � A , U � BU, U � B , 1 7. a) S � OS, AB � B A , B A � AB , A � O, B � I S� A b) S � AO, A � l A, A � A c) S � OOOS, S � A 1 9. a) Type 2, not type 3 b) Type 3 c) Type 0, not type 1 d) Type 2, not type 3 e) Type 2, not type 3 t) Type 0, not type 1 g) Type 3 h) Type 0, not type 1 i) Type 2, not type 3 j) Type 2, not type 3 2 1 . Let S, and

S2 be the start symbols of G , and G2 , respectively. Let S be a new start symbol. a) Add S and productions S � S, and S � S2 . b) Add S and production S � S, S2 . c) Add S and production S � A and S � S, S.

23. a)

the

happy

hare

runs

Answers

8-78

to Odd-Numbered Exercises

S- 78

sentence

sentence

�I � l� 2� / � I / � I I I I I I I I I I

b)

c)

/�

article

adJective

noun

verb

the

sleepy

tortoise

runs

noun phrase

adverb article

quickly

the

transitive verb phrase

noun phrase

noun

transitive verb

article

noun

tortoise

passes

the

hare

sentence

d)

�I � /I� I /I� I I I I I I I noun phrase

transitive verb phrase

noun phrase

article

adJective

noun

transitive verb

article

adjective

noun

the

sleepy

hare

passes

the

happy

tortoise

b) No

c) Yes

25. a) Yes 27.

d) No

29. a) S .... (sign) (integer)

/� I /� I /� I I I signed integer

sign

integer

digit

integer

digit

integer

o

digit

9

b)

S .... (sign) (integer) . (positive integer) (sign) .... + (sign) .... (integer) .... (digit) (integer) .... (integer) (digit) (digit) .... i, i = 1 , 2, 3 , 4, 5, 6, 7, 8 , 9, 0 (positive integer) .... (integer) (nonzero digit) (positive integer) .... (nonzero digit) (integer) (positive integer) .... (integer) (nonzero digit) (integer) (positive integer) .... (nonzero digit) (nonzero digit) .... i, i = 1 , 2, 3 , 4, 5 , 6, 7, 8 , 9 (signed decimal number) : : = (sign) (integer) 1 (sign) (integer) . (positive integer) (sign) : : = + 1 (integer) : : = (digit) 1 (integer) (digit) (digit) : : = 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 (nonzero digit) : : = 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 (positive integer) : : = (integer) (nonzero digit) 1 (nonzero digit) (integer) 1 (integer) (nonzero integer) (integer) 1 (nonzero digit)

Answers to Odd-Numbered Exercises

S- 79

c)

/7� �·rF i /\ .

-

integer

I I

digit

nonzero digit

digit

I

I

1

4

3

3 1 . a) (identifier) : : = (Icletter) 1 (identifier) (lcletter) (Icletter) : : = a l b 1 c 1 1z b) (identifier) : : = (Icletter) (Icletter) (Icletter) 1 (Icletter) (Icletter) (Icletter) (Icletter) 1 (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) 1 (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) : : = a l b 1 c 1 1z c) (identifier) : : = (ucletter) 1 (ucletter) (letter) 1 (ucletter) (letter) (letter) 1 (ucletter) (letter) (letter) (letter) 1 (ucletter) (letter) (letter) (letter) (letter) 1 (ucletter) (letter) (letter) (letter) (letter) (letter) (letter) : : = (Icletter) 1 (ucletter) (Icletter) : : = a l b 1 c 1 . . . 1 z (ucletter) : : = A l B 1 C 1 . . . 1 z d) (identifier) : : = (lcletter) (digitorus) (alphanumeric) (alphanumeric) (alphanumeric) 1 (lcletter) (digitorus) (alphanumeric) (alphanumeric) (alphanumeric) (alphanumeric) (digitorus) : : = (digit) 1 (alphanumeric) : : = (letter) 1 (digit) (letter) : : = (lcletter) 1 (ucletter) (lcletter) : : = a l b 1 c 1 . . . 1 z (ucletter) : : = A l B 1 C 1 . . . 1 Z (digit) : : = 0 1 1 1 2 1 19 33. (identifier) : : = (letterorus) 1 (identifier) (symbol) (letterorus) : : = (letter) 1 (symbol) : : = (Ietterorus) 1 (digit) (letter) : : = (lcletter) 1 (ucletter) (lcletter) : : = a l b 1 c 1 . . . 1 z (ucletter) : : = A l B 1 C 1 . . . 1 Z (digit) : : = 0 1 1 1 2 1 1 9 35. numeral : : = sign? nonzerodigit digit* decimal? 1 sign? 0 decimal? sign : : = + 1 nonzerodigit : : = 1 1 2 1 19 digit : : = 0 1 nonzerodigit decimal : : = . digit* 37. identifier : : = letterorus symbol* letterorus : : = letter 1 symbol : : = letterorus 1 digit letter : : = Icletter 1 ucletter lcletter : : = a l b 1 c 1 . . . 1 z 1 Z ucletter : : = A l B 1 C 1 digit : : = 0 1 1 1 2 1 . . . 1 9 • • •

• • •

_

• • •

_

• • •

• • •

• • •

S-79

S-80

Answers to Odd-Numbered Exercises

39. a) (expression)

(term) (term) (addOperator) (jactor) (jactor) (jactor) (muIOperator) (addOperator) (identifier) (identifier) (identifier) (muIOperator) (addOperator) abc*+

b) Not generated c) (expression) (term) (jactor) (jactor) (muIOperator) (expression) (jactor) (muIOperator) (term) (term) (addOperator) (jactor) (muiOperator) (jactor) (jactor) (addOperator) (jactor) (muiOperator) (identifier) (identifier) (addOperator) (identifier) (muiOperator) xy-z* d) (expression) (term) (jactor) (jactor) (muIOperator) (jactor) (expression) (muIOperator) (jactor) (term) (muIOperator) (jactor) (jactor) (jactor) (muiOperator) (muiOperator) (jactor) (jactor) (expression) (muIOperator) (muiOperator) (jactor) (jactor) (term) (term) (addOperator) (muIOperator) (muIOperator) (jactor) (jactor) (jactor) (jactor) (addOperator) (muIOperator) (muIOperator) (identifier) (identifier) (identifier) (identifier) (addOperator) (muIOperator) (muIOperator) wxyz - */ e) (expression) (term) (jactor) (jactor) (muiOperator) (jactor) (expression) (muIOperator) (jactor) (term) (term) (addOperator) (muiOperator) (jactor) (jactor) (jactor) (addOperator) (muIOperator) (identifier) (identifier) (identifier) (addOperator) (muiOperator) ade-* 41. a) Not generated b) (expression) (term) (addOperator) (term) (jactor) (muiOperator) (jactor) (addOperator) (jactor) (muIOperator) (jactor) (identifier) (muIOperator) (identifier) (addOperator) (identifier) (muIOperator) (identifier) a/b + c/d c) (expression) (term) (jactor) (muIOperator) (jactor) (jactor) (muIOperator) ( (expression) (jactor) (muIOperator) ( (term) (addOperator) (term) (jactor) (muiOperator) ( (jactor) (addOperator) (jactor) (identifier) (muIOperator) ( (identifier) (addOperator) (identifier) m * (n + p)

S-80

Answers to Odd-Nwnbered Exercises

S-8 J

S-81

d) Not generated e) (expression)

(term) (factor) (muLOperator) (factor) ( (expression) ) (muIOperator) ( (expression) ((term) (addOperator) (term) ) (muIOperator) ( (term) (addOperator) (term) «factor) (addOperator) (factor) ) (mulOperator) ( (factor) (addOperator) (factor) ( (identifier) (addOperator) (identifier) ) (muLOperator) ( (identifier) (addOperator) (identifier) (m + n) * (p - q)

Section 12.2

1. a)

b)

c)

1 ,0

1, 0

Start

Start

Start

3. a) 0 1 0 1 0

b) 0 1 000

c) 1 1 0 1 1

5. a) 1 1 00

b) 00 1 1 0 1 1 0

7.

25, 0

XE

{'if', �, !I}

'if' � !I

cola root beer = ginger ale =

=

c) 1 1 1 1 1 1 1 1 1 1 1 25, 0

25, 5

25, 1 0

8-82

Answers to Odd-Numbered Exercises

S-82 1 1.

v = Valid ID i = Invalid ID p = Valid password q = Invalid password

a

= "Enter user ID" b = "Enter password" c = Prompt X = Any input

(5, 10, 25 ) , open

13.

25, open

15.

Let So be the start state and let SI be the state representing

a successful call. From So , inputs of 2,

3, 4, 5, 6, 7 ,

or 8 send

the machine back to So with output of an error message for the

user. From So an input of 0 sends the machine to state S I , with

the output being that the

0

is sent to the network. From So an

of2 sends the machine to state S5 with no output; and this .path continues in a similar

manner

to the 9 1 1 path, looking next

for 1 , then 2, then any seven digits, at which point the machine goes to state SI with the output being that the ten-digit input is sent to the network. Any "incorrect" input while in states Ss

input of 9 sends the machine to state S2 with no output; from

or S6 (that is, anything except a 1 while in S5 or a 2 while in S6 )

put; from there an input of 1 sends the machine to state SI with

for the user. Similarly, from S4 an input of8 followed by appro­

there an input of 1 sends the machine to state S3 with no out­

sends the machine back to So with output of an error message

the output being that the 9 1 1 is sent to the network. All other

priate successors drives us eventually to S I , but inappropriate

inputs while in states S2 or S3 send the machine back to So with

outputs drive us back to So with an error message. Also, inputs

output of an error message for the user. From So an input of 1

while in state S4 other than 2 or 8 send the machine back to

sends the machine to state S4 with no output; from S4 an input

state So with output of an error message for the user.

S-83

Answers to Odd-Numbered Exercises

1, 0

1 7.

Start

1, 0 R, I

19.

21.

23. a) l l l l l

f

Input

State

0

1

So

Sl

S2

Sl

Sl

So

S2

Sl

S2

1000000 c) 1000 1 1 001 1 00 b)

g

1 1 0

25.

s-83

S-84 Answers to Odd-Numbered Exercises S ection 12.3

1 . a) {OOO, 00 1 , I l OO, I l O I } c) {OO, O I I , 1 1 0, I I I I }

b) {OOO, OO I l , O I O, O I I l } d) {OOOOOO, 00000 1 , 000 1 00, 000 1 0 1 , 0 1 0000, 0 1 000 1 , 0 1 0 1 00, 0 l 0 1 O I } 3. A = { I , I O I } , B = {0, 1 l , 000}; A = { I O, I l I , I O I O, 1 000, I O I I l , 1 0 1 000} , B = {A}; A = {A , IO} , B = { I O, 1 1 1 , 1 000} or A = {A} , B = { I O, I l l , 1 0 1 0, 1 000, 1 0 1 1 1 , I O I OOO} 5. a) ·The set of strings consisting of zero or more consecutive bit pairs 1 0 b) The set of strings consisting of all 1 s such that the number of I s is divisible by 3, including the null string c) The set of strings in which every 1 is immediately pre­ ceded by a 0 d) The set of strings that begin and end with a 1 and have at least two 1 s between every pair of Os 7. A string is in A* if and only if it is a concatena­ tion of an arbitrary number of strings in A . Because each string in A is also in B , it follows that a string in A * is also a concatenation of strings in B . Hence, A * 5:; B * . � � fu �� �� � fu Q fu � fu b) No c) Yes d) No 13. a) Yes b) Yes I I . a) Yes c) No d) No e) No Q No 1 5. We use structural in­ duction on the input string y. The basis step considers y = A, and for the inductive step we write y = w a , where w E [ * and a E I . For the basis step, we have xy = x , so we must show that f(s, x) = f(f(s, x ) , A). But part (i) of the definition of the extended transition function says that this is true. We then assume the inductive hypothesis that the equation holds for w and prove that f(s, xwa) = f(f(s, x) , wa). By part (ii) of the definition, the left-hand side of this equation equals f(f(s, xw) , a). By the inductive hypothesis, f(s, xw) = f(f(s, x ) , w ), so f(f(s, xw ) , a) = f(f(f(s, x) , w ) , a). The right-hand side of our desired equality is, by part (ii) of the definition, also equal to f(f(f(s, x) , w ) , a), as desired. 19. {om l n I m � O and n � I } 1 7. {O, 1 0 , I l } {O, I } * 2 1 . {A} U {O } { I } * {O} U { I O, I l } {O, I } * U {O} { I } * { O I } {O, I } * U {O} { I } * {OO} {O} * { I } {O, I } * 23. Let S2 be the only final state, and put transitions from S2 to itself on either input. Put a tran­ sition from the start state So to s, on input 0, and a transition from s, to S2 on input 1 . Create state S3, and have the other transitions from So and s) (as well as both transitions from S3) lead t o S3 . 25. Start state So, only final state S3 ; transitions from So to So on 0, from So to s, on 1 , from s, to S2 on 0, from s, to s) on 1 , from S2 to 'so on 0, from S2 to S3 on 1 , from S3 to S3 on 0, from S3 to S3 on 1 27. Have five states, with only S3 final. For i = 0, 1 , 2, 3, transition from Si to itself on input 1 and to Si + ' on input O. Both transitions from S4 are to itself. 29. Have four states, with only S3 final. For i = 0, 1 , 2, transition from Si to Si + ) on input 1 but back to So on input O. Both transitions from S3 are to itself.

S-84

31. Start

0, 1

33. Start state So, only final state s) ; transitions from So to So on 1 , from So to

35.

s)

on 0, from s) to

s)

on 1 ; from s, to So on 0

Start -----.,�

37. Suppose that such a machine exists, with start state So

and other state s) . Because the empty string is not in the language but some strings are accepted, we must have s, as the only final state, with at least one transition from So to s) . Because the string 0 is not in the language, the tran­ sition from So on input 0 must be to itself, so the transi­ tion from So on input 1 must be to s, . But this contradicts the fact that 1 is not in the language. 39. Change each fi­ nal state to a nonfinal state and vice versa. 4 1 . Same ma­ chine as in Exercise 25, but with So, s" and S2 as the final states 43. {O, O I , I l } 45. {A,O} U {om I n I m � 1 , n � I } 47. { I on I n � O } U { I on 1 0m I n , m � O } 49. The union of the set of all strings that start with a 0 and the set of all strings that have no Os

S-85

Answers to Odd-Numbered Exercises

53. Add a nonfinal state S3 with transitions to S3 from So on input 0, from Sl on input 1 , and from S3 on input 0 or 1 .

0, 1

S-85

equivalence class to contain just one state). Therefore this sequence of refinements must remain unchanged from some point onward. It remains to show that as soon as we have Rn = Rn + l , then Rn = Rm for all m > n , from which it fol­ lows that R n = R , and so the equivalence classes for these * two relations will be the same. By induction, it suffices to show that if Rn = Rn + t . then Rn + 1 = R n+2 . Suppose that Rn+1 =1= Rn +2 • This means that there are states s and t that are (n + I )-equivalent but not (n + 2)-equivalent. Thus there is a string x of length n + 2 such that, say, f(s , x) is final but f(t , x) is nonfinal. Write x = a w , where a E I . Then f(s , a) and f(t , a) are not (n + I )-equivalent, because w drives the first to a final state and the second to a nonfinal state. But f(s , a) and f(t , a) are n-equivalent, because s and t are (n + I )-equivalent. This contradicts the fact that Rn = Rn + l . 61 . a) By the way the machine M was constructed, a string will drive M from the start state to a final state if and only if that string drives M from the start state to a final state. b) For a proof of this theorem, see a source such as Introduc­ tion to Automata Theory, Languages, and Computation (2nd Edition) by John E. Hopcroft, Raj eev Motwani, and Jeffrey D. Ullman (Addison-Wesley, 2000).

c) Section 12.4

0, 1

57. Suppose that M is a finite-state automaton that accepts the set of bit strings containing an equal number of Os and I s. Suppose M has n states. Consider the string on + I I n + I . By the pigeonhole principle, as M processes this string, it must encounter the same state more than once as it reads the first n + l Os; so let S be a state it hits at least twice. Then k Os in the input takes M from state s back to itself for some positive integer k. But then M ends up exactly at the same place after l l k reading on + + 1 n + 1 as it will after reading on +I I n + . There­ n n+ n+ I I + fore, because M accepts o I it also accepts o k+ I I n + I , which is a contradiction. 59. We know from Exercise 58d that the equivalence classes of Rk are a refinement of the equivalence classes of Rk - I for each positive integer k. The equivalence classes are finite sets, and finite sets cannot be refined indefinitely (the most refined they can be is for each

1. a) Any number of Is followed by a 0 b) Any number of Is followed by one or more Os c) I I I or 00 1 d) A string of any number of 1 s or OOs or some of each in a row e) A or a string that ends with a 1 and has one or more Os before each I t) A string of length at least 3 that ends with 00 3. a) No � � � � � � � � t) � � � � � 5. a) 0 U 1 1 U 010 b) 000000* c) (0 U 1)« 0 U 1)(0 U 1»)* d) 0* 10* e) (1 U 01 U 001)* 7. a) 00* 1 b) (0 U 1) (0 U 1)(0 U 1)*0000* c) 0* 1* U 1*0* d) 1 1( 1 1 1)* (00)* 9. a) Have the start state so, nonfinal, with no transitions. b) Have the start state so, final, with no trahsitions. c) Have the nonfinal start state So and a final state Sl and the transi­ tion from So to S l on input a . 1 1 . Use an inductive proof. If the regular expression for A is 0, A, or x, the result is triv­ ial. Otherwise, suppose the regular expression for A is BC. Then A = B C where B is the set generated by B and C is the set generated by C. By the inductive hypothesis there are regular expressions B' and C' that generate B R and C R , respectively. Because A R = (BC) R = C R B R , C'B' is a reg­ ular expression for A R . If the regular expression for A is B U C, then the regular expression for A is B' U C' because (B U C) R = (B R ) U (C R ). Finally, if the regular expression for A is B* , then it is easy to see that (B')* is a regular expres­ sion for A R .

8-86

13. a)

c)

S-86

Answers to Odd-Numbered Exercises o

o

o

S-8 7

1 5. S � OA , S � I B , S � 0 , A � OB , A � I B , B � 1 7. S � Oe , s � I A , S � I , A � l A, OB , B � I B A � Oe , A � I , B � O B , B � I B , B � O , B � I , e � oe , e � I B , e � I . 1 9. This follows because input that leads to a final state in the automaton corresponds uniquely to a derivation in the grammar. 2 1 . The "only if" part is clear because I is finite. For the "if" part let the states be Sio ' Si p Si2 , . . . , Si. , where n = lex ). Because n ::: l S I , some state is repeated by the pigeonhole principle. Let y be the part of x that causes the loop, so that x = uyv and y sends Sj to k Sj , for some j. Then uy v E L ( M ) for all k. Hence, L ( M ) is infinite. 23. Suppose that L = {02n I n } were regular. Let S be the set of states of a finite-state machine recognizing this set. Let z = 02n I n where 3n ::: I S I . Then by the pump­ ing lemma, z = 02n I n = uvw , l (v ) ::: I , and uv i w E {02n l n I n ::: O} . Obviously V cannot contain both 0 and I , because v 2 would then contain 10. So v is all Os or all I s, and hence, uv 2 w contains too many Os or too many I s, so it is not in L . This contradiction shows that L is not regular. 25. Suppose that the set of palindromes over to , I } were regular. Let S be the set of states of a finite-state machine recognizing this set. Let z = on I On , where n > l S I . Apply the pumping lemma to get uv i w E L for all nonnegative integers j where lev ) ::: I , and l ( uv) ::::: l S I , and z = on I On = uvw. Then v must be a string of Os (because In l > l S I ), so uv 2 w is not a palindrome. Hence, the set of palindromes is not regular. 27. Let z = I ; then I I I ¢. L but 1 0 1 E L , so I I and 1 0 are distinguishable. For the second question, the only way for Iz to be in L is for z to end with 0 1 , and that is also the only way for l Iz to be in L , so 1 and 1 1 are indistinguishable. 29. This follows immediately from Exercise 28, because the n distinguishable strings must drive the machine from the start state to n different states. 3 1 . Any two distinct strings of the same length are distinguishable with respect to the language P of all palin­ dromes, because if x and y are distinct strings of length n , then xx R E P but yx R ¢. P . Because there are 2n different strings of length n , Exercise 29 tells us that any deterministic finite-state automaton for recognizing palindromes must have at least 2n states. Because n is arbitrary, this is impossible.

Section 12.5

1. a) The nonblank portion of the tape contains the string 1 1 1 1 when the machine halts. b) The nonblank portion of the tape contains the string 0 1 1 when the machine halts. c) The nonblank portion of the tape contains the string 0000 1 when the machine halts. d) The nonblank portion ofthe tape 3. a) The contains the string 00 when the machine halts. machine halts (and accepts) at the blank following the input, having changed the tape from 1 1 to 0 1 . b) The machine changes every other occurrence of a I , if any, starting with the first, to a 0, and otherwise leaves the string unchanged; it halts (and accepts) when it comes to the end of the string. 5. a) Halts with 0 1 on the tape, and does not accept b) The

Answers to Odd-Numbered Exercises

8-87

first 1 (if any) is changed to a 0 and the others are left alone. The input is not accepted. 7. (so , 0 , S\ , 1 , R), (so , 1 , so , 1 , R) 9. (so , 0 , so , 0 , R), (so , I , 1 1. (so , O,s\ , 0 , R), S\ , 1 , R), (S\ , O,s \ , 0 , R) , (s\ , l ,s\ , O , R) (so , 1 , so , 0 , R), (s\ , 0 , S\ , 0 , R), (s\ , l ,so , O, R ), (s\ , B , S2 , 13. (so , 0 , so , 0 , R), (so , 1 , S\ , 1 , R), (s \ , 0 , S\ , 0 , R), B , R) (s\ , 1 , so , I , R), (so , B , S2 , B , R) 1 5. If the input string is blank or starts with a 1 the machine halts in nonfinal state so . Otherwise, the initial 0 is changed to an M and the machine skips past all the intervening Os and I s until it either comes to the end of the input string or else comes to an M . At this point, it backs up one square and enters state S2 . Because the accept­ able strings must have a I at the right for each 0 at the left, there must be a I here ifthe string is acceptable. Therefore, the only transition out of S2 occurs when this square contains a 1 . If it does, the machine replaces it with an M and makes its way back to the left; if it does not, the machine halts in nonfinal state S2 . On its way back, it stays in S3 as long as it sees I s, then stays in S4 as long as it sees Os. Eventually either it encouters a 1 while in S4 at which point it halts without accepting or else it reaches the rightmost M that had been written over a 0 at the start of the string. If it is in S3 when this happens, then there are no more Os in the string, so it had better be the case that there are no more 1 s either; this is accomplished by the transitions (S3 , M , ss , M , R) and (ss , M , S6 , M , R), and S6 is a final state. Otherwise the machine halts in nonfinal state ss . If it is in S4 when this M is encountered, things start all over again, except now the string will have had its leftmost remain­ ing 0 and its rightmost remaining 1 replaced by M s. So the machine moves, staying in state S4, to the leftmost remaining o and goes back into state So to repeat the process.

17. (so , B , S9 , B , L ), (so , 0 , S \ , 0 , L ), (s\ , B , S2 , E , R), (S2 , M , S2 , M , R), (S2 , 0 , S3 , M , R), (S3 , 0 , S3 , 0 , R), (S3 , M , S3 , M , R ), (S3 , I , S4 , M , R), (S4 , 1 , S4 , 1 . R ), (S4 , M , S4 , M , R), (S4 , 2, SS , M , R), (ss , 2 , ss , 2 . R), (ss , B , S6 , B , L ), (S6 , M , Sg , M , L ), (S6 , 2 , S7 , 2 , L ), (S7 , 0 , S7 , 0 , L ), (S7 , I , S7 , I , L ), (S7 , 2 , S7 , 2, L ), (S7 , M , S7 , M , L ), (S7 , E, S2 , E, R), (sg , M , Sg , M , L ), (Sg , E, S9 , E, L ) where M and E are markers, with E marking the left end of the input 19. (so , 1 , S\ , B , R), (s\ , I , S2 , B , R), (S2 , I , S3 , B , R), (s3 , I , s4 , 1 , R), (s\ , B , S4 , I , R), (s2 , B , s4 , l , R), (S3 , B , S4 , I , R) 2 1 . (so , 1 , S\ , B , R), (s\ , I , S2 , B , R), (s\ , B , S6 , B , R), (S2 , I . S3 , B , R), (S2 , B , S6 , B , R), (S3 . I , S4 , B , R), (S3 , B , S6 , B . R), (S4 , I , SS , B , R), (S4 , B , S6 , B , R), (S6 , B , SI O , 1 , R), (ss , 1 , ss , B , R ), (ss , B . S7 . 1 , R), (S7 , B , Sg , 1 , R), (Sg , B , S9 , 1 , R), (S9 , B . SIO , I , R) 23. (so , l , so , I , R), (so , B , s\ , B , L), (s\ , l , s2 , 0 , L ), (S2 ' 0 , S2 , 0 , L ), (S2 , 1 , S3 , 0 , R), (S2 , B , S6 , B , R), (S3 , O , S3 , 0 , R), (S3 , I , S3 , I , R), (S3 , B , S4 , I , R), (S4 , B , SS , I . L ), (ss , 1 . ss , I , L ), (ss . 0 , S2 , 0 , L ), (S6 , 0 , S6 , I , R), (S6 , I , S7 , 1 , R ), (S6 , B , S7 , B , R) 25. (so , 0 , so . 0 , R), (so . *, ss , B , R), (S3 , * , S3 , * , L), (S3 , 0 , S3 , 0 , L ), (S3 , 1 , S3 , 1 , L ), (S3 , B , so , B , R),

S-88

Answers to Odd-Numbered Exercises

(S5 , 1 , S5 , B , R), (S5 , 0 , S5 , B , R), (S5 , B , S6 , B , L), (S6 , B , s6 , B , L), (S6 , 0 , S7 , 1 , L ), (S7 , 0 , S7 , 1 , L), (so , 1 , Sh 0 , R), (Sh 1 , Sh 1 , R), (S\ , * , S2 , * , R), (S2 , 0 , S2 , 0 , R), (S2 , 1 , S3 , 0 , L), (S2 , B , S4 , B , L), (S4 ' 0 , S4 , 1 , L), (S4 ' * , S8 , B , L), (S8 , 0 , S8 , B , L), (s8 , I , s8 , B , L ) 27. Suppose that Sm is the only halt state for the Turing ma': chine in Exercise 22, where m is the largest state number, and

suppose that we have designed that machine so that when the machine halts the tape head is reading the leftmost 1 of the answer. Renumber each state in the machine for Exercise 1 8 by adding m to each subscript, and take the union of the two sets of five-tuples. 29. a) No b) Yes c) Yes d) Yes 3 1 . (so , B , S \ , I , L ), (so , I , Sh l , R), (Sh B , SO , I , R)

S-88

0

1 5.

Supplementary Exercises

1. a) S --+ 00S 1 1 1 , S --+ >.. b) S --+ A A B S , A B --+ B A , c) S --+ E T , T --+ B A --+ A B , A --+ 0 , B --+ I , S --+ >.. O T A , T --+ I T B , T --+ >.. , O A --+ AO, I A --+ A I , OB --+ BO, I B --+ B I , E A --+ EO, E B --+ E I , E --+ >..

5

3.

1 1 /1\ I /\ A

B

(

A

19.

)

B

(

5.

)

/5\ /5\ 5 I

o

0

o

/5\ 5\ 5/ I

0

0

o

9. No, take A = 7. No, take A = { I , 1 0 } and B = {O, OO} . {OO, 000 , OOOOO} and B = {OO, OOO}. 1 1 . a) I b) 1 c) 2 d) 3 e) 2 t) 4

1, 1

13.

0, 0

0, 0

0, 0

0, 1

0

0

0

0

Answers to Odd-Numbered Exercises

S-89

21.

S-89

a)

b)

c)

23.

Construct the deterministic finite automaton for A with states S and final states F . For if use the same automaton but with final states S - F .

b)

0, I 27. Suppose that L = { I P i p is prime} is regular, and let S be the set of states in a finite-state automaton recognizing L . Let z = I P where p is a prime with p > l S I (such a prime exists because there are infinitely many primes). By the pumping lemma it must be possible to write z = uvw with l (uv ) � l S I , l (v ) ::: 1, and for all nonnegative inte­ gers i , uv i W E L . Because z is a string of all I s, u = I a , V = I b , and w = I C , where a + b + c = p, a + b � n , and b > 1 . This means that uv i w = l a l bi lc = l (a + b+c) +b(i -l ) = F+b(i -l ) . Now take i = P + 1 . Then uv i w = F( l+b) . Be­ cause p(1 + b) is not prime, uv i w rf. L, which is a contradic­ tion. 29. (so , * , ss , B , L ), (so . o. so . o. R), (so . 1 . Sl , 0 , R), (Sl . * . S2 . *. R), (Sl . I , S" I , R), (S2 , O . S2 , 0 , R), (S2 . 1 . S3 , o. L), (S2 , B . S4 , B , L), (S3 , *, S3 , * , L), (S3 . O . S3 , o. L), (S3 . 1 . S3 . 1 . L), (S3 . B . so . B . R), (S4 , * , SS , B , L), (S4 , O . S4 . B . L), (ss . 0 , ss , B , L), (ss . B , S6 , B , R), (S6 , 0 , S7 , I , R), (S6 , B , S6 , B , R), (S7 , 0 , S7 , I , R), (s7 , I ,s7 , I , R), (ss , 0 , ss , I , L ), (ss , I , ss , I , L )

APPENDIXES Appendix l 1 . Suppose that I ' is also a multiplicative identity for the real numbers. Then, by definition, we have both I . I' = I and 1 . I ' = I ' , so I ' = 1 . 3. For the first part, it suffices to show that [(-x) · y] + (x · y) = 0, because Theorem 2 guarantees

S-90

Answers to Odd-Numbered Exercises

that additive inverses are unique. Thus [(-x) . y] + (x · y) = ( -x + x) . y (by the distributive law) = 0 . y (by the inverse law) = y . 0 (by the commutative law) = 0 (by Theorem 5). The second part is almost identical. 5. It suffices to show that [(-x) · (-y)] + [-(x · y)] = 0, because Theorem 2 guar­ antees that additive inverses are unique: [(-x) · (-y)] + [-(x · y)] = [(-x) · (-y)] + [(-x) · y] (by Exercise 3) = (-x) · [(-y) + y] (by the distributive law) = (-x) · 0 (by the inverse law) = 0 (by Theorem 5) . 7. By definition, -(-x) is the additive inverse of -x. But -x is the additive in­ verse of x , so x is the additive inverse of -x. Therefore -(-x) = x by Theorem 2. 9. It suffices to show that (-x - y) + (x + y) = 0, because Theorem 2 guarantees that additive inverses are unique: (-x - y) + (x + y) = [(-x) + (-y)] + (x + y) (by definition of subtraction) = [(-y) + (-x)] + (x + y) (by the commutative law) = (-y) + [(-x) + (x + y)] (by the associative law) = (-y) + [(-x + x) + y] (by the associative law) = (-y) + (O + y) (by the inverse law) = ( -y) + Y (by the identity law) = 0 (by the inverse law). 1 1 . By definition of division and uniqueness of multiplicative inverses (Theorem 4) it suf­ fices to prove that [(w /x) + (y/z)] · (x · z) = w · z + x · y. But this follows after several steps, using the distributive law, the associative and commutative laws for multiplication, and the definition that division is the same as multiplication by the inverse. 13. We must show that if x > 0 and y > 0, then x . y > O. By the multiplicative compatibility law, the commutative law, and Theorem 5, we have x . y > 0 . y = O. 15. First note that ifz < O, then -z > O (add -z to both sides of the hypothesis). Now given x > y and -z > 0, we have x . (- z) > y . ( -z) by the multiplicative compatibility law. But by Exercise 3 this is equivalent to -(x · z) > -(y . z). Then add x . z and y . z to both sides and apply the various laws in the obvious ways to yield x . z < y . z. 1 7. The ad­ ditive compatibility law tells us that w + y < x + y and (to­ gether with the commutative law) that x + y < x + z. By the transitivity law, this gives the desired conclusion. 1 9. By Theorem 8, applied to l /x in place of x, there is an inte­ ger n (necessarily positive, because l /x is positive) such that n > l /x . By the multiplicative compatibility law, this means that n . x > 1 . 2 1 . We must show that if (w , x) '" (w' , x') and (y, z) '" (y' , Zl), then (w + y , x + z) '" (w' + y', x' + Zl) and that (w · y + x · Z, X · Y + w · z) '" (W' · y' + X' · Z' , X' · y' + w ' · Zl). Thus we are given that w + x' = x + w' and that y + Z' = z + y', and we want to show that w + y + X' + Z' = X + z + w' + y' and that w . y + x . z + x' . y' + w' . Zl = X Y + w . z + w' . y' + x' . z'. Forthe first ofthe desired con­ clusions, add the two given equations. For the second, rewrite the given equations as w - x = w' - x' and y - z = y' - Zl, multiply them, and do the algebra. •

S-90

Appendix 2

1. a) 23

b) 2 6

c) 24

3. a) 2y

b) 2y /3

c) y /2

5.

10 9 8

7

6 5

4 3

2

3

y = OY 2

o

2

2

3

4

5

Appendix 3

1 . After the first block is executed, a has been assigned the original value of b and b has been assigned the original value of c, whereas after the second block is executed, b is assigned the original value of c and a the original value of c as well. 3. The following while construction does the same thing. i := initial value � final value

while i begin

statement

i := i + 1

end

Photo Credits CHAPTER 1

CHAPTER 2 CHAPTER 3

CHAPTER 4 CHAPTER S

Page 2 : © National Library of Medicine; p. 5: Library of Congress; p. 1 4 : Courtesy Indiana University Archives; p. 1 6 : University Archives. Department of Rare Books and Special Collections. Princeton University Library; p. 25: By pennission of the London Mathematical Society; p. 27: © Mary Evans Picture Library; p. 28: Henry Maurice Sheffer ( 1 883-1 964) by Mabel Lisle Ducasse. Courtesy ofthe Harvard University Portrait Collection, Department of Philosophy, CNA l . Photo by Katya Kallsen; p. 3 2 : © BettmannlCorbis Images; p. 44: © Granger Collection; p. 93 : © Master and Fellows of Trinity College, Cambridge; p. 94: © Master and Fellows of Trinity College Cambridge. Page 1 13 : Library of Congress; p. 1 14 : Library of Congress; p. 1 1 5 : By pennission of the President and Council of the Royal Society; p. 1 1 7 : © Scientists and Inventors Portrait File Collection, Archives Center, National Museum of American History; p. 1 54 : Courtesy Neil Sloane and AT&T Shannon Lab. Page 168: ©

from THE SCIENTISTS OF THE ANCIENT WORLD, by Anderson/Stephenson. pg. 90. Illustra­ tion by Marie Ie Glatin Keis; p. 1 82 : Bachmann, Paul. Die Arithmetik Der Quadratischen Fonnen. Verlag und Druck von BG Teubner. Leipzig. Berlin. 1 923; p. 1 83 : Smith Collection, Rare Book and Manuscript Library, Columbia University; p. 1 84 : © Stanford University News Service; p. 204 : Smithsonian Institution. Photo No. 46, 834-M; p. 2 1 3 : © Mary Evans Picture Library; p. 228: Scientists and Inventors Portrait File Collection, Archives Center, National Museum of American History; p. 239: From A History ofScience, Technology and Philosophy in the 1 6th and 1 7th Centuries, by Abraham Wolf. Reprinted by pennission of Harper Collins Publishers Lts. © Abraham Wolf; p. 242, top : Courtesy Ronald L. Rivest; p. 242, middle: © Kristen Tsolis; p. 242, bottom: © Courtesy Leonard Adleman. Page 298 : ©

p. 332 : ©

Page 348: ©

Page 394 :

7

Page 508:

CHAPTER S CHAPTER 9

CHAPTER 10

CHAPTER 11 CHAPTER 12

APPENDIX:

Granger Collection; p. 352: Courtesy Mrs. Jane Burch;

p. 324:

p. 366: ©

Courtesy Tony Hoare;

National Library of Medicine.

From A Concise History ofMathematics by Dirk Struik, 1 967, Dover Publications; p. 407: © Granger Collection; p. 420: Courtesy Stephen Stigler; p. 438: © Smith Collection, Rare Book and Manuscript Library, Columbia University.

CHAPTER 6 CHAPTER

Granger Collection; p. 299: © Mary Evans Picture Library; Matthew Naythons/TimePixlGetty.

From Math Tutor Archive.

Page 551 : Courtesy Stephen Warshall; and Council of the Royal Society.

p. 571 : ©

Deutsches Museum;

p. 584:

By pennission of the President

Page 635: ©

Scientists and Inventors Portrait File Collection, Archives Center, National Museum of American History; p. 640: Master and Fellows of Trinity College Cambridge; p. 646: Julius Petersen: Metoder og Teorier. Edition no. 6, Det Schoenbergske Forlag, Copenhagen 1 9 1 2. Photo courtesy Bjarne Toft; p. 650: Courtesy, Edsger W. Dijkstra; p. 663 : Courtesy, the Archive of Polish Mathematicians, at the Institute of Mathematics of the Polish Academy of Sciences. Reproduced with pennission of the Institute; p. 668: By pennission of the London Mathematical Society. Page 688 : © Master and Fellows of Trinity College Cambridge; p. 702 : Courtesy, California State University, Santa Cruz; p. 72 1 : Courtesy, the Archive of Polish Mathematicians, at the Institute of Mathematics of the Polish Academy of Sciences. Reproduced with pennission of the Institute; p. 738 : Courtesy Robert Clay Prim; p. 739: Courtesy, Joseph B. Kruskal. Photo by Rachel Kruskal.

Courtesy MIT Museum; p. 768 : Courtesy Maurice Karnaugh; p. 775: Courtesy Edward J. McCluskey; Courtesy the Quine Family.

Page 750: p. 776:

Page 791 : © Donna CoveneylMIT; p. 792, top: Courtesy IBM, Almaden Research Center; p. 792, bottom: Courtesy Peter Naur; p. 805: From: I Have a Photographic Memory by Paul Halmos ( 1 987), American Mathe­ matical Society; p. 8 1 1 : © BettmannlCorbis; p. 826: © Science Photo LibrarylPhoto Researchers, Inc. p. 836 : © University Archives. Department of Rare Books and Special Collections. Princeton University Library.

A4 Appendix 1 : ©

Hulton-Deutsch Collection/Corbis.

Index of Biograp hies Ada, Augusta (Countess of Lovelace), Adleman, Leonard, Archimedes, A-4 Aristotle,

27

Kempe, Alfred Bray,

668

al-Khowarizmi, Abu Ja' far Mohamme d

242

1 68 805 Knuth, Donald E., 1 84 Kruskal, Joseph Bernard, 739 Kuratowski, Kazimierz, 663 ibn Musa,

2

Kleene, Stephen Cole,

Bachmann, Paul Gustav Heinrich,

1 82

792 Bayes, Thomas, 420 Bernoulli, James, 407 Backus, John,

Landau, Edmund,

Boole, George, S Cantor, Georg,

1 13 240

Carroll, Lewis (Charles Dodgson),

44

688

Chebyshev, Pafnuty Lvovich, Chomsky, Avram Noam, Church, Alonzo,

394

Laplace, Pierre Simon, Lukasiewicz, Jan,

Carmichael, Robert Daniel, Cayley, Arthur,

299 1 83

Lame, Gabriel,

43 8

79 1

Naur, Peter,

366

Peirce, Charles Sanders,

32

Petersen, Julius Peter Christian,

650 348

Dijkstra, Edsger Wybe, Dirichlet, G. Lejeune,

775

213

792

Pascal, Blaise,

25

1 17

Descartes, Rene,

332

McCluskey, Edward J. ,

Mersenne, Marin,

836

De Morgan, Augustus,

McCarthy, John,

72 1

Prim, Robert Clay,

Dodgson, Charles (Lewis Carroll),

44

Quine, Willard van Orman, Ramanuj an, Srinivasa,

Ramsey, Frank Plwnpton,

Euclid,

Euler, Leonhard,

Russell,

2 1 4, 635

Fermat, Pierre de,

Shamir, Adi,

239

242

Sheffer, Henry Maurice,

1 54

Gauss, Karl Friedrich,

Sloane, Neil,

Goldbach, Christian,

Smullyan, Raymond,

204 215

Stirling, James, Hamilton, William Rowan, Hardy, Godfrey Harold,

14

145

640

93

Tukey, John Wilder,

571

16

Turing, Alan Mathison,

Hoare, C . Anthony R . ,

1 76, 826

324

Hopper, Grace Brewster Murray,

81 1

Vandermonde, Alexandre-Tbeophile,

368

1 15

Huffman, David A.,

702

Venn, John,

Karnaugh, Maurice,

768

Warshall, Stephen,

1/- /

750 28

Shannon, Claude Elwood,

298

Hasse, Helmut,

352

242 Bertrand, 1 1 4

Rivest, Ronald,

228

Fibonacci,

776

94

Eratosthenes, Erdos, Paul,

508 584

646

73 8

55 1 1-1

Index Absorption laws for Boolean algebra, 753, 754 for lattices, 585 for propositions, 24 for sets, 1 24 Abstract definition of Boolean algebra, 755 Academic papers, 593 Academy, Plato's, 2 Acceptance, deferred, 1 79 Accepted language, 806 Accepted strings, 806 Ackermann, Wilhelm, 3 1 0 Ackermann's function, 3 1 0 Acquaintanceship graph, 592-593 paths in, 623 Actors, 593 Ada, Augusta (Countess of Lovelace), 25, 27 Adapting existing proofs, 96 Adders, 764-765 full, 764, 7 8 1 half, 764, 78 1 Addition of functions, 1 3 5- 1 36 of geometric progressions, 270--27 1 of integers, 222-224 of matrices, 247-248 of multi sets, 1 3 2 of subsets, 732 Addition rule of inference, 66 Additive Compatibility Law, A-2 Address system, universal, 7 1 1 Adjacency list, 6 1 1 Adjacency matrices, 6 1 2--6 1 4, 676 counting paths between vertices by, 628--629 Adjacent cells, 768 Adjacent vertices, 598, 600, 675 Adjective, 786 Adleman, Leonard, 24 1 , 242 Adverb, 786 Affine transformation, 208 Airline system, weighted graphs modeling, 648 Alcuin of York, 632 Aleph null, 1 5 8 Alexander the Great, 2 Algebra, Boolean, 5, 28 abstract definition of, 755 definition of, 781 identities of, 752-754, 755 ALGOL, 792 ALGOL 60, 792 Algorithm(s), 1 67- 1 77 approximation, 655 for base b expansion, 2 1 9, 22 1 for binary search tree, 695-707 for Boolean product of zero-one matrices, 252-254 bubble sort, 1 73 change-making, 1 74- 1 76 for coloring graphs, 67 1 , 674 complexity of, 1 93-1 99 average-case, 1 95, 430--433 of binary search algorithms, 1 94- 1 95 of breadth-first search algorithms, 73 1 of bubble sort, 1 94- 1 95 computational, 1 93 ofDijkstra's algorithm, 653 constant, 1 97 of depth-first search algorithms, 729 exponential, 1 97

1-2

factorial, 1 97 of finding maximum element of set, 1 94 of insertion sort, 1 96 linear, 1 97 of linear search algorithms, 1 94 logarithmic, 1 97 of merge sort, 479 polynomial, 1 97 of searching algorithms, 1 94- 1 95 , 1 97 of sorting algorithms, 1 95-1 96 space, 1 93 time, 1 93-1 96 worst-case, 1 94, 1 95-1 96 computer time used by, 1 98-1 99 for computing div and mod, 225-226 deferred acceptance, 1 79, 293 definition of, 1 68 Dijkstra's, 649--65 3 , 676 divide-and-conquer, 474, 5 1 4 division, 202-203, 257 estimating number of operations of, 1 80-- 1 82 Euclidean, 227-229, 258, 298 extended, 232, 246 for Euler circuits, 636--637 for Euler paths, 63 7--638 for evaluating polynomials, 1 99 for fast integer multiplication, 475-476 for fast matrix multiplication, 475 for Fibonacci numbers, 3 1 6--3 1 7 for finding maximum element in sequence, 1 69 for finding minimum element in sequence, 259 Fleury's, 637, 646 Floyd's, 656--6 5 7 for generating combinations, 384-3 85 for generating permutations, 3 82-384 greed� 1 74- 1 76, 275-276, 703, 738, 744 history of word, 1 68 Huffinan coding, 702-703 inorder traversal, 7 1 8 insertion sort, 1 74 for integer addition, 222-224 for integer multiplication, 224-226 for integer operations, 222-226 integers and, 2 1 9-229 iterative, for Fibonacci numbers, 3 1 7 Kruskal's, 739-740, 744 for matrix multiplication, 249-250 merge sort, 3 1 7-32 1 for minimum spanning trees, 738-74 1 for modular exponentiation, 226 Monte Carlo, 4 1 1 -4 1 3 NAUTY, 6 1 8 optimal, 200 parallel, 606 postorder traversal, 7 1 8 preorder traversal, 7 1 6 Prim�, 738-739, 744 probabilistic, 393, 4 1 1 -4 1 3 , 442 properties of, 1 69 recursive, 3 1 1 -32 1 , 329 binary search, 3 1 4 for computing aU , 3 1 2 for computing greatest common divisor, 3 1 3 correctness of, 3 1 5-3 1 7 for factorials, 1 97 for Fibonacci numbers, 3 1 6 linear search, 3 1 4 modular exponentiation, 3 1 3

searching, 1 70, 257 binary, 1 7 1 , 257, 474 Boolean, 1 3 breadth-first, 729-73 1 , 744 complexity of, 1 94- 1 95, 1 97 depth-first, 726--7 29, 744 applications with, 727-729 in directed graphs, 732-734 linear, 1 70, 256, 257 average-case complexity of, 43 1-432 complexity of, 1 94, 1 97 recursive sequential, 3 1 4 ternary, 1 78 Web page, 1 3 serial, 606 shortest-path, 649--653 Sollin's, 743 sorting, 1 72-1 74 complexity of, 1 95-196 topological, 576--5 78, 582 for spanning trees, 738-74 1 topological sorting, 577 for transitive closure, 550 traversal, 7 1 2-7 1 9 Warshall's, 550--5 53, 582 Alice in Wonderland (Carroll), 44 Alpha-beta pruning, 707, 748 Alphabet, 787, 838 Alphanumeric character, 340 Ambiguous grammar, 840 Analytic Engine, 27 Ancestors of vertex, 686, 743 AND, 1 3 , 1 5- 1 6 AND gate, 76 1 , 7 8 1 Antecedent, 6 Antichain, 585 Antisymmetric relation, 523-524, 5 8 1 representing using digraphs, 542 using matrices, 538-539 Appel, Kenneth, 668, 669 Application( s) of backtracking, 73 1 -732 of Bayes' Theorem, 4 1 9-42 1 of congruences, 205-207 of graph theory, 592-595, 604-607, 623--625, 627, 638, 643 , 67 1--672 of graphs colorings, 67 1 --672 of inclusion-exclusion, 505-5 1 2 o f number theory, 23 1-244 of pigeonhole principle, 3 5 1 -353 of trees, 695-707 Approximation algorithm, 655 Archimedean property, A-5 Archimedes, A-4 Archimedes' screw, A-4 Argument, 44, 63 Cantor diagonalization, 1 60 form, 64 valid, 64 Aristotle, 2 Arithmetic computer, with large integers, 237-238 Fundamental Theorem of, 2 1 1 , 257, 285-286 matrix, 247-248 modular, 203-205 Arithmetic mean, 95, 282 Arithmetic progression, 1 5 1 Arithmeticorum Libri Duo (Maurolico), 265

/2- /

Index

12-2

Array linear, 606, 607 two-dimensional, 606 Arrow, Peirce, 29 Ars Conjectandi (Bernoulli), 407 Art Gallery problem, 675 Theorem, 675

Art of Computer Programming, The (Knuth), 1 72 Article, 786 Articulation points, 625 Artificial intelligence, 332 fuzzy logic in, 1 9 fuzzy sets in, 1 32- 1 3 3 Assertion final, 323, 329 initial, 323, 329 Assignment ofjobs, 5 1 3 Assignment, stable, 1 79, 293 optimal, 293 Assignment statements, A- I O - A-I I Assignments, frequency, 672 Associated homogenous recurrence relations, 467 Associative laws for addition, A- I for Boolean algebra, 753, 755 for lattices, 585 for multiplication, A-I for propositions, 24 for sets, 1 24 Asymmetric relation, 528 Asymptotic functions, 1 46, 1 92 Asynchronous transfer mode (ATM), 1 44 ATM cells, 1 44 AT&T network, 625 Automated theorem proving, 1 08 Automaton finite-state, 805-8 1 1 deterministic, 807-8 1 1 , 839 nondeterministic, 8 1 1 -8 14, 820-82 1 , 822, 839 regular sets recognized by, 8 1 9-82 1 set not recognized by, 824-825 linear bounded, 825 pushdown, 825 quotient, 8 1 7 Average-case complexity o f algorithms, 1 95, 430-433 Avian influenza, 424 AVL-trees, 748 Axiom(s), 75, A- I - A-5 Completeness, A-2 field, A- I - A-2 of Mathematical Induction, A-5 order, A-2 in proving basic facts, A-2 - A-5 for real numbers, A-I - A-2 for set of positive integers, A-5 Babbage, Charles, 27 Bachmann, Paul Gustav Heinrich, 1 82 Back edges, 728 Backtracking, 726-729, 744 applications of, 73 1 -732 in directed graphs, 733 Backus, John, 792 Backus-Naur form (BNF), 792-793, 839 Backward differences, 460 Backward reasoning, 94 Bacon, �in, 623-624 Bacon number, 623 Bacteria, 457 Balanced rooted trees, 69 1 , 743 Balanced strings of parentheses, 332 Balanced ternary expansion, 230 Bandwidth of graph, 680 Bar chocolate, 292 Ziegler's Giant, 1 84

Barber paradox, 1 9-20 Base b Miller's test for, 245 strong pseudoprime to, 245 Base b expansion, 2 1 9, 22 1 Base conversion, 220 BASIC, 340 Basis step, 264, 284, 299, 328, 690, 706 Basis, vertex, 632 Basketball, 382 Bayes, Thornas, 4 1 8, 420 Bayes' Theorem, 4 1 7-425 generalized, 420 Bayesian spam filters, 42 1-423 Beaver, busy, 835 Begging the question, 84 Begin statements, A-I I , A-1 4 Bell, E . T., 566 Bell numbers, 566 Bernoulli family tree, 684 Bernoulli, James, 406, 407 Bernoulli trial, 428, 442 and binomial distribution, 406-408 mutually independent, 378 Bernoulli's inequality, 280 Biconditional, 9-1 0 logical equivalences for, 25 Bicycle racers, 424 Bidirectional bubble sort, 1 73 Big-O notation, 1 80- 1 86 Big-Omega notation, 1 82, 1 89-1 90 Big-Theta notation, 1 82, 1 89- 1 90 Bigit, I 6 Bijective function, 1 3 8, 1 63 Binary coded decimal, 23 1 Binary coded decimal expansion, 774 Binary digit origin of term, 1 5 Binary expansion, 2 1 9 Binary insertion sort, 1 72, 1 74, 1 79 Binary relation, 5 1 9, 5 8 1 Binary search algorithm, 1 7 1 , 257, 474 complexity of, 1 94- 1 95, 1 97 recursive, 3 1 4 Binary search trees, 695-698, 743 Binary trees, 302-304, 686, 687 definition of, 743 extended, 302 full, 304-305 height of, 306 number of vertices of, 306 with prefix code, 700-70 I Binding variables, 3 8-39 Binit, 1 6 Binomial coefficients, 357, 363-368, 387 extended, 486 Binomial distribution, 406-408 Binomial expression, 363 Binomial Theorem, 363-365, 387 extended, 486 Binomial trees, 745 Bipartite graphs, 602-604, 675 coloring of, 603 Bird fiu, 424 Birthday problem, 409-4 1 1 Bit, 1 04 origin of term, 1 5 , 1 6 Bit operations, 1 5 , 1 04 Bit strings, 1 5, 1 04, 1 29- 1 3 0 with a n odd number of I s and ending with at least two Os, 809 counting, 336 without consecutive Os, 454-455 decoding, 700-70 I generating next largest, 385 in interstellar communication, 424 length of, 1 5 , 454-455 Bitwise operators, 1 5- 1 6, 1 04 Blocks of statements, A- I I

1-3

BNF (Backus-Naur form), 792-793, 839 Boat, 632 Bonferroni's inequality, 4 1 5 Book number o f graph, 682 Boole, George, 3 , 5, 420, 749 Boolean algebra, 5, 28, 752-755 abstract definition of, 755 definition of, 7 8 1 identities of, 752-754, 755 Boolean expressions, 750-752, 7 8 1 dnal of, 754, 7 8 1 Boolean functions, 749-755, 7 8 1 dual of, 754, 78 1 functionally complete set of operators for, 759 implicant of, 770 minimization of, 766-779, 78 1 representing, 757-759 self-dual .. 782 threshold, 783 Boolean power (of zero-one matrices), 252-254 Boolean product, 749-750, 75 1 , 757-759, 7 8 1 of zem-one matrices, 252-254, 2 5 7 Boolean searches, 1 3 Boolean sum, 749-750, 75 1 , 757-759, 7 8 1 Boolean variable, 1 5, 1 04, 750, 758, 7 8 1 Boole 's inequality, 4 1 5 Boruvka, C»akar, 740 Bots, 734 Bottom-up parsing, 79 1 Bound greatest lower, 582 of poset, 574 least upper, 582, A-2 of po set, 574 lower of lattice, 586 of poset, 574, 582 upper, A-2 of lattice, 586 of poset, 5 74, 582 Bound variable, 3 8-39 Bounded lattices, 586 Boxes, distributing objects into, 376-3 79 distinguishable, 376 indistinguishable, 376 Breadth-first search, 729-73 1 , 744 Bridge, in graphs, 625 Bridge problem, Konigsberg, 633-634, 636, 638 B-tree of degree k, 745 Bubble sort, 1 73 , 257 bidirectional, 1 73 , 259 variant of, 1 73 , 2 1 9 worst-case complexity of, 1 94- 1 95 Bug, computer, 8 1 1 Buoyancy, principle of, A-4 Busy beaver function, 835 Butane, 688 Bytes, 220 C programming language, 346, 559 Cabbage, 632 Cactus, 746 CAD programs, 772 Caesar, Julius, 207 Caesar cipher, 207-208 Calculus predicate, 3 1 propositional, 2 Call graphs, 594 connected components of, 625 Canterbury Puzzles, The (Dudeney), 454 Cantor diagonalization argument, 1 60 Cantor digits, 386 Cantor expansion, 23 1 , 386 Cantor, Georg, I l l , 1 1 3 , 1 60 Card hands, 3 5 8-3 59, 376-3 77 Cardinality aleph null, 1 58 of set, 1 1 3 , 1 1 6, 1 5 8- 1 60

1-4

12-3

Index

Carmichael, Robert Daniel, 240 Carmichael numbers, 240-24 1 , 257 Carroll, Lewis (Charles Dodgson), 44-45, 50 Carry, 222 Cartesian products, 1 1 7-1 1 8 Cases, proofs by, 86-90, 1 05 Cash box, 372-373 Catalan numbers, 456 Caterpillar, 746 Cayley, Arthur, 683, 688 Ceiling function, 1 43-145, 1 63 Celebrity problem, 282 Cells, adjacent, 768 Center, 695 Chain, 585 Chain letter, 691 Change-making algorithm, 1 74-1 76 Characteristic equation, 46 1 Characteristic function, 148 Characteristic roots, 46 1 Chebyshev, Pafuuty Lvovich, 438 Chebyshev's inequality, 438-439, 442 Checkerboard, tiling of, 97- 1 02, 277, 283 Cheese, in Reve's puzzle, 454 Chen, J. R., 2 1 5 Chess, game tree for, 707 Chessboard knight's tour on, 647 n-queens problem on, 73 1 -732 squares controlled by queen on, 679 Chi (Greek letter), 667 Chickens, 680 Child of vertex, 686, 743 Chinese meal, preparing, 585 Chinese postman problem, 638, 682 Chinese Remainder Theorem, 235-237, 258 Chocolate bar, 292 Chomp, 9 1 , 292, 586 Chomsky, Avram Noam, 785, 789, 790 Chomsky classification of grammars, 789-790 Chromatic number, 668, 676 edge, 674 of graph, 667 k-critical, 674 Chung-Wu Ho, 290 Church, Alonzo, 833 Church-Turing thesis, 833 Cipher affine, 208 Caesar, 207-208 RSA, 24 1 -244 shift, 208 Circuits combinational, 76 1 -763 depth of, 766 examples of, 763-764 minimization of, 766-779 in directed graph, 546, 582 Euler, 633-63 8, 676 in graph, 622 Hamilton, 638-643, 676 multiple output, 764 simple, 622 Circular reasoning, 84, 1 05 Circular relation, 584 Civil War, 32 Class congruence, 204 equivalence, 558-559, 582 definition of, 558 and partitions, 559-562 representative of, 558 NP, 1 97 P, 1 97 Class A addresses, 34 1 Class B addresses, 34 1 Class C addresses, 34 1 Class D addresses, 3 4 1

Class E addresses, 3 4 1 Clauses, 68 Clique, 678 Closed walk, 622 Closest-Pair Problem, 479-482 Closure, Kieene, 805, 8 1 7 Closure laws for addition, A-I for multiplication, A-I Closures of relations, 544-553, 582 reflexive, 544, 582 symmetric, 545, 582 transitive, 544, 547-550, 582 computing, 550-553 Coast Survey, U. S., 32 COBOL, 792, 8 1 1 Codes, Gray, 642-643 Codes, prefix, 700-703, 743 Codeword enumeration, 455-456 Coding, Huffman, 70 1 -703 , 744 Codomain of function, 1 34, 1 49, 1 63 Coefficient( s) binomial, 357, 363-368, 387 extended, 486 constant linear homogenous recurrent relations with, 460, 46 1 -467, 5 1 4 linear nonhomogenous recurrent relations �th, 467-47 1 , 5 1 4 multinomial, 382 Coins, change using fewest, 1 75-1 76 Collaboration graphs, 593-594 paths in, 623 Collatz problem, 1 0 1 - 1 02 Collision, 206 in hashing functions, probability of, 4 1 0-4 1 1 Coloring of bipartitl' graphs, 603 chromatic number in, 667 of graphs, 666-672, 676 of maps, 666-667 Column of matrix, 247 Combinations, 357-360 of evenm, 396-398, 403-404 generating, 3 84-385 linear, 232 with repetition, 3 7 1 -375 Combinational circuits, 76 1 -763 depth of, 766 examples of, 763-764 minimization of, 766-779 Combinatorial identities, 364-368 Combinatorial proof, 359, 386 Combinatorics, 335, 352, 378, 386 Comments in pseudocode, A- I I - A-1 2 Common difference, 1 5 1 Common divisor, greatest, 2 1 5-2 1 7 Common errors in exhaustive proofs, 90 in proof by cases, 90 Common multiple, least, 2 1 6, 2 1 7 Common ratio, 1 50 Commutative laws for addition, A-I for Boolean algebra, 753, 755 for lattices, 585 for multiplication, A-I for propositions, 26 for sem, 1 24 Comparable elements in poset, 567, 582 Compatible total ordering, 576, 582 Compatibility laws, A-2 Compilers, 559, 8 1 0 Complement, 749, 7 8 1 o f Boolean function, 75 1 double, law of, in Boolean algebra, 753, 755, 757 of fuzzy set, 1 3 2 one's, 230

of set, 123, 1 63 two's, 230-23 1 Complement laws, for sets, 1 24 Complementary event, 396 Complementary graph, 6 1 1 Complementary relation, 528 Complementation law, for sets, 1 24 Complemented lattice, 586, 755 Complete bipartite graph, 604, 675 Complete graphs, 60 1 , 675 Complete induction, 284 Complete m-ary tree, 694 Complete m-partite graph, 678 Complexity of algorithms, 1 93-1 99 average-case, 1 09, 430-433 of binary search algorithms, 1 94- 1 95 of breadth-first search algorithms, 73 1 of bubble sort, 1 94-1 95 computational, 1 93 ofDijkstra's algorithm, 653 constant, 1 97 of depth-first search algorithms, 729 exponential, 1 97 factorial, 1 97 of finding maximum element of set, 1 94 of insertion sort, 1 96 linear, 1 97 of linear search algorithms, 1 94 logarithmic, 1 97 of merge sort, 479 polynomial, 1 97 of searching algorithms, 1 94- 1 95, 1 97 of sorting algorithms, 1 95-1 96 space, 1 93 time, 1 93-1 96 worst-case, 1 94, 1 95- 1 96 Complexity of merge sort, 479 Components of graphs, connected, 625-626 strongly, 627, 676 Composite integers, 1 89-1 90, 2 1 0, 2 1 1 , 2 1 7, 257 Composite key, 532, 582 Composite of relations, 526-527, 5 8 1 Composition rule, 324 Compositions of functions, 1 3 9- 1 4 1 , 1 63 Compound interest, 45 1 Compound propositions, 3 , 1 0-1 1 , 2 1 , 22, 1 04 consistent, 1 04 disjunctive normal form of, 29 dual of, 29 equivalences of, in Boolean algebra, 750 satisfiable, 30 well-formed formula for, 3 0 1 Computable function, 1 63, 835 Computation, models of, 785-837 Computational complexity of algorithms, 1 93 ofDijkstra's algorithm, 653 Turing machine in making precise, 833 Computational geometry, 288-290, 479-48 1 Computer arithmetic, with large integers, 237-238 Computer debugging, 8 1 1 Computer file systems, 689 Computer network, 590 with diagnostic lines, 5 9 1 interconnection networks, 606-607 local area networks, 605-606 multicasting over, 726 with mUltiple lines, 590 with multiple one-way lines, 592 �th one-way lines, 591 Computer programming, 792 Computer programming languages, 68, 1 3 5 Computer representation, of sets, 1 29-1 3 0 Computer science, 792 Computer time used by algorithms, 1 98-1 99 Computer virus transmission, 389 Computer-aided design (CAD) programs, 772 Concatenation, 300, 804, 839

Index

12-4

Conclusion, 6, 44, 64 fallacy of affirming, 69 Concurrent processing, 595 Conditional constructions, A-1 2 - A-1 3 Conditional probability, 400, 404-405, 442 Conditional statement, 6 for program correctness, 324-326 Conditions don � care. 774-775 initial, 450, 5 1 3 Congruence class, 204 Congruence modulo m. 203-204, 556, 558, 582 inverse of, 234 Congruences, 205-207 linear, 234-235, 257 Conjecture, 75, 1 05 3x + I , \ 0 1- \ 02 and counterexamples, 96-97 Frame's, 460 Goldbach's, 2 1 4-2 1 5 and proof, 96-97 twin prime, 2 1 5 Co�unction, 4-5, 1 04, 781 distributive law of disjunction over, 23 Conjunction rule of inference, 66 Conjunctive normal form, 758 Connected components of graphs, 625-626 strongly, 627, 676 Connected graphs, 62 1-629 directed, 626-627 planar simple, 659-663 strongly, 626 undirected, 624-626 weakly, 626 Connecting vertices, 598 Connectives, 4 Connectivity relation, 547, 582 Consecutive composite integers, 2 1 7 Consequence, 6 Consistency, of system specifications, 1 2 Consistent compound propositions, 104 Constant coefficients linear homogenous recurrent relations with, 460, 46 1-467, 5 1 3 linear nonhomogenous recurrent relations with, 467-47 1 , 5 1 4 Constant complexity of algorithms, 1 97 Construction of the real numbers, A-5 Constructions conditional, A-1 2 - A-1 3 loop, A-1 3 - A-14 Constructive existence proof, 91, 1 05 Contain, 1 1 2 Context-free grammars , 789, 839 Context-free language, 789 Context-sensitive grammars, 789 Contingency, 2 1 , 1 04 Contradiction, 2 1 , 80 proofs by, 80-83 Contrapositive, of implication, 8 Converse of directed graph, 6 1 1 of implication, 8, 1 04 Convex polygon, 288 Cookie, 373 Corollary, 75, 105 Correctness of programs, 322-328, 329 conditional statements for, 324-326 loop invariants for, 326-327 partial, 323 program verification for, 323 rules of inference for, 323-324 of recursive algorithms, 3 1 5-3 1 6 Correspondences, one-to-one, 1 36, 1 60 Countability, of rational numbers, 1 58, 1 63, 2 1 8 Countable set, 1 5 8 Counterexamples, 3 4 , 8 3 conjecture and, 96-97

Counting, 335-3 9 1 , 449-5 1 8 basic principles of, 335-340 bit strings, 336 without consecutive Os, 454-455 Boolean functions, 75 1 combinations, 357-360 derangements, 5 1 0, 5 1 4 by distributing objects into boxes, 376-379 functions, 336-337 generating functions for, 488-493 Internet addresses, 341 one-to-one functions, 337 onto functions, 509-5 1 0, 5 1 4 passwords, 340 paths between vertices, 628-629 permutations, 355-357 pigeonhole principle and, 347-353 reflexive relations, 524 relations, 522 with repetition, 370-3 7 1 subsets o f finite set, 3 3 8 telephone numbers, 3 3 7 tree diagrams for, 343-344 variable names, 340 Countrapositive, of implication, 1 04 Course in Pure Mathematics, A (Hardy), 93 Covariance, 442 Covering relation, 579 Covers, 579 CPM (Critical Path Method), 587 Crawlers, 734 Cricket, 93 Critical Path Method (CPM), 587 Crossing number, 666 Cryptography, 207-208 private key, 24 1 public key, 23 1 , 24 1 -244 Cunningham numbers, 2 1 4 Cut edge, 625 Cut set of graph, 746 Cut vertices, 625 Cycle in directed graph, 546, 582 with n vertices, 60 I Cylinder, 769 Czekanowski, Jan, 740 Data compression, 701 Database composite key of, 532 intension of, 53 1 primary key of, 53 1 records in, 53 1 relational model of, 5 3 1 -532, 5 8 1 Database query language, 535-536 Datagrams, 346-347 Datalogy, 792 Datatype, 1 1 3 de Bruijn sequences, 682 de Mere, Chevalier, 400 De Morgan, Auguatus, 22, 25, 668, 669 De Morgan's laws for Boolean algebra, 753, 757 for propositions, 22, 25-26 proving by mathematical induction, 274-275 for quantifiers, 40-4 1 for sets, 1 24-1 26 Debugging, 8 1 1 Decimal, binary coded, 23 1 Decimal notation, 1 68, 2 1 9 Decision problems, 834 Decision trees, 698-700, 743 Decreasing function, 1 3 7 Decryption, 208, 23 1 , 257 RSA, 243 Deductive reasoning, 264 Deep-Blue, 707, 748 Deferred acceptance algorithm, 1 79, 293

1-5

Definition domain of, 1 49 recursive, 263, 294-308 Degree of linear homogenous recurrence relations, 461 of membership, 1 32 of n-ary relations, 530 of region, 661 of vertex in undirected graph, 598 Degree-constrained spanning tree, 746 Delay machine, 799-800 Dense graph, 6 1 4 poset, 5 8 1 Dependency notation, 784 Depth of combinatorial circuit, 766 Depth-first search, 726-729, 744 applications with, 732-734 in directed graphs, 732-734 Derangements, 5 1 0-5 1 2, 5 1 4 number of, 5 1 0, 5 1 4 Derivable form, 787 Derivation, 787 Derivation trees, 790-792, 839 Descartes, Rene, 1 1 7 Descendants of vertex, 686, 743 Detachment, law of, 65 Deterministic finite-state automata, . 807-8 1 1 , 839 Deviation, standard, 436 Devil's pair, 62 1 Diagnostic test results, 4 1 9-420 Diagonal matrix, 255 Diagonal of a polygon, 288 Diagonal relation, 545 Diagonalization argument, 1 60 Diagrams Hasse, 5 7 1 -572, 574, 575, 582 state, for finite-state machine, 798 tree, 343-344, 386 Venn, 1 1 4, 1 1 5, 1 22, 1 23, 1 63 Diameter of graph, 680 Dice, 394 Dictionary ordering, 382 Die, 4 1 6, 436-437 dodecahedral, 443 octahedral, 443 Difference, A-6 backward, 460 common, 1 5 1 forward, 5 1 6 o f multisets, 1 32 of sets, 1 23, 1 63 symmetric, 1 3 1 , 1 63 Difference equation, 460 Digits binary origin of term, 1 5 , 1 6 Cantor, 386 Digraphs, 582, 591, 675 circuit (cycle) in, 546, 582 connectedness in, 626-627 converse of, 6 1 1 depth-first search in, 732-734 edges of, 59 1 , 600-60 1 Euler circuit of, 634 loops in, 5