mathematical structures for computer science

820

Click here to load reader

Upload: shankugeetu

Post on 21-Oct-2015

3.055 views

Category:

Documents


122 download

TRANSCRIPT

Page 1: Mathematical Structures for Computer Science
Page 2: Mathematical Structures for Computer Science

, 7 ` {Mathematical Structures for Computer Science\ K V V .. { ( .

Page 3: Mathematical Structures for Computer Science

MATH EMATICAL STRUCTURES IFOR COMPUTER SCIENCEA Modern Approach to Discrete Mathematicski SIXTH EDITIONA Judith L. GerstingUniversity of Hawaii at HiloIE W. H. Freeman and Company New Yorkx

Page 4: Mathematical Structures for Computer Science

Now there are 01011 favorite discrete structures:(Adam A Francine), (Jason A Cathryn —> Samantha)Publisher: Craig BleyerAcquisitions Editor: Terri WardExecutive Editor Statistics and Mathematics: Ruth BcruthAssociate Editor: Brendan CadyExecutive Marketing Manager: Sarah MartinSenior Media Editor: Wctoria AndersonCover Designer: Blake beganCover Art: Fine Line IllustrationsText Designer: Lisa DuuglisProject Editor: Penelope HullIllustrations: Fine Line IllustrationsIllustration Coordinator: Bill PageProduction Coordinator: Julia DeRosaComposition: Black Dot GroupPrinting and Binding: Quebecnr World Bock GroupLibrary of Congress Control Number: 2006926756ISBN-10: 0-7167-6864-XISBN-13: 978-0-7l67·684-X@ 2007, 2003, 1999. 1993, 1987, 1982 by W. 1-I. Freeman and Company. All rights reserved.Printed in the United States of AmericaFirst printingW. 1-I. Freeman and Company41 Madison Avenue, New York, NY 100101-{ountlmills. Basingstoke RG21 6XS, England ·www.whfreeman.com

Page 5: Mathematical Structures for Computer Science

Contents in Brief IPreface ..................................... xvNote to the Student ........................... xviiCHAPTER 1 Formal Logic .................................. 1CHAPTER 2 Proofs, Recurslon, and Analysis of Algorithms ....... 88CHAPTER 3 Sets, Combinatorics, Probablllty, andNumber Theory .............................. 186CHAPTER 4 Relations, Functions, and Matrlces ............... 285CHAPTER 5 Graphs and Trees ...........,................ 401CHAPTER 6 Graph Algorithms ........................,... 475CHAPTER 7 Boolean Algebra and Computer Logic ............ 534CHAPTER 8 Modeling Arlthmetlc, Computation, and Languages . . 592APPENDIX A Derlvatlon Rules for Prupositional ,and Predlcate Logic ............... 685APPENDIX B Summation Notation .............. 687APPENDIX C The Logarithm Function ........... 690Answers to Practice Problems ................... 693Answers to Selected Exercises .................. 727Answers to Self-Tests ......................... 789Index .................................,.... 797

Page 6: Mathematical Structures for Computer Science

· J: `V » ;, M ¤,;¤ ‘¤·‘<· ¢, ” i »_,_; — »~ ¤?%* "P*°z%*’°¤V ?~%i‘° 7,-. V»V- . ‘= % ,·,. J ‘<#¤< ,& . my ·~% ~. Jr Fay *‘ —‘‘·’/·- ¢·· — ,, = .—r’ —_»*»»;.n<.,=.=¤;‘.s¤;>,·~ · ·~ »=,. w w . , ~ » _ >»wv.# s ‘— yr- ; 3,,V—`,s. -,»gi_ »_·` ,- *5;; f,;.»_;;€·xgm/ —,»`§ \-; le _ x¥‘g§¢§z_pJ;,Mw..~,L,_ , — ,_,__ I ir /— I gfx » ·2 ··>·¤ ‘ #<—"*2v= 3~ V V , *‘ w ., ·g¢;¥}i; *¤:~1 —· ··—; > `‘ ~~ · `·? 3 ` Www? v ' "Z "`*¥?*#$¤;·1 4 ` . rr? ' ’?’2?*T L—:.·`*· .‘;·= 3 , r ; ‘ L · . \=_·~ ,1; » — v»,· —, vi,»··»e~ wpv erm:. .—..f ,;~ ¢¤»·:;é;’ A ~‘ — * ?%·,~~`s’Z‘ K ’ ’;»r~<·i· I·:‘ . =~·’ .—._ .-* ~ V » ~ k_!_r="§;}a r_ " ,·,,»;2· ”`li¢' ,,¤* gg, C,;¤E>}$"Z e · T . I I I ,.—»», `f »’—’ » I ‘ w V »·§- p JE ` *111; *:· `V T · 4% · ,,4 —§ri*i`€¥:E*£· ‘ * » Myv,·’ ~ ‘,, ~’ ¤="~ ’ " »

,; »-_ , ‘· , . . =z` e · ~_,/4r,~,;i ;;, ;· ’ ‘ J a·‘¤f" .—‘. vi _r,, <~·¤,;» x J ·.‘=‘ > ·¢ ` ’:·4 3; M`— j ·’ 1, · ~= ‘ ,4 _ ,·, ; * ;_ ,: ‘ r - ,:»~ ag v 1·, i `.,~· 3; LV 7·if¤» ’r= v ’ —»*· * ’;`:~ ag; ya ,_»; = A.,··’ #,31 · - **’· ·’~ ··‘* , , V;} »:¤· * ·¢ ‘~¤¥%¢ `‘‘·-· in 4:~ * ,i:* ¢»< ¥’· _ , ,_·m*·¥, ~‘ J , ran, 2·,.,;; cz, *5-.4·;.>;;¤;se '»&·»-¤;%=#>‘;¢¢ - gi tai?-~A¤ _. 4— ,2;` » —» , — »’~»,,, ,_ » · .‘‘»V . V,·‘A ·2= * ;2·¢¢ ;» ·¢+~:»< = »= ’~ »,, ··=§ \, ;. · ` {4 . ,·’· ".‘ e, , fe- n ,, Mi V, L': '&";ig . ‘, · “f* “·~‘ 9 ;¤· xagir *Q~ ` .‘, * ., ‘» ~ ¢¤ · *“ *§. ._ y —·_»··; iv ; . .3 · www ,~ ; ~: · §: ,§;§g w ,— wm 49,#2 BIA v i? , ·· *‘· ‘* Y ’ ’*,, — - ,v?;i¥fi := 'V . ";· ; *` ·&5*' *'**=` >·i· ¤» ‘ T I" — »y · — ‘ ;V — »Q . I `: =¤ _ ~:¤¤ ¤ · — r=» \ ,..~»‘» , . » · " » ~` ¥= we »$;¤;~v;%, v,;;,·—¤_,~ ·L»r E` · ~ » » ~— ··». ; ,. V · · . · —`»· ,»»· gy ~ s A » »gu »¤> ,-,v ·¢ —» >’~, _ _ ,2 — ~·se; ·,r, . ;a #¥·F··¤s· 4 =~»~,y .‘··z>¢’ »i¥»;i 7 ..~1¤v%¢¤ ·~ · ·i ~ ¤ V ¤~.J.ia:,:·:· 4~ 32;»» » J,§§;§w »» ’<·,,,; — :,£§&&~ » J ’ ; , » — ¤.wc: L ‘ 5-Tjlf ‘·r ·· 7/~¤ f ~>E** ` ·.‘» ‘»~*g?;e3q,g,.i,; .a , ,5.4* ~ Z_ ,; _,` / ` , _—V·:·¤#z¤ , ‘’., V y <#s w ;»¢$?%; —»v: #1 ~·*¢1»»r —,—,. —. . ~.r. » V ;. ., sy\~_~—(` . , r,y,V . V -‘ — » . ~»V~., M ¢,—; -» , .4,»k», v-,.,,,·»a>»{; ,»,, /. 4 W ._.=~~·y V ~·» ` 2 4*,»

‘F i ·· ., —»‘» , ,» —’~, , » in `· .1 »`‘—» · v ,·,’` 1 #/**7 ;=€ =· »<§»§

- ’=~· /`—`i’*. in ; — V ’ ’ ’ ~ ,

Page 7: Mathematical Structures for Computer Science

Contents CPreface ................................ xvNote tothe Student ...................... xviiCHAPTER 1 Formal Logic ............................. 11.1 Statements, Symbnllc Representation, and 'Dautologles ............ 2Connectives and Truth Values ..............,..,..,..,....,.. 2Tautologies ..,....4........................,..4..,....... 8Logical Connectives in the Real World ........,.e..e..e...... 10An Algorithm .,.e....... . .............,..,.............. 1 lExercises 1.I ........,.,................,..,l.,....,.... 141.2 Pmposillunal Logic . . .............................. , ........ 21Valid Arguments ......,.........................,....e.. 21Derivation Rules for Propositional Logic .......,..,..,....... 24Deduction Method and Other Rules .............. , ..e..,.... 27Verbal Arguments ....e........................,..,.,.... 29Exerczses 1.2 .........4,r...,.i.. . .................r,.. 311.3 Quantihers, Predicates, and Validity ........................... 35Quantifiers and Predicutes ....,.....................e..,.., 35Translation ...........,....e......................,.,.., 38Validity ........... . .....e........................,....l 41Exenives 1.3 ,..,....... . ......l,,....,.... . .......,.l. 431.4 Predlcatze Lngic .............................. , ............. 49Derivation Rules for Predicate Logic l............ , ........,.. 49Universal lnstantiation .....,........................,. 5lExistential Instantintion .............................,. 5lUniversal Generalization ..........................,... 52Existential Generalization ,....... . .............,..,... 53More Work with Rules ..,.,..,........................,.., 54Verbal Arguments .....,..,.,.....,..................,,., 58Conclusion ...,..................,.......,..,..,..,..... S9Exerrises 1.4 .................,........................, 601.5 Logic Programming ................................ , ....... 63Prolog ................,,.........,.,................... 64Hom Clauses and Resolution ............................... 66

Page 8: Mathematical Structures for Computer Science

CvnfentsRecursion ......,,.,.,,..,.,........................,.4. 69Expert Systems .,..,.,,..,.,..,......................... 71Exencixes 1.5 .,4,.....................................4, 721.6 Proof of Correctness ......................,................. 74Assertions ..............,.......,..... . ..,.,,.,,.,,.,... 75Assignment Rule ...............4..4,.4,.,,.,..,..,....,. 76Conditional Rule ..........,.......4,.4,.,..,.,..4,.4,.,. 79Exercises 1.6 .....,..........,..,.i,.i.......i.,........ 82Clnapter 1 Review ........... . .....,.......,..,..,....... 84On the Computer ..... . . ................................ 87CHAPTER 2 Proofs, Recurslon, and Analysis of Algorithms . . 882.1 Proof Techniques .................... . · ......·.............. B9'1'heorems and Informal Proofs .......................4..... 89To Prove or Not ro Prove .,.............................,.4 90Exhaustive Proof .,..,..,.4,.,..........................4 91Direct Proof ..,..,.4...,.,...............o........,..,.. 92Contraposition ,,.........,.... . .....,.............,..,.o 93Contradiction ....,.,..4..,..,.,.....................o.., 95Serendipity ......,.,.....,..,.,.....................o... 96Common Definitions ,..,..,..,.,....... . ................, 97Exencrvcr 2.1 .,.....................,.......,.....,.o,4. 982.2 Induction ........... . ..... . .............................. 100First Principle of Induction .............. . ..,..,.,,..,.,.. 100Proofs by Mathematical Induction l.....4,.,..,.,,.,,.,..... 102Second Principle of Induction ............,.......,..,..,.. 107Exercises 2.2 ................... . ...,l,.....,l,........ I 12Z3 More on Proof of Correctness ...............r............... 118Loop Rule .,.....,.....,.4,.,,.,...................... 1 18Euclidean Algorithm .......,..,..,...................... 121Exercises 2.3 .....,........,.,......................... 1241.4 Recursive Dellnltlons ....r.....»........··.....··...······- 119Recursively Defined Sequences ............................ 129Recursively Deined Sets ................................. 132Recursively Defined Operations ......,..,..,.,..,..,...... 134Reeursively Defined Algorithms ................l.......... 135Exerczrev 2.4 ............ . ............................ 1402.5 Recurrence Relations .................................... . . 147Linear First·0rder Recurrence Relations ..................... 147Expand, Guess, and Verify ......,,.,.................. 147A Solution Formula ........ , ..,,.,.................. 149Linear Second-Order Recurrence Relations .................. 156Divide·and—Conquer Recurrence Relations ,.,................ 161Exerrisei Z.5 ........ . ........................ . . ..... 164

Page 9: Mathematical Structures for Computer Science

Contents lx2.6 Analysis u1`AlguritInns .,.........................,...... ;. . 169The General Idea ......................,..,,.,.,..,...., 169Analysis Using Recurrence Relations ......,..,..,.,,.,.r.., 172Upper Eound Euclidean Algorithm) ..,..,.,,.,,.,..,.,..,., 174Exencises26 ..,.......,....,.. .... ..............,.. 175Chapter 2 Review ..........,........................... 181On the Computer. ............... , ..................... 183CHAPTER 3 Sets, Combinatorics, Probability. andNumber Theory ......................... 1863.1 Sets ........................ 1 ...................... , ..... 187Notation .....................,..,..,.,..,..,.......... 187Relationships Between sets ....,.,,.,....,..,..,.......... 139Sets of Sets .........,.......,..,.,..,....,............. 191Binary and Unary Operations ..,..,....,.....,....,....... 192Operations on Sets ,,.,,.,.4,.,..,..........,.... . ....... 194Set Identities .... . ....,..,.,.....,.......,............ 196Countable and Uncountable Sets ......................,..,, 199Exerrires .7,l , 1 , . . .......,......................,.,,., 2023.2 Counting ........,............ , ........................... 211Multiplication Principle ........,,..,.,....,.....,....,,,, 212Addition Principle ............,,..,.,..,....r.........., 215Using the Principles Together ....,,.,....,.r,..,.,....,.., 216Decision Trees .....4,...,,.,,..,.,.r..,.,,..,.,,.,,,.,, 218Exerczses 312 1 ,..,....,....,..................... 1 ,..,. 2203.3 Principle of Inclusion and Exclusion; Plgeunlmle Principle ....... 225Principle of Inclusion and Exclusion ...,.,,.,..,..,......... 225Pigeonhole Principle ......,...........,.,..,,....,.1.... 230Exercises 3.3 ....1,..,1.,1,..,..,.,..,.... . .... . ..... 23 I3.4 Permutatiuns and Cumblnatinns .......,...... , .............. 233Permutations ,.... . .....................,..1,.,,.,...., 233Combinations ,.1.. . ..,..........1................,..,.1 235Eliminating Duplicates ....................... , ..,.1..,.. 238Perznutations and Combinations with Repetitions ...,..,.,..,.. 239Ecemisei 3.4 .......,1..1..,,.1..................... . . 2463.5 Probability ......... . ..................1...........,...... 252Introduction to Finite Probability ....1.............,..,.... 252Probability Distributions ..............1.............,.... 254Conditional Probability .............................1,... 256Expected Value ..............1........................1. 257Average Case Analysis of Algorithms ................,..,... 260Exercises 3.5 .......... . ............. . , 1 , .......... 261

Page 10: Mathematical Structures for Computer Science

X Contents3.6 Binnmial Theorem ....... . ....,............................ 266Pascal‘s Triangle ...........,..,.4,.............,....4., 266Binomial Theorem and Its Proof ,.,..4....... . .....,..,.... 267Applying the Binomial Theorem ,.4,..,...... . ......,.,.... 269Exercises 3,6 , 4 ,.............,.... 4 ,.,............ 4 ..4, 2703.7 Number Theory ........................................ . . . 272Fundamental Theorem of Arithmetic ....,................... 272More on Prime Numbers ....4........,..,.............,.. 276Euler Phi Function .............,..,....,..........,..4.. 277Exercises 3. 7 ...4,.,..........4, 4 ,..,.... . ......4,4,. 2 79Chapter 3 Review ...........................,..,....... 281On the Computer, ...................... , . ,....... . .... 283CHAPTER 4 Relations, Functions. and Matrices .......... 2854.1 Relations ............,......,..... 4 ....................... 286Binary Relations 4.....4. . ...,.4...,......4........,..,. 286Properties of Relations .....,.,..,..,...........,.4.., 289Closures of Relations .,....4..4.......4..,,.......,.. 291Partial Orderings ........ . ,.4.......... . .....,.......4.. 293Equivalence Relations .....,...................,.4....4.. 295Exemlses 4.1 ,.......... . .4,.4,..,.... . ..... . ...,. . . 3014.2 Topological Sorting ........... , .......................,.... 311Exercises 4.2 .4,..,................,.,.... . . .... . , 3164.3 Relations and Databases ............................4....... 319Entity-Relationship Model ........ . ...,................,.. 319Relational Model ,.... . ........,..,................,.,.. 320Operations on Relations .4.......,..,..,....,........,..,. 323Null Values and Three-Valued Logic ,.4,.4,..........,..,.,. 326Database Integrity 4 ..........4..,.............,,.4,4,.4. 328Exercises 4.3 ....,4..................,.... . .... . ,.... ? 294.4 Functions ..................................... , .......... 331Definition ....,..,.,.............,..,....4...........,. 332Properties of Functions ................,.,.............4. 337Onto Functions ...................,................. 337One-to—One Functions ........,..,................,., 338Bijections ....4................4................,.4 339Composition of Functions .............,................,. 339Inverse Functions ...4....... . ..,..,,.,.............,.... 341Pemiutation Functions .....,..,....,...........,..,...... 343How Many Functions .......,.4,.,.............,,.4..,... 345Equivalent Sets .,................,..,...............4.., 348Order of Magnitude of Functions 4,.4..4....... . ...,.4..,.. 349

Page 11: Mathematical Structures for Computer Science

Contents xiMore on Analysis of Algorithms ..................,., L ..... 352Exemises 4.4 ....,....,.., . .,.........,...............4 3544.5 The Mighty Mod Function .......... . ...........,...... , ,,,, 362l-lashing .....,..,.4,.,...............,...........,..,. 363Computer Security ,..,.,..,..,.... . ................,..,. 366Cryptography ,..,.,..,..,.4............. . ..,..,,.,. 366I-lashing for Password Encryption ...............,..,... 372Miscellaneous Applications ........................,..,... 373Identification Codes .,.l,........................,.., 373Generating and Decomposing Integers ...........,..,,., 375Modular Arithmetic Designs ........... . ,.4,.,........ 376Exercises 4.5 ...l,......l,..,.......... . .........,.l,. 5784.6 Matrices ..................,.....................,.,,,,,, , 381Terminology .......,.................................,. 3 B2Matrix Operations ................,.... . ........ , ..,.... 384Boolean Matrices .,................................,,... 388Exercises 4.6 ..............,..,........................ 390Chapter 4 Review ...................................... 396On the Computer .......................,.............. 399CHAPTER 5 Graphs and Trees ....................... 4015.1 Graphs and Their Representations ........................... 401Definitions of a Graph ........,.... . ...........,.,..,.... 402Applications of Graphs .................................. 405Graph Terminology ......................,.,,........... 407Isomorphic Graphs ....................,...,.,.... , ...... 410Planar Graphs ..,.....................,..,.............. 413Computer Representation of Graphs ......,..4,............. 418Adjacency Matrix ..... . ..............,.,..,..,..... 418Adjacency List ......... . .......,..,..........4..... 420Exercises 5.] .....,..,........................,........ 4235.2 'lrees and Their Representations .................,........... 433Tree Terminology ......4......................,.,....... 433Applications of Trees ..................................,. 435Binary Tree Representation ..................,............ 437Tree Traversal Algorithms .................,....,......... 438Results About Trees ...................,..,,............. 442Exercises 5.2 .....,....,............... . ..........,... 44453 Decision Trees ................... . ..,.... . ................ 451Searching .... . .,.............................,,....,.. 452Lower Bounds on Searching ..................,.,..... 454Binary Tree Search .............. . ................... 455

Page 12: Mathematical Structures for Computer Science

\xii ContentsSorting . .......,.i......,.,....4...,.......,.......... 457Exercises 5.3 .i,.,....i,.... . .....,.,.....,i........., 4595.4 Huffman Codes ................................,.......... 462Problem and 'Trial Solution . ..,,.,..,....,,.......,....... 462Huffman Encoding Algorithm ..,....i.....,.4...,..,...... 464Justification ,.....,..........,.,................ 4 ...... 466Applications of Huffman Codes .,........,..........,..... 468E,rerv.ise.v 5.4 ....,.,.....,...........,.......,......... 469Chapter 5 Review ...........,.......................... 472On the Computer ........ . .... , ....................... 474CHAPTER 6 Graph Algorithms ....................... 4756.1 Directed Graphs and Binary Relations;Warshall’s Algorithm ................ , ..................... 476Directed Graphs and Binary Relations .............,........ 477Reachabiiity ...............,...................,....... 479Warshall's Algorithm ......,....,......,.......,......... 483Exemises 6 I . ....,.. . .,,.,........,.........,........ 4876.2 Euler Path and Hamiltonian Clrcult .......................... 490Euler Path Problem ..........,..........,............... 490Hamiltonian Circuit Problem ....,..........,.............. 495Exercises 6.2 ......,........,..........,.......,..,.... 4966.3 Shortest Path and Minimal Spanning 'lree ..................... 499Shortest-Path Problem . ..... . .....,,.,.......,........... 499Minimal Spanning Tree Problem .....,..,.......,..,....... 505Exercrsex 6,3 .......,,.,.......,......... . . . .....,... 5076.4 Traversal Algorithms .................................,.... 513Depth—Fii·st Search ..........,................,.......... S13Breadth-First Search ....,.... , ........,................. 515Analysis .........,........................,........... 518Applications .,.............,........,.......,.......... 519Brercisex 6.4 ..,.......,.... . .......,.......,........, 5216.5 Articulation Points and Computer Networks. .................. S24The Problem Statement ..........................,....... 524The Idea Behind the Algorithm ............................ 525The Algonthm Itself ..................................... 526Exevrises 6.5 . , . ,....... . ,..........,. . ........,....... 529Chapter 6 Review .............................. . ....... 531On the Computer .............................. . . ..... S32

Page 13: Mathematical Structures for Computer Science

Contents xlllCHAPTER 7 Boolean Algebra and Computer Logic ........ 5347.1 Boolean Algebra Structure .................................. 535Models or Abstractions ,....,.....,.......r..,..,.r,..,.. 536Definition and Properties ....,,....,.r,.......,..,.r,..,.. 537Isomorphic Boolean Algebras ...r..,.r........,..,.r...,.. 541What Is Isomurphism7 ..,..r..,.r........,....r...... 541lsomorphism as Applied to Boolean Algebra .,..,.r,..,.. 544Exercises 7.1 ,.....,....,.....,....,..,,....,..,.....,. 5467.2 Logic Networks ....................................... { . . . 552Combinational Networks ....,.....,..,.......,..,....,,.4 552Basic Logic Elements ..,.....,.r.....r,.,,.,..,.,,.. 552Boolean Expressions ...,,....,.......r..,.......,,.. 553Truth Functions .............,....,.....,.......,... 554Networks and Expressions .............r..,.......4,.. 554Canonical Form ..................,.....,........... 556Minimizatiou ..........................,.r......... 558Programmable Logic Arrays ........,.......r......... 560A Useful Network ............................r.....,... 562Other Logic Elements .................................., 564Constmcting Truth Functions ...,......................... 565Exercises 7,2 ..............,.............,.... . .,..,.. 5667.3 Minitnixatlon ............................................. 574Minimization Process ..............r.................... 574Karnaugh Map ...............................r......... 575Maps for Three and Four Variables ......l.............. 576Using the Kamaugh Map ...................,......... 578Quine·McCluskey Procedure .............................. 582Exercises 7,3 ...........,..,....,..,.....,..,.,..,..,.. 586Chapter 7 Review. .................. . .................. 590On the Computer .............,....................... 591CHAPTER 8 Modeling Arithmetic. Computation,and Languages ......................... 5928.1 Algebraic Structures ...................... . ................ S93Definitions and Examples ................................ 593Basic Results About Groups .........,.................... 600Subgroups .......,....,..,..,......................... 603lsomorphic Groups ,............ . ...................... 606Exercises 8.1 . . .,..........,........................... 6128.2 Finite-State Machines ...................................... 618Definition ...................................,......... 618/

Page 14: Mathematical Structures for Computer Science

xlv ContentsExamples of Finite-State Machines .,......,.,...........,.. 619Recognition ..........,...............,..,..........,.4 622Regular Sets and Kleene's Theorem .,......,.,....,........ 624Machine Minimization ...,...4...........,..........,,... 626Unreachable States ..,.4..,...........,.,........,.,, 626Minimization Procedure ................,......,.,.., 627Sequential Networks and Finite-State Machines .........,....i 632Ex4·rcixc.r 8.2 ..........i,.,.............i..........i,., 6368.3 'lurlng Machin ...,.................,......,............. 646Definition .......,........,.i...........,..........,,.. 647Turing Machines as Set Recognizers .........,...........,.. 651Turing Machines as Function Computers . ..,,.,..........,,i 653Church-'Dning Thesis ..,,.,.............,.4...........,. 655Decision Problems and Uncomputability ....,...........,... 657Examples of Decision Problems .....,..,.,........,... 658I-lalting Problem ..,................,.,............., 659Computational Complexity ,.............,. 4 ........,,.... 660Exercises 8.3 ,....... . .....,.............,.... . .....,i 6638.4 Forma] Languages ...........................,............. 666Classes of Grammars ....4................,..........,,.. 672Formal Languages and Computational Devices 4,.... . .....,.4 675Context-Free Grammars ..,,.,.............,............. 676Exercises 8.4 r..,........ , ....,..........r,r,.......... 679Chapter 8 Review .....,.. . ..........,.................. 682On the Computer. . .................... , ............... 684APPENDIX A Derivation Rules for Propositional andPredicate Logic ......................... 685APPENDIX B Summation Notation ..................... 687APPENDIX C The Logarithm Function .................. 690Answers to Practice Problems .............. 693Answers to Selected Exercises .............. 727Answers to Self-Tests .................... 789Index ................................. 797

Page 15: Mathematical Structures for Computer Science

Discrete structures plays an ever more prominent role in computer science education. ComputingCurricula 2001 (CC200l) was a joint undertaking of the Institute for Electrical and ElectronicEngineers/Computer Society (IEEE/CS) and the Association for Computing Machinery (ACM).The iinal report may be found at http://www.acm.orysigcsdcc2001/. CC200l identines a com-puter science bady of knowledge, a set of knowledge units "for which there is a broad consensusthat the material is essential to an undergraduate degree in computer scienee.” CC200l describesdiscrete structures as "foundational material for computer science" and devotes 43 hours of thecomputer science body of knowledge to discrete structures, more hours than to any other area.This Sixth Edition of Mathematical Structures for Computer Science covers all the topics inthe CC200l discrete structures core and quite a bit more. All topics suggested for a one—semeste1intensive discrete structures course—and virtually everything suggested for a two—semesterversion of a discrete structures course~are covered here.Binding together what otherwise appears to be a collection of disjoint topics are the followingthemes:• Importance of logical thinking• Power of mathematical notation• Usefulness of abstractionsOf all computer science courses, discrete structures is probably the least appreciated in realtime and the most appreciated given more perspective. Two years after the fact, students report,"Every course I had after that used material from discrete structures?Content Changes in the Sixth EditionMajor changes include the following:• Reorganization of Chapter 2 to make Recurrence Relations a separate section(Section 2.5), with the addition of material on linear second-order recurrencerelations and on divide-and-conquer recurrence relations• New section on number theory (Section 3.7)• New section (Section 4.5) on "tl1e mighty mod function," giving a number ofapplications of the mod function in hashing, cryptography, identification codes,and modular arithmetic designs. among others• Restoration, at the request of some reviewers. of a section (Section 6.5) onarticulation points in graphs that had been deleted from the Fifth Edition.

Page 16: Mathematical Structures for Computer Science

xvi PrefaceNew Exercises have also been added to each section. It appears that exercises. like chocolatechip cookies, are something you just can’t have too many of. Answers to all Practice Problemsare given at the back of the book, as are answers to all starred Exercises.A complete Solutions Manual is available to instructors from the publisher.Web S iteOnline Study GuideA Web site for the book may be found at www.whfreen1an.com/gersting, accessed using thepassword "logic." It functions as an online study guide, offering for each chapter a chapteroverview, section overviews, and sample problems with audio commentaries outlining the srep-by-step solution process. The sample problems (not contained in the text) are representative ofmany of the end-of-section Techniques. Each Technique that has a corresponding Web pageexample is marked with the icon W .Each sample problem on the Web first states the problem, and then, as the student navigatesthe pages, the solution unfolds step-by-step much as the student would be expected to write it.A compressed audio tile is also part of each Web page after the initial problem statement. Theaudio file contains a first-person stream-of-consciousness thought process about that step of thesolution—why it occurred to the narrator to try it, why it looked promising, what knowledgewas being called upon to suggest that it should come next, and so on. Students see perfect and~complere worked-out proofs in the textbook and often also see them performed by the instruc-tor. Yet when a student goes home and tries to produce a solution alone, he or she is unsurewhere to start or howto think about the problem or how to discem any pattem to enable a guessas to what to do next. Consequently the student gives up in frustration. The purpose of the audionarration is to share the "secret picture" that mathematicians use to solve problems.To access the problems, after you go to www.whfreeman.com/gersting, select a chapter, login with the password "logic," select a chapter section, then select a sample problem and followits step-by-step process with the "Next" button. Several sample problems are provided for eachsection of each chapter.PowerPoint SlidesPowerPoint slides accompanying each section of the text are available on the Web site atwww.whfreeman.com/gersting.AcknowledgmentsMy thanks to the reviewers of this edition, as well as to reviewers of earlier editions, all ofwhose help is greatly appreciated.Elizabeth S. Adams, James Madison Adrienne Decker, SUNY BujfaloUniversity Steve Donaldson, Samford UniversityKemal Akkaya, Southern Illinois Universiry Mordechai S. Goodman, DominicanCharles Ashbacher, Mount Mercy College UniversityTerry J. Bridgman, Colorado School of Mines Michael A. Gray. American UniversityDavid Casperson, University of Northern Jerrold R. Griggs, University of SouthBritish Columbia Carolina

Page 17: Mathematical Structures for Computer Science

Preface xviiJoseph Hobart, Okanagan College Amar Raheja, Cal Poly _Mark Jacobson, University of Northern Iowa J. Ben Schafer, University of Northern IowaLisa A. J amba, University of Northern Ali Shaykhian, Florida Institute ofFlordio TeehnalogyTim Lin, Cal Poly Shunichi Toida, Old Dominion UniversityDavid Luginbuhl, Western Carolina William J. Weber, Southeast Missouri StateUniversity UniversityDamian Lyons. Fordham University Eric Westlund, Luther UniversityMariana Maris, Arizona State Universiry Hua Yan, Borough of Manhattan CommunityMikel D. Petty, University ofAlabama in CollegeHuntsville Yu Zhang, Texas A&M Corpus ChristiMy deepest thanks go to my husband, John, ever my most ardent supporter and dearestfriend.Note to the Student

As you go through this book, you‘ll encounter many new terms and new ideas. Try reading withpencil and paper at hand and work the Practice Problems as you encounter them. They areintended to reinforce or clarify terminology or a method just introduced; answers are given atthe back of the book. Pay attention also to the Reminders that point out common pitfalls or pro-vide helpful hints. Be sure to visit the Web site at www.whfreeman.com/gersting, using thepassword "logic," for detailed, worked-out solutions to additional example problems tied to theTechniques at the end of each section, The Web site solutions are accompanied by audio tilesthat explain each step.You may find at first that the thought processes required to solve the exercises in the book arenew and difficult. Your biggest attribute for success will be perseverance. Herels what l tell mystudents: "lf you do not see at first how to solve a problem, don’t give up. Think about it somemore; be sure you understand all the terminology used in the problem, play with some ideas. Ifno approach presents itself, put it aside and think about it again later. Repeat this process fordays on end. When you finally wake up in the middle of the night with an idea, y0u’ll know youare putting in the right amount of effort for this course." Mathematical results don’t spring fullyformed from t.he foreheads of mathematical geniuses (well. maybe from mathematical geniuses);but for the rest of us, it takes work, patience, false starts. and perseverance.Enjoy the experience!

Page 18: Mathematical Structures for Computer Science
Page 19: Mathematical Structures for Computer Science

F I L I C H A P T E RCHAPTER OBJECTIVESAfter studying this chapter you will be able to;• Use the formal symbols of propositional logic,• Find the truth value of an expression in propositional logic.• Construct formal proofs in propositional logic, and use such proofs to detemiine thevalidity of English language arguments.• Use the formal symbols of predicate logic.• Find the truth value in some interpretation of an expression in predicate logic.• Use predicate logic to represent English language sentences.• Construct formal proofs in predicate logic, and usevsuch proofs to determine the validity ofEnglish language arguments.• Understand how the programming language Prolog is built on predicate logic,• Mathematically prove t.\1e correctness of programs that use assignment statements andconditional statements.You have been selected to serve on jury duty for a criminal case. The attorney for the defense arguesas follows:If my client is guilty, then the knife was in the dr-awen Either the knife was not in the drawer orJason Pritchard saw the knife. If the knife was not there on October 10. it follows that JasonPritchard did not see the knife. Furthermore, if the knife was there on October 10, then the knifewas in the drawer and also the hammer- was in the barn. But we all know that the hammer was notin the barn. Therefore, ladies and gentlemen of the jury, my client is innocent.Q U E STI 0 N Is the attnrney’s argument sound? How should you vote?It's much easier to answer this question if the argument is recast in the notation of formal logic.Formal logic strips away confusing verbiage and allows us to concentrate on the underlyingreasoning being applied. In fact, formal logic—the subject of this chapter~provid.es thefoundation for the organized, careful method of thinking that characterizes any reasonedactivity—a criminal investigation, a scientific experiment, a sociological study. In addition. for-mal logic has direct applications in computer science. The last two sections of this chapterexplore a programming language based on logic and the use of formal logic to verify the

Page 20: Mathematical Structures for Computer Science

2 cuaarea 1 Formal ragircorrectness of computer programs. Also, circuit logic (the logic governing computer circuitry)is a direct analog of the statement logic of this chapter. We shall study circuit logic in Chapter 7.SECTION 1.1 Statements, Symbolic Representation,and Tautologies

Fomial logic can represent the statements we use in English to communicate facts or informa-tion. A statement (or proposition) is a sentence that is either true or false.EXAMPLE ‘| Consider the following:a. Ten is less than seven.b. How are you?c. She is very talented.d. There are life forms on other planets in the universe.Sentence (a) is a statement because it is false. Because item (b) is a question, it cannot be con-sidered either true or false. It has no truth value and thus is not a statement. Sentence (c) is ¤ei—ther true nor false because "she" is not specified; therefore (c) is not a statement. Sentence (d) isa statement because it is either true or false; we do not have to be able to decide which. •Connectives and 'Iruth ValuesIn English, simple statements are combined with connecting words like and to make moreinteresting compound statements. The truth value of a compound statement depends on thetruth values of its components and which connecting words are used. If we combinethe two true statements "Elephants are big“ and "Baseballs are round," we would consider theresulting statement, “Elephants are big and baseballs are round," to be true. In this book, as inmany logic books, capital letters near the beginning of the alphabet, such as A, B, and C, are usedto represent statements and are called statement letters; the symbol A is a logcal connectiverepresenting und. We agree, then that if A is true and B is true, A A B (read ‘2A and B") shouldbe considered true.a. If A is true and B is false, what truth value would you assign to A A B7ln. If A is false and B is true, what truth value would you assign toA A B7c. lf A and B are both false, what truth value would you assign to A A B7 •The expressionA A B is called the conjunction of A and B, and A and B are called the con-juncts of this expression, Table l.l summarizes the truth value of A A B for all possible truth. 1Answerst¤ mace problems are in me back ef me ma.

Page 21: Mathematical Structures for Computer Science

szcn0N 1.1 smemems, symbolic kepresenrnmsn, and raumlogies 3TABLE 1.1 TABLE 1.2 values of the conjuncts A and B, Each row oftheA B A A B A B A V B table represents a particular truth value assign-ment to the statement letters, and the resultrngT T T T T T truth value for the compound expression isT F F T F shown.F T F F T Another connective is ine word or, denotedF F F F F by the symbol v. The expression A v is (read"A or B") is called the drisjunctlon of A and B,and A and B are called the rlisjuncts of this expression. lf A and B are both ture, then A V Bwould be considered true, giving the tirst line of the tnrth table for disjunction (see 'Iable 1.2).Use your understanding of the word or to complete the truth table for disjunc-tion. 'I`able 1.2. •Statements may be combined in the form "If statement l, then statement 2." If A denotesstatement l and B denotes statement 2. the compound statement would be denoted by A —> B(read "A implies B"). The logical connective here is implication. and it conveys the meaningthat the truth of A implies or leads to the truth of B. In the implicationA —> B. A stands for theantecedent statement and B stands for the consequent statement.The truth table for implication is less obvious than that for conjunction or disjunction, Tbunderstand its definition. let's suppose your friend remarks. "1f I pass my economics test. thenI‘1l go to the movie Friday." If your friend passes the test and goes to the movie. the remark wastrue. If your friend passes the test but doesn't go to the movie, tl·re remark was false. If yourfriend doesn’t pass the test. then—whether he or she goes to the movie or not—you could notclaim that the remark was false, You would probably want to give the benefit of the doubt andsay that the statement was true, By convention, A —> B is considered true if A is false, regardlessof the truth value of B,Summarize this discussion by writing the tnrth table for A —> B. •The equivalence connective is symbolized by ·», The expression A ~» B is shorthand for(A —> B) A (B —> A). We can write the truth table for equivalence by constructing, one piece ata time, a table for (A -> B) A (B -> A), as in 'Iable 1.3. From this truth table, A e-» B is trueexactly when A and B have the same truth value.TABLE 1.3A B A—>B B->A (A—>B)A(B—>A)T T T T TT F F T FF T T F FF F T T TThe conrrectives we’ve seen so far are called binary connectives because they join twoexpressions together to produce a third expression. Now let's consider a unary connective. aconnective acting on one expression to produce a second expression. Negation is a unary con-nective. The negation of A—symbol.ized by A’—is read "not A."

Page 22: Mathematical Structures for Computer Science

4 cHAPtER1 rmmaitagicwnte me truth table rar A'. (it will require only two rows.) •Table 1.4 summarizes the t:ruth values for all of the logical connectives. This infomation iscritical to an understanding of logical reasoning.TABLE 1.4A B AAB AVB A—>B A·»B A'T T T T T T FT F F T F FF T F T T F TF F F F T TBecause of the richness of the English language, words that have different shades of mean-ing are nonetheless represented by the same logical connective. Table 1.5 shows the commonEnglish words associated with various logical connectives.TABLE 1.5_ English Word Logical Connective Logical Expressionand; but; also; in addition; moreover Conjunction A A B" or Disjunction A V B` If A, then B. Implication A —> BA implies B.A, therefore B.A only if B.B follows from A.A is a sufficient condition for B.B is a necessary condition for A.A if and only if B. Equivalence A ·-» BA is necessary and sufficient for B.not A Negation A'It is false that AIt is not true that ASuppose that A —> B is true. Then, according to the truth table for implication, the conse-quent. B, can be true even though the antecedent. A, is false. So while the truth of A leads to(implies) the truth of B, the ¤·uth of B does not imply the t.ruth of A. The phrase "B is a neces-sary condition for A” to describe A -> B simply means that if A is true. then B is necessarilytrue, as well. "A only if B" describes the same thing, that A implies B.EXAMPLE 2 The statement "Fire is a necessary condition for smoke" can be restated as "If_ there is smoke. then there is f'1re." The antecedent is "there is smoke.” and the consequent is"there is fire." •

Page 23: Mathematical Structures for Computer Science

SEC 710/V 1.1 Statements, Symbolic Representation, and Tautologies 5-'`'v T Name the antecedent and consequent in each of the following statements.(Hint: Rewrite each statement in if-then form.)a. If the rain continues, then the river will flood.b. A sufficient condition for network failure is that the central switch goes down.c. The avocados are ripe only if they are dark and soft.d. A good diet is a necessary condition for a healthy cat. •EXAMPLE 3 Expressing the negation of a statement must be done with care, especially fora compound statement. 'l‘able 1.6 gives some examples.TABLE 1.6Statement Correct Negation Incorrect NegationIt will rain tomorrow. It is false that it will raintomorrow.lt will ncl rain tomorrow.Peter is tall and thin. It is false that Peter is tall and Peter is short and fat.mm Too strong a statement.Peter is not tall or he is not thin. Peter fails to have both. properties (tallness andP°'°' *5 ‘l‘°" °' M thinness) but may stm haveone property.The river is shallow lt is false that the river is The river is not shallow oror polluted. shallow or polluted. not polluted.The river is neither shallow Too weak a statement. Thenor polluted. river fails to have eitherThe river is dwp and gngpelrgyknotjust fails to haveunpolluted. P P ny'’'`'' j ""f`§ '>``' _aj5 Which of the following represents A' if A is the statement "Julie likes butterbut hates cream"?a. Julie hates butter and cream.b. Julie does not like butter or cream.c. Julie dislikes butter but loves cream.d. Julie hates butter or likes cream. •

Page 24: Mathematical Structures for Computer Science

6 CHAPTER 1 Formal logicWe can string statement letters, connectives, and parentheses (or brackets) together to fonnnew expressions, as in(A —> B) A (B —> A)Of course, just as in a computer programming language. certain syntax ruler (rules on whichstrings are legitimate) prevail; for example,A ))A A —> BCwould not be considered a legitimate string. An expression that is a legitimate string is called awell-formed formula, or wlf. To reduce the number of parentheses required in a wtf. we stip-ulate an order in which connectives are applied. This order afpreczdence isl. connectives within parentheses, innermost parentheses first3. A. V4. —>5. 4-»This means that the expression A V B' stands f0rA V (B'), not (A V B)'. Similarly, A V B —> Cmeans (A V B) -> C, not A V (B —> C). However. we often use parentheses anyway, just to besure that there is no confusion.In a wff with a number of connectives, the connective to be applied last is the main con-nective. InA A (B —> C)’the main connective is A. In((AVB)AC)—>(BVC')the main connective is —>. Capital letters near tl1e end of the alphabet, such as P, Q, R, and S.are used to represent wffs. Thus P could represent a single statement letter, which is the sim-plest kind of wff, or a more complex wff. We might represent((AVB)AC)—>(BVC')asP —> Qif we want to hide some of the details for tl1e moment and concentrate only on the mainconnective.Wffs composed of statement letters and connectives have truth values that depend on thetruth values assigned to their statement letters. We write the truth table for any wff by buildingup the component parts, just as we did for (A —> B) A (B—> A). The main connective isaddressed in the last column of the table,

Page 25: Mathematical Structures for Computer Science

sscnorv 1.1 statements, Symbolic Representation, and Tautologies 7EXAMPLE 4 The truth table for the wffA V B' -> (A V B)' is given in Table 1.7. The main »connective, according to the rules of precedence, is implication.TABLE 1.7A B B' AVE' AVB (AVB)’ AVB'-·>(AVB)'T T F T T F FT F T T T F FF T F F T F TF F T T F T T •If we are making a truth table for a wff that contains 11 different statement letters, how manyrows will the truth table have'] From truth tables done so far, we know that a wff with only onestatement letter has two rows in its truth table, amd a wff with two statement letters has fourrows. The number of rows equals the number of true-false combinations possible among thestatement letters. The first statement letter has two possibilities, T and E For each of these pos-sibilities, the second statement letter has two possible values. Figure l.la pictures this as atwo-level "tree" with four branches showing the four possible combinations of T and F for twostatement letters. For n statement letters, we extend the tree to 11 levels. as in Figure l.lb. Thetotal number of branches then equals 2". The total number of rows in a truth table for rr state-ment letters is also Z".Statement letters ChoicesT F1 2 e 2* branchesT F T F2 4 = 22 branchesT T F FT F T Fta)Statement letters ChoicesT F1 2 e 21 branchesT F T F2 4 = 22 branchesT F T F T F T Fz s = 23 branchesn 2** brancheslb)FIGURE 1.1

Page 26: Mathematical Structures for Computer Science

B CHAPTER 1 Rsrmal Logic1>xBLE L8 This tree structure also tells us how to enumerate all the T-F combinations amongA B C the n statement letters when setting up a truth table. If we read each level of the tree frombottom to top. it says that the T-F values for statement letter n (which will compose theT T T last column of the truth table) altemate, those for statement letter n — 1 altemate everyT T F two values, those for statement letter n — 2 altemate every four values. and so forth.T F T Thus a truth table for three statement letters would begin as shown in Table 1.8. The val-T F F ues for statement letter C altemate, those for statement letter B altemate in groups ofF T T two, and those for statement letter A altemate in groups of four, resulting in somethingF T F like a sideways version of the tree. (Reading the rows from the bottom up and usingE 11; I; l forT and 0 forF shows that we are simply counting up from zero in binary numbers.)Construct truth tables for the following wffs,a. (A —> B) ·· (B —> A) (Remember that C ·» D is true precisely when C and D have thesame truth value.)b. (A VA') —> (B A B')c. [(A A B') —> C']'d,(A->B)<-·(B'—>A') •TautologiesA wff like item (d) of Practice 7. whose truth values are always true. is called a tautology. Atautology is "inti-insically true" by its very structure; it is true no matter what truth values areassigned to its statement letters. A simpler example of a tautology is A V A'; consider, forexample, the statement "Today the sun will shine or today the sun will not shine," which mustalways be true because one or the other must happen. A wff like item (b) of Practice 7, whosetruth values are always false. is called a contradiction. A contradiction is "intrinsical1y false"by its very structure. A simpler example of a contradiction is A A A': consider "Today is Tues-day and today is not Tuesday." which is false no matter what day of the week it is.Suppose that P and Q represent two wffs, and it happens that the wff P ·· Q is a tautology. Ifia : we did a truth table using the statement letters in P and Q, then the truth values of the wffsP and would a ree for every row of the truth table. In this case, P and Q are said to be equiv-_ . . -. galent wffs. denoted by P ·= Q. Thus P =· Q states a fact, namely, that the particular wff P —» Q' is a tautology. Practice 7(d) has the form P <-> Q, where P is the wff (A —» B) and Q is the wff1*** (B' —> A'), and P ·-» Q was shown to be a tautology. Therefore, (A —> B) ·= (B' —> A').E iufvzsmd We shall list some basic equivalences, prove one or two of them by constructing truth tables,` and leave the rest as exercises. We represent any contradiction by 0 and any tautology by l.SOME TAUTOLOGICAL EQUIVALLNCESla. A V B ae B V A lb. A A B ¢ B A A (commutative properties)2a. (A V B) V C c> A V (B V C) 2b. (A A B) A C ¢A A (B A C) (associative properties)3a. A V (B A C) ee 3b. A A (B V C) ev (distributive properties)(AVB)A(AVC) (AAB)v(AAC)4a. A V 0 ¤A 4b. A A l eo A (identity properties)5a. A-V A' ce l 5b. A A A' co 0 (complement properties)

Page 27: Mathematical Structures for Computer Science

sscnorv 1.1 statements, Symbolic Representation, and numlogies 9Note that 2a allows us to writeA V B V C with no need for parentheses; similarly, 2b allows usto writeA AB A C.V EXAMPLE 5 The truth table in Table 1.9a verifies equivalence la, the comrnutative prop-erty for disjuncdon, and that in 'I‘ab1e 1.9b verifies 4b, the identity property for conjunction.Note that only two rows are needed for Table 1.9b because 1 (a tautology) cannot take on falsetruth values.TABLE 1.9A B AVB BVA AVBQBVAT T T T TT F T T T A 1 A A l A A l<-»AF T T T T T T T TF F F F T F T F T(a) (bl •® Verify equivalence Sa. •The equivalences in the list are grouped into tive pairs. In each pair, one equivalence can beobtained from the other by replacing A with V, V with A, O with 1, or 1 with 0. Each equiva-lence in a pair is called the dual of the other. Thus, la and lb (commutativity of disjunction andcommutativity of conjunction) are duals of each other. This list of equivalences appears in amore general setting in Chapter 7.Two additional equivalences that are very useful are De Morgan's laws, named for thenineteenth-century British mathematician Augustus De Morgan. who first stated them.Each is the dual of the other. De Morgan’s laws help i.n expressing the negation of a compoundstatement, as in Practice 6.Suppose that P and Q are equivalent wffs. Then P can be replaced by Q in any wff R con-taining P, resulting in a wff RQ that is equivalent to R.EXAMPLE 6 bet R be (A —> B) -> B, and let P be A —> B. From Practice 7(d), P is equiva-lent to Q = B' —> AK Replacing P with Q in R, we get RQ = (B’ —>A') —> B. The truth tablesfor R and RQ are shown in 'I`ables 1.10 and 1.11, which show that R and RQ are equivalent.TABLE 1.10 TABLE 1.11A B A->B (A->B)—>B A B A’ B' B'-+A' (B'-»A')—>BT T T T T T F F T TT F F T T F F T F TF T T T F T T F T TF F T F F F T T T F •

Page 28: Mathematical Structures for Computer Science

10 CHAPTER 1 Formal logicLogical Connectives in the Real WorldWeb search engines allow exploration of the vast resomces available on the Web, but a littlecare in your search query can help focus the results more quickly. For example, if you enterused carsin a Web search engine, you may get back references to any Web site containing eitber the wordused or the word cars; this could include sites for antique dealers and sites for the latest autoracing results. Entering the phrase"used cars"in quotes restricts the search, on most search engines, to Web sites containing this exact phrase.Most search engines also allow you to enter an expression using logical connectives as yoursearch query. which can help make the query even more specific. To further narrow your usedcar search, for example, you could enter"used cars” AND (Ford OR Buick)This would tend to limit your search to sites that mention only particular brands of used cars,although you could still wind up with a link to Jim Bob Ford’s Loan Shark Agency, which willlend you money for any used car. The query"used cars" AND (Ford OR Buick) AND NUT truckswould eliminate sites mentioning trucks. Many search engines use + (a plus sign) in place ofAND and - (a minus sign) in place of AND NO'I`.The logical connectives AND, OR, and NOT are also available in many programming lan-guages, as well as on programmable graphing calculators. These connectives, in accordancewith the truth tables we have defined, act on combinations of true or false expressions to pro-duce an overall tnith value. Such truth values provide the decision-making capabilities funda-mental tothe flow of control i.n computer programs. Thus, at a conditional branch in a program,if the truth value of the conditional expression is true, the program will next execute one sectionof code; if the value is false, the program will next execute a different section of code. If theconditional expression is replaced by a simpler, equivalent expression, the truth value of theexpression and hence the flow of control of the program is not affected, but the new code is eas-ier to imderstand and may execute faster.EXAMPLE 7 Consider a statement in a computer program that has the formif ((outflow > inflow) and not ((outflow > inflow) and (pressure < 1000)))do something;elsedo something else;

Page 29: Mathematical Structures for Computer Science

SECTION 1.1 szatemems, Symbolic Represemaiion, and raurologies 11Here the conditional expression has the form .A A (A A B)'where A is "outt'low > inflow" and B is "pressure < l000." This expression can be simplifiedby replacing some wffs with equivalent wffs,A A (A A B)' = A A (A' V B') (De Morgan’s Laws)= (A AA') V (A A B') (tautology 3b)= 0 V (A A B') (tautology 5b)= (A A B') V 0 (tautology la)= A A B' (tautology 4a)The statement form can therefore be writtenif ((outtlow > inflow) and not (pressure < 1000))do something:elsedo something else; •An AlgorithmTo test whether a wff is a tautology. we can always write its truth table. For n statement letters.2" rows will be needed for the truth table. Suppose, however. that the wff has implication as itsmain connective, so it has the form P —> Q where P and Q are themselves wffs. Then we canuse a quicker procedure than constructing a truth table to determine whether P —> Q is a tautol-ogy. We assume that P —> Q is not a tautology. and see whether this leads to some impossiblesituation. If it does, then the assumption that P —> Q is not a tautology is also impossible, andP -> Q must be a tautology after all.Tb assume that P —» Q is not a tautology is to say that it can take on false values, and, by thetruth table for implication, P —> Q is false only when P is true and Q false. By assigningP true and Q false, we determine possible truth values for the wffs making up P and Q. We con-tinue assigning the truth values so determined until all occurrences of statement letters have atruth value. lf some statement letter is assigned both true and false values by this process, wehave an impossible situation, so the wff P —> Q must be a tautology. Otherwise. we have founda way to make P -> Q false, and it is not a tautology.What we have described is a set of iustructions—a procedure—for carrying out the task ofdetennining whether P -> Q is a tautology. This procedure can be executed by mechanicallyfollowing the instructions; in a finite amount of time, we will have the answer. In computer sci-ence tenns, the procedrne is an algorithm.Algorithms constitute the very heart of computer science, and we shall have much ro sayabout them throughout this book. You are probably already aware that the major task in writing

Page 30: Mathematical Structures for Computer Science

12 cewtssi twmni loana computer program for solving a problem consists of devising an algorithm (a procedure) toproduce the problem solution.Algorithms are often described in a form that is a middle ground between a purely verbaldescription in paragraph form (as we gave for deciding whether P —> Q is a tautology) and acomputer program (that, if executed. would actually carry out the steps ofthe algorithm) writ-ten in a programming languager This compromise form to describe algorithms is calledpseudocode. An algorithm written in pseudocode should not be hard to understand even if youknow nothing about computer programming, The only thing to note about the pseudocode usedin this book is that lines preceded by double slashes (//) are explanatory comments, not part ofthe algorithm itself.Following is a pseudocode form of the algorithm to determine whether P —> Q is a tautology.ALGORITHM Tautology TestTauml0gyTést(wff P; wtf Q)//Given wffs P and Q, decides whether the wff P —> Q is alftautology.//Assume P —> Q is not a tautologyP = true llassign T to PQ = false //assign F to Qrepeatfor each compound wff already assigned a trtrth value.assign the truth values detennined for its componentsuntil all occurrences of statements letters have tnith valuesif some letter has two truth valuesthen Ncontradiction, assumption falsewrite ("P —> Q is a tautology.")else //found a way to make P ——> Q falsewrite ("P —> Q is not a tautology.")end ifend TautologyTestThe algorithm First assigns the truth values "uue" to P and "false" to Q, consistent with theassumption that P —> Q is not a tautology. The algorithm then enters a loop. where a sequenceof steps is repeated until some condition is met. Within the loop, truth assignments continue tobe made to smaller and smaller components of the original P and Q until all occurrences ofindividual statement letters have truth values. Then the algorithm tests whether a contradictionhas occurred, and writes out the infonnation about whether P —> Q is a tautology.EXAMPLE 8 Consider the wff (A —> B) —> (B' —> A'). This matches the pattem needed inorder to use algorithm TautolngyTe.vt, namely P —> Q. where P is A —> B and Q is B' —> A'.

Page 31: Mathematical Structures for Computer Science

SECTION 1,1 Statements, Symbolic Representation, and Tautologies 13Following the algorithm, we first assign truth values .A —>Btrue and B' —>A' falseMoving on to the loop, the assignment of false to the compound statement B'—> A' determinesthe further assignmentsB' true and A' false

B false and A trueNow working with P, A true andA —> B true determines the assignmentB trueAt this point all occurrences of statement letters have truth values. as follows:A—T B—T B—F A-—T(A —>B) —> (B' —>A’)T FThis terminates the loop. In the final step of the algorithm, B now has an assignment of bothT and F, so the algorithm decides that (A -> B) —» (B' -·> A') is a tautology. Actually, we leamedNg this earlier (in Practice 7(d)) by building a truth table. •__ Algorithm Tautalugyhrt decides whether wffs of a certain form, namely, those where themain logical connective is ->, are tautologies. However, the process of building a truth table~ and then examining all the truth values in the final column constitutes an algorithm to decidein "; whether an arbitrary wff is a tautology. This second algorithm is therefore more powerful,,,,,,,,5 because it solves a more general problem, but algorithm Tau.wlagyTe.tr is usually faster forQ. those wffs to which it applies.SECTION 1 .1 ReviewTECHNIQUESW • Construct truth tables for compound wffs.W • Recognize tautologies and contradictions.MAIN IDEAS• Wffs are symbolic representations of statements.• Truth values for compound wffs depend on the truth values of their componentsand the types of connectives used, _• Tautologies are "intrinsically true" wffs—true for all truth values.

Page 32: Mathematical Structures for Computer Science

14 cuarrsk 1 romral LogicEXERCISES 1.1Answers to starred items are given in the back of the book.* 1. Which of the following are statements?a. The moon is made of green cheese. Qb. He is certainly a tall man. Qc. Two is a prime number. Md. Will the game be over sooniie. Next year interest rates will rise.~(f. Next year interest rates will fall. \/g. rz — 4 = OX2. Given the tmth values A true. B false, and C tmc. what is the truth value of each of thefollowing wffs7a.AA(BvC) c. (AAB)'vCb.(AAB)vC d.A'v(B’AC)'3. What is the truth value of each of the following statements?a. 8 is even or 6 is odd. e. If 8 is odd, then 6 is odd.b. S is even and 6 is odd. f. I.f 8 is even, then 6 is odd.c. 8 is odd or 6 is odd. g. lf Sis odd. then 6 is even.d. 8 is odd and 6 is odd. h. lf 8 is odd and 6 is even, then S < 6.it 4. Find the antecedent and consequent in each of the following statements.a. Healthy plant growth follows from sufficient water.b. Increased availability of information is a necessary condition for furthertechnological advances.c. Errors will be introduced only if there is a modification of the program.d. Fuel savings implies good insulation or storm windows throughout.5. Several forms of negation are given for each of the following statements. Which arecorrect'!a. The answer is either 2 or 3.1. Neither 2 nor 3 is the answer.2. The answer is not 2 or not 3.3. The answer is not 2 and it is not 3.b. Cucumbers are green and seedy.1. Cucumbers are not green and not seedy.2. Cucumbers are not green or not seedy.3. Cucumbers are green and not seedy.c. 2 < 7 and 3 is odd.1. 2 > 7 and 3 is even.2. 2 2 7 and 3 is even.3. 22 7or3isodd.4. 2 2 7 or 3 is even.6. Several forms of negation are given for each statement. Which are correct?a. The carton is sealed or the milk is sour.1. The milk is not sour or the carton is not sealed.2. The carton is not sealed and also the milk is not sour.3. If the carton is not sealed, then the milk will be sour.

Page 33: Mathematical Structures for Computer Science

sscnorv 1.1 Slalemenls, Symbolic Representation, and rsuiorogiss 15b. Flowers will bloom only if it rains. .l. The flowers will bloom but it will not rain.2. The flowers will not bloom and it will not rain.3. The flowers will not bloom or else it will not rain.c. lf you build it, they will come.l. lf you build it, then they won’t come.2. You don’t build it, but they do come.3. You build it, but they don't come.7. Write the negation of each statement.it a. lf the food is good, then the service is excellent.it b. Either the food is good or the service is excellent.c. Either the food is good and the service is excellent, or else the price is high.d. Neither the food is good nor the service excellent.e. I.f the price is high, then the food is good and the service is excellent.B. Write the negation of each statement.a. The processor is fast but the printer is slow.b. The processor is fast or else the printer is slow.c. If the processor is fast, then the printer is slow.d. Either the processor is fast and the printer is slow. or else the file is damaged.e. If the file is not damaged and the processor is fast. then the printer is slow.f. The printer is slow only if the file is damaged.9. Let A, B, and C be the following statements:A Roses are red.B Violets are blue.C Sugar is sweet.Translate the following compound statements into symbolic notation.a. Roses are red and violets are blue.b. Roses are red. and either violets are blue or sugar is sweet.c. Whenever violets are blue. roses are red and sugar is sweet.d. Roses are red on.ly if violets aren‘t blue or sugar is sour.e. Roses are red and, if sugar is sour, then either violets aren't blue or sugar is sweet.10. Let A, B. C, and D be the following statements;A The villain is French.B The hero is American.C The heroine is British.D The movie is good.Translate the following compound statements into symbolic notation.a. The hero is American and the movie is good.b. Although the villain is French, t.he movie is good.c. lf the movie is good. then either the hero is American or the heroine is British.d. The hero is not American, but the villain is French.e. A British heroine is a necessary condition for the movie to be good.

Page 34: Mathematical Structures for Computer Science

16 CHAPTER 1 Formal Logicl1. Use A, B, and C as dehned in Exercise 9 to translate the following statements into English.a.BVC' e. (B/\C')'—>Ab.B'V(A—>C) f,Av(BAC')c. (C/\A')~»B g. (AvB)AC'd. C A (A' H B)* 12, Using letters for the component statements, translate the following compound statementsinto symbolic notation.a. If prices go up, then housing will be plentiful and expensive; but if housing is notexpensive, then it will still be plentiful.b. Either going to bed or going swimming is a sufficient condition for changing clothes;however, changing clothes does not mean going swimming.c. Either it will rain or it will snow but not both.d. lflanet wins or if she loses, she will be tired.e. Either Janet will win or, if she loses, she will be tired.13. Using letters H, K, A for the component statements, translate the following compoundstatements into symbolic notation.a. lf the horse is fresh, then the knight will win.b. The knight will win only if the horse is fresh and the armor is strong.c, A fresh horse is a necessary condition for the knight to win.d. The knight will win if and only if the armor is strong.e. A sufficient condition for the knight to win is that the armor is strong cr the horse is `fresh.14. Using letters A, T, E for the component statements, translate the following compoundstatements into symbolic notation.a. lf Anita wins the election, then tax rates will be reduced.b. Tax rates will be reduced only if Anita wins thc election and the economy remainsstrong.c. Tax rates will be reduced if the economy remains strong,d. A strong economy will follow from Anita winning the election.e. The economy will remain strong if and only if Anita wins the election or tax rates arereduced.15. Using letters F, B, S for the component statements, translate the following compoundstatements into symbolic notation.a, Plentiful fish are a sufficient condition for bears to be happy,b. Bears are happy only if there are plentiful fish.c. Unhappy bears means that the fish are not plentiful and also that there is heavy snow.d. Unhappy bears is a necessary condition for heavy snow.e. The snow is heavy if and only if the fish are not plentiful,16. Using letters P, C, B, L for the component statements, translate the following compoundstatements into symbolic notation.a. If the project is finished soon, then the client will be happy and the bills will be paid.b. lf the bills are not paid, then the lights will go out.c. The project will be finished soon only if the lights do not go out.d. lf the bills are not paid and the lights go out, then the client will not be happy.e. The bills will be paid if and only if the project is finished soon, or else the lights go out.f. The bills will be paid if and only if either the project is finished soon or the lightsgo out.

Page 35: Mathematical Structures for Computer Science

sscnorv 1.1 sraremems, Symbolic Representation, and Tautnlngies 1717. Construct truth tables for the following wffs. Note any tautologies or contradictions.tra. (A—>B)<-»A’ VB*1:, (AAB)VC—>AA(BVC)c. A A (A' vB')'d. A A B ->A’e. (A->B)—»[(AVC)—>(BVC)]f. A —> (B —> A)g. A A B Q B’ V A'h. (A VB') A (A A B)'i. [(AvB)A C'] ->A' V C* 18, A memory chip from a microcomputer has 2** bistable (ON-OFF) memory elements.What is the total number of ON-OFF configurations?19. Verify the equivalences in the list on page 8 by constructing truth tables. (We have alreadyverified la. 4b. and 5a.) /20. Verify by constructing truth tables that the following wffs are tautologies.*a. A VA' e. (AvB)’~»A' AB' (De Morgan‘slaw)b. (A')' -»A f. (A A B)’ »A' V B' (De Morgan‘s law)*c,AAB->B g.AvA<->Ad. A —> A V B21. Prove the following tautologies by starting with the left side and finding a series of equiv-alent wffs that will convert the left side into the right side. You may use any of the equiva-lencies in the list on page 8 or in Exercise 20,a. (AAB')AC·-»(AAC)AB' d. (AAB')'VB<.»A'VBb. (AVB)A(AVB')<—A e.AA(AAB')'<—>AABc. A V (BAA')·->AVB* 22. Write a logical expression for a Web search engine to find sites pertaining to dogs that arenot retrievers.23. Write a logical expression for aWeb search engine to find sites pertaining to oil paintingsby Van Gogh or Rembrandt but not Vermeer.24. Write atlogical expression for a Web search engine to find sites pertaining to novels orplays about AIDS.25. Consider the following pseudocode,repeati = 1read a value for xii'((x < 5.0) and (2:: < 10.7)) or > 5.1) thenwrite the value of xend ifincrease i by 1nmtil i > 5The input values for x are 1.0, 5.1, 2.4. 7.2, and 5.3, What are the output values?26. Suppose that A, B, and C represent conditions that will be true or false when a certaincomputer program is executed. Suppose further that you want the program to carry out acertain task only when A or B is true (but not both) and C is false. Using A, B. and C andthe ccnnectives AND, OR, and NOT, write a statement that will be true only under theseconditions.

Page 36: Mathematical Structures for Computer Science

18 CHAPTER 1 hrmal Logic27. Rewrite the following statement form with a simpliiied conditional expression, where thefunction add(n) retums true if n is odd.if not((Value1 < Vulue 2) or odd(Number))or (nut(Va1uel < Value2) and odd(Number)) thenstatementlelsestatement2end if28. a. Verify thatA —> B is equivalent to A' V B.h. Using part (a) and other equivalences, write the negation of the statement "lf Sampassed his bar exam, then he will get the job."29. Use algorithm TautologyTest to prove that the following are tautologies.* a. [B' A (A —>B)] —>A'b. [(A —>B) AA] —>Bc. (A VE)AA' —>Bd. (AAB)AB'—>Ae. (A AB’) —> (A —>B)'30. In each case, construct compound wffs P and Q so that the given statement is a tautology.a. P A Qb. P —> P'c. P A (Q —> P')3l. From the truth table forA V B, the value ofA V B is true ifA is true, ifB is true, or if bothare true. This use of the word "or," where the result is true if both components are true. iscalled the inclusive ar. It is the inclusive or that is understood in the sentence "We mayhave rain or drizzle tomorrow." Another use of the word "or" in the English language isthe exclusive or, sometimes written XOR, in which the result is false when both compo-nents are true. The exclusive or is understood in the sentence {At the intersection, youshould tum north or south." Exclusive or is symbolized byA ® B.a. Write the truth table for the exclusive or.b. Show thatA (-3 B 4-» (A <-» B)’ is a tautology.32. Every compound statement is equivalent to a statement using only the connectives ofconjunction and negation, To see this, we need to find equivalent wffs for A V B and forA —> B that use only A and '. These new statements can replace, respectively, anyoccurrences of A V B and A —> B. (The connective <—> was defined in terrns of other con-nectives, so we already know that it can be replaced by a statement using these otherconnectives.)a. Show thatA V B is equivalent to (A' A B')'.b. Show thatA —> B is equivalent to (A A B')'.33. Show that every compound wff is equivalent to a wff using only the connectives ofa. V and 'b. —> and ’(Hint: See Exercise 32.)34. Prove that there are compound statements that are not equivalent to any statement usingonly the connectives -> and V.

Page 37: Mathematical Structures for Computer Science

sscncrv 1,1 statemems, symbolic nepresmmim., and raumlogies 19* 35, The binary connective i is defined by the following truth table: ’A B A\BT T F Show that every compound wff is equivalent to a wff using onlyT F T the connective (Him: Use Exercise 32 and find equivalentF T T statements forA A B and A' in terms of i .)F F T36. The binaiy connective l is defined by the following truth table:A is Ala-1- T F Show that every compound statement is equivalent to a state-T F F ment using only the connective l. (Hint: See Exercise 35.)F T FF F T37. Propositional wffs and tmth tables belong to a system of twa·valued logic because every-thing has one of two values, false or a·ue. Three·valued logic allows a third value of nullor "unknown" (Section 4.3 discusses the implications of three-valued logic on databases).The truth tables for this three-valued system follow.A B A A B A B A V B A A'T T T T T T T FT F F T F T F TT N N T N T N NF T F F T TF F F F F FF N F F N NN T N N T TN F F N F NN N N N N Na. Viewing N as "unknown." explain why it is reasonable to define T A N = N,FVN=N.andN'=N.Suppose that the statement "F1ight 237 is on time" is true, the statement "Runway condi-cions are icy" is false, and the truth value of the statement "l-Flight 51 is on time" isunknown. Find the cruth values of the following statements:b. Runway conditions are not icy and flight 51 is on time.c. Flight 51 is on time and ilight 237 is not.d. Flight 51 is not on time or runway conditions are not icy.

Page 38: Mathematical Structures for Computer Science

20 CHAPTER1 Furmallogic38. Propositional wffs and truth tables belong to a system of rwmvalued logic because every-thing has one of two values, F or T, which we can think of as 0 or 1. In fiugy logic. ormany-valued logic. statement letters are assigned values in a range between 0 and 1 toreflect some "probability" to which they are false or true. A statement letter with a truthvalue of 0.9 is "mostly true" or "has a high probability of being true" while a statementletter with a truth value of 0.05 "has a very high probability of being false." Fuzzy logic isused to manage decisions in many imprecise situations such as robotics, manufacturing.or instrument control. Truth values for compound statements are determined as follows:A' has the truth value 1 - A.A A B has the truth value that is the minimum of the values of A and of B.A V B has the truth value that is the maximum of the values of A and of B.a. Explain why these are reasonable assignments for the truth values of A’, A A B, andA V B.Suppose the statement "Flight 237 is on time" is estimated to have a truth value of 0.84,and the statement "Runway conditions are icy” is estimated to have a truth value of 0.12.Find the truth values of the following statements:b. Runway conditions are not icy.* c. Runway conditions are icy and Flight 237 is on time.d. Runway conditions are icy or Flight 237 is not on time.39. Four machines, A, B. C, and D. are connected on a computer network. It is feared that acomputer virus may have infected the network. Your security team makes the followingstatements:1. IfD is infected, then so is C.2. lf C is infected, then so is A.3, lf D is clean, then B is clean but C is infected.4. lf A is infected, then either B is infected or C is clean.Assuming that these statements are all true, what can you conclude?40. An advertisement for a restaurant at an exclusive club in Honolulu says, "Members andnonmembers only.“ Give two possible interpretations of this statement.41. The following newspaper headline was printed during a murder trial:"I nm n liar" says murder defemianx!Can the jury reach any conclusion from this statement?In Exercises 42414, you are traveling in a certain country where every inhabitant is either atruthteller who always tells the truth or a liar who always lies}* 42. You meet two of the inhabitants of this country, Percival and Llewellyn. Percival says, "Atleast one of us is a liar." ls Percival a Liar or a truthteller? What about Llewellyn? Explainyour answer.` 2For more puzzles about **knlglits" and ··knaver," see What ir the Nam: afmr Bock? by the 1¤gl¤ian~andmagician-Raymond Smullyan (Prentice-t-tau, l97H).

Page 39: Mathematical Structures for Computer Science

sscnorv 1,2 rmposirianal Logic 2143. Traveling on, you meet Merlin and Meredith. Merlin says, "If I am a truthtellergthenMeredith is a truthteller." Is Merlin a liar or a truthteller'? What about Meredith? Explainyour answer.44. Finally, you meet Rothwold and Grymlin. Rothwold says, "Either I am a liar or Grymlinis a truthteller." Is Rothwold a liar or a truthteller? What about Grymlin? Explain youranswer.45. In 2003, then U.S. Secretary of Defense Donald Rumsfeld won Britain’s Plain EnglishCampaign 2003 Golden Bull Award for this statement: "Reports that say that somethinghasn‘t happened are always interesting to me, because as we know, there are knownknowns, there are things we know we know. We also know there are known unknowns;that is to say we know there are some things we do not know. But there are also unknownunknowns—the ones we don’t know we don't know.“ What possibility did SecretaryRumsfeld omit'!SECTION 1.2 Propositional Logic

The argument of the defense attomey at the beginning of this chapter made a number of (sup-posedly true) statements and then asked the jury to draw a specific conclusion based on thosestatements. Ln Section l.l, we used the notation of formal logic to represent statements in sym-bolic fonn as wffs; because statements are sometimes called propositions, these wfis are alsocalled propusltional wffs. Now we want to use tools from formal logic to see how to reachlogical conclusions based on given statements. The formal system that uses propositional wffsis called propositionul logic, statement logic, or propusitional calculus. (The word calculusis used here in tl1e more general sense of "caleulat.ion" or "reasoning," not "differentiating" or"integrating.")Valid ArgumentsAn argument can be represented in symbolic form asP, AP,/tP3A·--APn—>Qwhere P1, P2, ,P_ are tl1e given statements, called the hypotheses of the argument, and Q isthe conclusion of the argument. As usual, the P‘s and the Q represent wffs, not merely state-ment letters. When should this be considered a valid argument? This question can be stated inseveral equivalent ways:• When can Q be logically deduced from P1, , PH'?• When is Q a logical conclusion from Pl, , PH'?• When does Pl, , Pn logically imply Q7• When does Qfollow logicallyfmm P`, , P"?and so forth.An informal answer is that Q is a logical conclusion from P1, , PK whenever the truth ofPl, , Pu implies the truth of Q. In other words. when the implicationPIAPZAP3/\···APn—>Q

Page 40: Mathematical Structures for Computer Science

22 CHAPTER1 rarmaltogicis true. (Of course, this implication is true if any of the hypotheses is false, but in an argumentwe usually care about what happens when all the hypotheses are true.) Furthermore, this impli-cation should be tme based on the relationship of the conclusion to the hypotheses. not on anyincidental knowledge we may happen to have about Q.EXAMPLE 9 Consider the following argument:George Washington wu the first president of the United States. Thomas Jefferson wrote theDeclaration of Independence. Therefore, every day has 24 hours.This argument has the two hypothesesl. George Washington was the first president of the United States.2. Thomas Jefferson wrote the Declaration of Independence.and the conclusionEvery day has 24 hours.Even though each of the individual hypotheses. as well as the conclusion, is a true statement,we would not consider this argument valid. The conclusion is merely an isolated true fact, notat all related to or "following from" the hypotheses. •A valid argument should therefore be true based entirely on its intemal structure; it shouldbe "intxinsically t:rue." Therefore we make the following fonnal definition.The argument in Example 9 would be symbolized as~ A A B -> Cwhich is clearly not a tautology.EXAMPLE 10 Consider the following argument.lf George Washington was the Hrst president of the United States, then John Adams was thefirst vice president. George Washington was the first president of the United States. ThereforeJohn Adams was the 6.rst vice president.

Page 41: Mathematical Structures for Computer Science

SECTION 1.2 Pruposiliunal logic 23This argument has the two hypotheses `l. lf George Washington was the first president of the United States, then John Adams wasthe first vice president.2. George Washington was the first president of the United States.and the conclusionJohn Adams was the First vice president.A symbolic representation of this argument has the form(A -> B) A A —> BA truth table or algorithm Tuu:ol0gyTest establishes that this argument is a tautology. The argu-ment is valid; its form is such that the conclusion follows inevitably from the hypotheses. Infact, this form of argument. known by its Latin name of modus panenr ("method of assertion"),is one of the rules of reasoning we will use to build propositional logic. •To test whether P, A P, A P3 A A Pu —> Q is a taurology. we could build a mrth table oruse algorithm TauralogyTesr. Instead, we will tum to formal logic, which uses a system of der-ivation rules that manipulate wffs in a truth-preserving manner. You begin with tbe hypothesesP`, , Pn (assumed urue) and attempt to apply the manipulation rules in such a way as to end upwith the conclusion Q (which must then also be true because truth is preserved under the rules).Using formal logic to prove that Q is a valid conclusion from Pl, , P". we must produce aproof sequence of the formP, (hypothesis)P2 (hypothesis)Pn I (hypothesis)wff, (obtained by applying a derivation rule to earlier wffs)wffz (obtained by applying a derivation rule to earlier wffs)Q (obtained by applying a derivation rule to earlier wffs)The derivation mles for a formal system must be carefully chosen. lf they are too powerful,then they won’t be truth-preserving and we'll be able to deduce anything at all from a given setof hypotheses. lf they are too weak, there will be logical conclusions that we won‘t be able to

Page 42: Mathematical Structures for Computer Science

\24 CHAPTER 1 Formal Logicprove from given hypotheses. We want a formal logic system that is correct (only valid argu-ments should be provable) and complete (every valid argument should be provable). In addition,the derivation rules should be kept to a minimum in order to make the formal system manage-able. We would like the system to have the smallest set of rules that still allows it to be complete.Derivation Rules for Propositional LogicThe derivation mles for propositional logic fall into two categories, equivalence rules andinference rules. Equivalence rules allow individual wffs to be rewritten, while inference rulesallow new wffs to be derived from previous wffs in the proof sequence.Equlvalence rules state that certain pairs of wffs R and S are equivalent. Remember fromSection 1.1 that R ev S means that R ·» S is a tautology and that S can be substituted for R in anywff with no change to its truth values. Equivalence rules are therefore truth-preserving; a truewff remains true if such a substitution is done within it.Table 1.12 lists the equivalence rules we will use in our formal system for propositionallogic. (Additional rules could be formulated based on other tautologies, but we are trying tokeep our nile set to a minimum.) Each is given a name to make it easier to identify its use in aproof sequence. We saw the commutadve and associative rules, as well as De Morgan’s laws,in Section 1.1. There they were given for statement letters only, here they are given for any wffsP, Q, R, but they are still tautologies.TABLE 1.12

Equivalence RulesExpression Equivalent to Name/Abbreviation for RuleP v Q Q v P Commutative—commP A Q Q A P(P V Q) V R P V (Q V R) Associative·—ass(PAQ)AR PA(QAR)(P V Q)’ P’ A Q' De Morgan’s laws—~De Morgan(P A Q)’ P' v Q'YP —> Q P' V Q lmplication—impP (P')' Double negation-dnP 4—» Q (P —> Q) A (Q —> P) Definition of equivalence—equProve me impmaaaa ana; that is. prove that<P—>Q)~·(P' V Q)is a tautology. •EXAMPLE 1 ’| Suppose that one hypothesis of a propositiooal argument can be symbolizedasK (A' v B’) v C

Page 43: Mathematical Structures for Computer Science

SECTION 1.2 rmpasizimtal logic 25Then a proof sequence for the argument could begin with the following steps: »1. (A' V B') V C hyp (hypothesis)2. (A A B)' V C 1, De Morgan3. (AAB)—>C 2,impThe justification given for each step is not a required part of the proof sequence, but it does con-firm that the step is a legitimate one. Step 1 is a hypothesis. Step 2 is derived from step 1 byapplying one of De Morgan's laws. Step 3 is derived from step 2 by using the Implication rulethat P —> Q is equivalent to P' V Q, where P is the wff A A B, and Q is the wff C. •The equivalence rules allow substitution in either direction. 'l'hat is, in Example 11 wereplaced A' v B’ with (A A B)', but in some other proof sequence, using the same rule, wemight replace (A A B)’ with A' V B'.Inference rules say that if one or more wffs that match the first part of the rule pattem arealready part of the proof sequence, we can add to the proof sequence a new wff that matches thelast part of the rule pattem. Table 1.13 shows the propositional inference rules we will use,again along with their identifying names.TABLE 1.13

Inference RulesFrom Can Derive Name/Abbreviation for Rule '*°· P ·> Q m Mm vom-mpP —> Q, Q' P' Modus tollens—mtP, Q P A Q Conjunction—conP A Q P, Q Simplification—simP P V Q Addition—addUnlike equivalence rules, inference rules do not work in both directions. We cannot"reverse" the addition rule in Table 1.13; from P V Q, we cannot infer either P or Q.EXAMPLE 12 Suppose that A -> (B A C) and A are two hypotheses of an argument. Aproof sequence for the argument could begin with the following steps:1. A -> (B A C) hyp2. A hyp3. B A C 1, 2, mpThe justification at step 3 is that steps 1 and 2 exactly match the pattern required for modusponens, where P is A and Q is B A C. Modus ponens ("method of assertion" in Latin) says thatQ can be derived from P and P —> Q. •

Page 44: Mathematical Structures for Computer Science

26 CHAPTER 1 Formal LogicGive a next step and a justification for a proof sequence that begins1. (AAB’)—>C hyp2. C ’ hyp •The inference rules are also truth-preserving. For example, suppose that P and P -> Q areboth true wffs in a proof sequence. Then Q is deducible from these two wffs by modus ponens.I.fP and P —> Q are both true, then—by the t.ruth table for implication—Q is also true.The derivation rules represent recipes or patterns for transforming wffs, A rule can beapplied only when the wffs exactly match the pattem.EXAMPLE 13 Suppose that (A —> B) V C and A are two hypotheses of an argument. AI ""`· proof sequence for the argument could begin with the following steps:1. (A -> B) v c hypwwmlw, 2. A hyp lM Ms °m°"" Unlike Example 12, however, nothing further can be done. Modus ponens requires the presenceof wffs matching the pattem P and P —-> Q. In P —> Q, the main connective is an implication.The wff (A —» B) v C has disjunction, not implication, as its main connective Modus ponensdoes not apply. •Now we are ready to work our way through a complete proof of an argument.EXAMPLE 1 4 Using propositional logic, prove that the argumentA/x(B-»C)A[(AAB)—>(DvC')]AB—>Dis valid.We must produce a proof sequence that begins with the hypotheses and ends with the con-clusion. There are four hypotheses, so this gives us lots of “ammunition" to use in the proof.The beginning of the proof is easy enough because it just involves listing the hypotheses:1. A hyp2. B —> C hyp3. (AAB)->(DvC') hyp4. B hypOur final goal is to arrive at D, the conclusion. But without even looking ahead, there are a cou-ple of fairly obvious steps we can take that may or may not be helpful.5. C 2, 4, mp6. A A B 1, 4, con ·7. D V C' 3, 6, mpAt least at this point we have introduced D, but it‘s not by itself. Note that from step 5 we» have C, which we haven't made use of. I.f only we had C —> D, we’d be home free. Ah, look at

Page 45: Mathematical Structures for Computer Science

SECTION 1.2 Pmposiiinnal logic 27the form of step 7; it’s a disjunction, and the implication rule says that we can transform a dis-junction of a certain form into an implication. The disjunction must have a negated wff on theleft. We can do that:8. C" v D 7, comm9. C —> D 8, impso10. D 5, 9, mp •As in Example 14, proof sequences involve a certain amount of rewriting just because youcan and a certain amount of keeping an eye on the desired goal and what it would take to getthere. Although not as mechanical as constructing a truth table, the strict rules of the gamenevertheless provide a more or less mechanical way to construct the proof sequence. There areonly a certain number of legitimate things that can be done at any one point in the sequence. ].fone choice seems to lead down a blind alley, go back and take another. Also, there may be morethan one correct proof sequence; as a relatively trivial instance. steps 6 and 7 could have beendone before step 5 in Example 14. `* Using propositional logic, prove the validity of the argnrment[(AvB’)—>C]A(C—>D)AA—>D •Deduction Method and Other Rules2 Suppose the argument we seek to prove has the form_ P,/xPZAP3A···APn->(R—>S)where the conclusion is itself an implication. Instead of using Pl, , Pn as the hypotheses andQi deriving R —> S, the deduction method lets us add R as an additional hypothesis and then deriveS. In other words, we can instead provemnmpinmnor PIAPZAPB/\··-AP"/xR—>S

Page 46: Mathematical Structures for Computer Science

28 CHAPTER 1 Formal logicThis is to our advantage because it gives us one more hypothesis, i.e., additional ammunitionfor the proof, and it simplifies the desired conclusion.The deduction method approach agrees with our understanding of implication, but Exercise 49at the end of this section provides a forma] justification.EXAMPLE 1 5 Use propositional logic to prove[A—>(A+B)] +(A—>B)Using the deduction method, we get two hypotheses instead of one, and we want to derive B.1. A -> (A -·> B) hyp2. A hyp3. A —> B l, 2, mp4. B 2. 3, mp •Q ,._. Q_ ___. f ;;§[_.g,, Use propositional logic to prove(A—>B)A(B—>C)—>(A—>C) •The formal system we have described is correct and complete. Every argument we can prove isa tautology (the system is correct), and every implication that is a tautology is provable (thesystem is complete). We can easily argnie for correctness because each of the derivation rules istruth-preserving. Completeness would be more difficult to prove, and we will not do so.Correctness and completeness say that the ser of derivation rules we have used is exactlyright—not too strong, not too weak. Nonetheless. many formal systems for propositional logicuse additional t.tut.h·preserving inference rules. We can prove these additional niles using ouroriginal rule set. Once such a rule is proved. it can be used as justification in a proof sequencebecause, if required, the single step invoking this rule could be replaced with the proofsequence for the mle. Nothing more can be proved by the addition of these rules. but the proofsequences might be shorter. (See Exercises 1.2 for a list of additional rules,)EXAMPLE 16 The rule of hypothetical syllogism (hs) isFrom P —> Q and Q —> R, one can derive P —> R.This rule is making the claim that(P->Q)A(Q-»R)—·>(P—»R)is a valid argument. The proof sequence for this argument looks just like that for Practice 12.Because it is a legitimate derivation rule, hypothetical syllogism can be used to justify a step ina proof sequence. •EXAMPLE 1 7 Use propositional logic to prove(A’ vB)A(B—>C)—>(A—>C)

Page 47: Mathematical Structures for Computer Science

sscnorv 1.2 Pruposiliunal Logic 29The following proof sequence will do. ·1. A' v B hyp2. B —> C hyp3. A —> B l. imp4. A —> C 2, 3, hsWithout use of the new rule, we could still have produced a proof sequence by essentially prov-ing the new rule as part of this proof:l, A' V B hyp2. B —> C hyp3. A —> B l, imp4. A hyp5, B 3, 4. mp6. C 2,5, mpAdditional rules thus can shorten proof sequences but at the expense of having to rememberadditional rules! •5 Prove(A—>B)A(C'vA)AC—>B •Verbal ArgumentsAn argument in English (an attomey’s trial summary, an advertisement, or a political speech)that consists of simple statements can be tested for validity by a two-step process.t. Symbolize the argument using propositional wffs.2. Prove that the argument is valid by constructing a proof sequence for it using the deriva-tion rules for propositional logic.EXAMPLE 1 8 Consider the argument "If interest rates drop, the housing market willimprove. Either the federal discount rate will drop or the housing market will not improve,Interest rates will drop. Therefore the federal discount rate will drop." UsingI Interest rates drop.H The housing market will improve.F The federal discount rate will drop.the argument is(l—>H)A(FvH')Al—>F

Page 48: Mathematical Structures for Computer Science

so CHAPTER 1 Formal LogicA proof sequence to establish validity is1. I -> H hyp2. F v H' hyp3. I hyp4. H' V F 2, comm5. H —> F 4, imp6. I -> F 1, 5. hs7. F 3, 6, mp •EXAMPLE 19 Is the following argument valid'] "My client is left-handed, but if the diaryis not missing, then my client is not left-handed; therefore, the diary is missing." There are onlytwo simple statements involved here, so we symbolize them as follows:L My client is left-handed.D The diary is missing.The argument is thenL A (D' —> L') —» DThe validity of the argument is established by the following proof sequence.1. L hyp2. D' —» L' hyp3. (D')' V L’ 2, imp4. D v L’ 3. dn5. L' V D 4, comm6, L -> D 5, imp7. D l, 6, mpThe argument says that yf the hypotheses are true, then the conclusion will be true. The validityof tl1e argument is a function only of its logical form and has nothing to do with the actual truthof any of its components. We still have no idea about whether the diary is really missing. Fur·thermore, the argument "Skooses are pink, but if Gingoos does not like perskees, then skoosesare not pink; therefore Gingoos does like perskees," which has the same logical form, is alsovalid, even though it does not make sense. •i;`,i.,*i"i`;ZQi`:}` Z` Q, ,,... Use propositional logic to prove that the following argument is valid. Usestatement letters S, R. and B: "If security is a problem. then regulation will be increased. Ifsecurity is not a problem, then business on the Web will grow. Therefore if regulation is notincreased, then business on the Web will grow." •

Page 49: Mathematical Structures for Computer Science

SECIION 1,2 Propositimral logic 31Formal logic is not necessary to prove the validity of propositional arguments. A valid argu-ment is represented by a tautology, and truth tables provide a mechanical lest for whether a wff isa tautology. So what was the point of all of this? In the next section we will see that propositionalwffs are not suflicient to represent everything we would like to say, and we will devise new wffscalled predicate wjb, There is no mechanical test for the prezhcate wE analogue of tautology. andin the absence of such a test, we will have to rely on formal logic to justify arguments. We havedeveloped formal logic for propositional arguments as a son of dry run for the predicate case.In addition. the sort of reasoning we have used in propositional logic carries over into every-day life. It is the foundation for logical thinking in computer science, mathematics. the court-room, the marketplace, the laboratory. Although we have approached logic as a mechanicalsystem of applying rules, enough practice should ingrain this way of thinking so that you nolonger need to consult tables of rules. but can draw logical conclusions and recognize invalidarguments on your own.SECTION 1.2 Review~ TECHNIQUESW • Apply derivation rules for propositional logic.W • Use propositional logic to prove the validity of a verbal argument.MAIN IDEAS• A valid argument can be represented by a wff of the forrn Pl A P2 A P3 A APu —> Q that is a tautology.• A proof sequence in a formal logic system is a sequence of wffs that are eitherhypotheses or derived from earlier wffs in the sequence by the derivation rules ofthe system.• The propositional logic system is complete and correct; valid arguments and onlyvalid arguments are provable.EXERCISES 1.2For Exercises l-4, what inference rule is illustrated by the argument given?t l. If Martina is the author, then the book is fiction. But the book is nonfiction. ThereforeMartina is not the author.2. If the business declares bankruptcy, then all assets must be confiscated. The businessdeclared bankruptcy, It follows that all assets must be confiscated.3. The dog has a shiny coat and loves to bark. Consequently. the dog loves to bark.4. If Paul is a good swimmer, then he is a good runner. IfPaul is a good runner, then he is agood biker. Therefore. if Paul is a good swimmer, then he is a good biker.For Exercises 5-8. decide what conclusion, if any. can be reached from the given hypothesesand justify your answer.5, If the car was involved in the hit-and-run, then the paint would be chipped. But the paintis not chipped.

Page 50: Mathematical Structures for Computer Science

32 cHAPTEn 1 Formal Aggie6. Either the weather will tum bad or we will leave on time. If the weather tums bad, thenthe flight may be cancelled.it 7. If the bill was sent today. then you will be paid tomorrow. You will be paid tomorrow.S. The grass needs mowing and the trees need trimming. lf the grass needs mowing. then weneed to rake the leaves. /9. Justify each step in the proof sequence ofAA(B—>C)—>(B->(AAC))1. A2. B —> C3. B4. C5. A A C* 10. Justify each step in the proof sequence of[A—>(BVC)] AB' AC/—>A’1. A -> (B v C)2. al3. C'4. B' A C'5. (B v C)'6. A'11. Justify each step in the proof sequence ofA’ABA[B—>(AvC)]—>C1. A'2. B3. B —> (A V C)4. A v C5. (A')' v C‘6. A' —> C7. CIn Exercises l2-22, use propositional logic to prove that the argmment is valid.12. A' A (B —>A) -+B'ik 13. (A—>B)A[A—>(B->C)]—>(A—>C)14. [(C->D) -> C] -> [(C—>D) —>D]*15. A’A(AVB)—>B16. [A->(B—>(,')]A(AvD')AB—»(D—>C)17. (A'—>B')ABA(A—>C)—>C18. (A—>B)A[B—>(C—>D)]A[A—>(B—>C)]—>(A—>D)19.[A—>(B—>C)]—>[B—>(A—>C)] ‘20. (A A B) —> (A —> B')’21. (A—>C)A(C—>B')AB—>A'22. [A—g(BvC)] AC’—>(A—>B)

Page 51: Mathematical Structures for Computer Science

sscnow 1.2 Pmpositiunalicgic aaUse prcpositional logic to prove the validity of the argumenu in Exercises 23-31. These willbecome additional derivacion rules for propositiorml logic, summarized in Table 1,14.TABLE 1,14More Inference Rules` From Can Derive Name/Abbreviation for RuleP Q Q, Q Q R P Q R [Example 16] HypoLhedca1syl1ogism—hsP v Q, P' Q [Exercise 23] Disjuncrive syll0gism—dsP Q Q Q' Q P' [Exercise 24] Cont.raposition—c¤ntQ' Q P’ P —> Q [Exercise 25] Contraposition—contP P A P [Exercise 26] Self-reference—seLfP V P P [Exercise 27] Self-reference—se1f(P A Q) Q R P Q (Q Q R) Exportation—exp[Exercise 28]P. P’ Q [Exercise 29] Inconsistency—incP A (Q V R) (P A Q) V (P A R) Distributive—dist[Exercise 30]P V (Q A R) (P V Q) A (P V R) Distributive—dist[Exercise 31]*23. (PVQ)AP'QQ24. (P Q Q) Q (Q' Q P')25. (Q' —> P') Q (P Q Q)26. P Q P A P27. P V P Q P (Him: Instead of assuming the hypothesis, begin with a version ofExercise 26; also make use of Exercise 25.)28. [(P A Q) QR] Q [P Q(Q —>R)]* 29. P A P' Q Q30. P A (Q v R) Q (P A Q) V (P A R) (Him.- First rewrite the conclusion.)31. PV (Q AR) Q (Pv Q) A (P V R) (Him.- ProvebothP V (Q A R) Q(PV Q) andP V (Q A R) Q (P v R): for each proof. first rewrite me conclusion,)For Exercises 32-39, use propositional logic to prove the arguments valid; you may use anyof the rules in Table 1.14 or any previously proved exercise.32. A' Q (A Q B)*33. (P—>Q)A(P'->Q)QQ34. (A’QB')A(AQC)Q(BQC)35. (A' QB)A (BQC)A (CQD)—>(A' QD)36. (AVB)A(AQC)A(BQC)QC

Page 52: Mathematical Structures for Computer Science

34 CHAPTER 1 Formal logic37. (Y—>Z’) A(X' —>Y) A [Y—>(X—> W)] A(Y—>Z)—»(Y—>W)it 38. (A A B)’ A (C' AA)’ A (CAB')' —>A’39. (Pv (QAR))A(R' vS) A(S—>T')—>(T—>P)Using propositional logic, including the rules in Table 1.14. prove that each argument inExercises 40-48 is valid. Use the statement letters shown.40. If the program is efficient, it executes quickly. Either the program is efficient, or it hasa bug. However, the program does not execute quickly. Therefore it has a bug.E. Q. B41. If Jane is more popular, then she will be elected. If Jane is more popular, then Craigwill resign. Therefore if Jane is more popular, she will be elected and Craig will resign.J, E, C42. If chicken is on the menu, then don’t order fish. but you should have either fish or salad.So if chicken is on the menu, have salad. C, E S43. The crop is good. but there is not enough water. If there is a lot of rain or not a lot ofsun, then there is enough water. Therefore the crop is good and there is a lot of sun.C, WZ R, S44. If the ad is successful, then the sales volume will go up. Either the ad is successful or thestore will close. The sales volume will not go up. Therefore the store will close.A, S, Cir 45. Russia was a superior power, and either France was not strong or Napoleon made anerror. Napoleon did not make an error, but if the army did not fail, then France wasstrong. Hence the army failed and Russia was a superior power. R, F, N, A46. It is not the case that if electric rates go up, then usage will go down, nor is it true thateither new power plants will be built or bills will not be late. Therefore usage will not godown and bills will be late. R, U, P, B47. If Jose took the jewelry or Mrs. Krasov lied, then a crime was committed. Mr. Krasovwas not in town. If a crime was committed, then Mr. Krasov was in town. Therefore Josedid not take the jewelry. J, L, C, T48. lfthe birds are flying south and the leaves are tuming, then it must be fall. Fall bringscold weather. The leaves are turning but the weather is not cold. Therefore the bi.rds arenot flying south. B, L, F, Ct 49. a. Use a tnrth table to verify that A-> (B —> C) ·» (A A B) —> C is a tautclogy.b. Prove thatA —> (B —> C) ·: (A A B) ·-> C by using a series of equivalences.c. Explain how this equivalence justifies the deduction method that says:to prove P, A P, A A Pn —> (R —> S), deduce Sfrom P1, P2, , P", and R.50. The argument of the defense attorney at the beginni.r1g of this chapter waslf my client is grilty, then the knife was in the drawer. Either the knife was not in thedrawer or Jason Pritchard saw the knife. lf the knife was not there on October 10, it followsthat Jason Pritchard didn't see the knife. Furthermore, if the knife was there on October 10,then the knife was in the drawer and also the hammer was in the ham. But we all know thatthe hammer was not in the barn. Therefore, ladies and gentlemen of the jufy. my clientis innocent.Use propositional logic to prove that this is a valid argument.

Page 53: Mathematical Structures for Computer Science

sscnclv 1,2 Quanlifiers, Predicaees, and Validity 35SECTION 1.3 Quantifiers, Pred icates, and Validity ·

Quantifiers and PredicatesPropositional wffs have rather limited expressive power. For example, we would consider thesentence "For every x, x > O" to be a true statement about the positive integers, yet it cannot beadequately symbolized using only statement letters, parentheses, and logical connectives. Itcontains two new features. a quanryier and a predicate. Quantifiers are phrases such as "forevery" or "for each" or "for some" that tell in some sense how many objects have a certainproperty. The universal quantlfler is symbolized by an upside down A, V, and is read "for all,""for every," "for each," or "for any.“ Thus the example sentence can be symbolized by(Vx)(x > O)A quantifier and its named variable are always placed in parentheses. The second set of paren-theses shows that the quantifier acts on the enclosed expression. which in this case is "x > O,"The phrase *5r > 0" describes a property of the variable x. that of being positive. A propertyis also called a predicate; the notadon P(x) is used to represent some unspecified predicate orproperty that x may have. Thus. our original sentence is an example of the more general form(Vx)P(x)The truth value of the expression (Vx)(x > 0) depends on the domain of objects in which weare "interpreting" this expression, that is. the collection of objects from which x may be chosen.This collection of objects is called the domain cfinrerpretarion. We have already agreed that ifthe domain of interpretation consists of the positive integers, the expression has the u·uth valuetrue because every possible value for x has the required property of being greater than zero. Ifthe domain of interpretation consists of all the integers, the expression has the truth value false,because not every x has the required property. We impose the condition that the domain ofinterpretation contain at least one object so that we are not talking about a trivial case.An interpretation of the expression (Vx)P(x) would consist of not only the collection ofobjects from which x could take its value but also the particular property that P(x) represents inthis domain. Thus an interpretation for (Vx)P(x) could be the following: The domain consists ofall the books in your local library, and P(x) is the property that x has a red cover. In this inter-pretation. (Vx)P(x) says tl1at every book in your local library has a red cover. The truth value ofthis expression, in this interpretation, is undoubtedly false,What is the truth value of the expression (V.r)P(x) in each of the followinginterpretations?a. P0:) is the property that x is yellow, and the domain of interpretation is the collection ofall buttercups.b. P(x) is the property that x is yellow, and the domain of interpretation is the collection ofall flowers.c. P(x) is the property that x is a plant. and the domain of interpretation is the collection ofall flowers.d. P(x) is the property that x is either positive or negative, and the domain of interpretationconsists of the integers. •

Page 54: Mathematical Structures for Computer Science

36 CHAPTER 1 rmmallogicThe existential quantiher is symbolized by a backward E, 3, and is read "there exists one,""for at least one," or "for some." Thus the expression(Elrxx > 0). is read "There exists an x such thatx is greater than zero."Again, the truth value of this expression depends on the interpretation. If the domain ofinterpretation contains a positive number, the expression has the value true; otherwise, it has thevalue false. The truth value of (Ex)P(x), if the domain consists of all the books in your locallibrary and P(x) is the property that r has a red cover, is true if there is at least one book in the""‘“°°"’ library with a red cover.a. Construct an interpretation (i.e., give the domain and the meaning of P(x)) in which(Vx)P(x) has the value true.b. Construct an interpretation in which (Vx)P(x) has the value false.c. Can you find one interpretation in which both (Vx)P(x) is true and (Ex)P(x) is false?d. Can you find one interpretation in which both (Vx)P(x) is false and (Hx)P(x) is true? •The predicates we have seen so far, involving properties of a single variable, are unary predi-cates. Predicates can be binary, involving properties of two variables, temary, involving prop-erties of three variables, or, more generally. n-ary, involving properties of rz variables.EXAMPLE Z0 The expression (Vx)(Ey)Q(x, y) is read "For every x there exists a y suchthat Q(x, y)." Note that there are two quantitiers for the two variables of the binary property.In the interpretation where the domain consists of the integers and Q(X. Y) is the property thatx < y, this just says that for any integer, there is a larger integer. The truth value ofthe expres-sion is true. In the same interpretation, the expression (Ey)(Vx)Q(x, y) says that there is a singleinteger y that is larger than any integer x. The truth value here is false, •Example 20 illustrates that the order in which the quantitiers appear is important.In expressions such as (Vx)P(x) or (Ex)P(x), it is a dummy variable; that is, the truth valuesof the expressions remain the same in a given interpretation if they are written, say, as (Vy)P(y)or (E;)P(z), respectively. Similarly, the truth value of (Vx)(Ely)Q(x, y) is the same as that of(V;)(Ew)Q(r, w) in any interpretation. However, (Vx)(Ex)Q(x, x) says something quite differ-ent. In the interpretation of Example 20, for instance, (Vx)(Ex)Q(x, x) says that for every inte-ger x, there is an integer x such that x < x. This statement is false, even though (Vx)(3y)Q(x, y)was true in this interpretation. We cannot collapse separate variables together into one withoutchanging the nature of the expression we obtain.Constants are also allowed in expressions. A constant symbol (a, b, c, 0, l, 2, etc.) is inter-preted as some specific object in the domain. This specification is part of the interpretation. Forexample, the expression (Vx)Q(x, a) is false in the interpretation where the domain consists ofthe integers, Q(x. y) is the property x < y, and a is assigned the value 7; it is not the case thatevery integer is less than 7.Now we can sum up what is required in an interpretation.

Page 55: Mathematical Structures for Computer Science

srcnou 1.2 Quamirim, Predicares, and validiry 37Expressions can he built by combining predicates with quantiiiers, grouping symbols(parentheses or brackets), and the logical connectives of Section 1.1. As before, an expressionmust obey rules of syntax to be considered a well-forrned formula. Well-fonned formulas con-taining predicates and quantiiiers are called predicate wffs to distinguish them from proposi-tional wffs. which contain only statement letters and logical connectives.The expression P(x)(Vx) A)3y is not a well-formed formula, Examples of predicate wEs areF(¤<) V QU) (1)(Vx)[P(x) —> Q(x)l (2)(Vx>((E|y)lP(x. y) A Q(x. y)l -> R(x)) (3)and(El¤¤)S(x) V (Vy)T(y) (4)"Grouping symbols" such as parentheses and brackets identify the scope of a quantifier, thesection of the wff to which the quantiiier applies. (This is analogous to the scope of an identi-frer in a computer program as the section of the program in which that identiiier has meaning.)There are no quantifiers in wff (1). In (2), the scope of the quantiher (Vx) is P(x) —> Q(x).In (3), the scope of (By) is P(x, y) A Q(x, y), while the scope of (Vx) is the entire expression inparentheses following it. ln (4), the scope of (Elx) is S(x) and the scope of (Vy) is T (y); paren-theses or brackets can be eliminated when the scope is clear.If a variable occurs somewhere in a wff where it is not part of a quantifier and is not withinthe scope of a quantitier involving that variable, it is called a free variable. For example, y is afree variable in(Vx)lQ(><. y) —> (3y)R(x. y)lbecause of the first occurrence of y, which is neither the variable of a quantifrer nor within thescope of a quantiiier using y. A wff with free variables may not have a truth value at all in agiven interpretation, For example, in the interpretation where the domain is all of the integers,the predicate P(x) means "x > 0," and 5 means (of course) the integer 5, the wffP(y) A P(5)has no truth value because we don‘t know which element of the domain y refers to. Some ele-ments of the domain are positive and others are not. The wffP(y) V P(5)

Page 56: Mathematical Structures for Computer Science

38 cuavteai Formal rngiris true in this interpretation even though we don't know what y refers to because P(5) is true. Inboth of these wffs y is a free variable,EXAMPLE 21 In the wif(Vx)Gy>[S(x. y> A L(y. ¤)lthe scope of (By) is all of S(x, y) A L(y, u). The scope of (Vx) is (3y)[S(x, y) A L(y, a)]. Considerthe interpretation where the domain consists of all the cities in the United States.S(x, y) is the property '1 and y are in the same state," L(y. z) is the property "y‘s name beginswith the same letter as z`s name," and u is assigned the value Albuquerque. So the interpretationof the entire wff is that for any city 1 there is a city y in the same state that begins wirh tl·reletter A. The wff is true in this interpretation. (At least it is true if every state has a city begin-ning with the letter A.) •who is me mini value ofthe wff<El><>(A<>¤> A (Vy)[B(x. y) —> C<y>l>in the interpretation where the domain consists of all integers, A(x) is **1 > 0," B(x, y) is**1 > y," and C(y) is ‘*y S 0"7 Construct another interpretation wit1·r the same domain in whichthe statement has the opposite truth value. •TranslationMany English language statements can be expressed as predicate wffs, For example, "Everyparrot is ugly" is really saying "For any thing, if it is a parrot, then it is ugly.`* Letting Pot)denote ‘U¢ is a parrot" and U(x) denote **1 is ugly," tl·re statement can be symbolized as(V¤¤>lF(»¥) r i/(xl]Other English language variations that take tl·re same symbolic form are *2411 parrots are ugly'*and "Each parrot is ugly." Notice tl·rat the quantifrer is the universal quantifrer and the logicalconnective is implication; V and —> almost always belong together. The wff (V1)[P(x) A U(1)]is an incorrect translation because it says that everything in the doma.in—understood here tobe the whole world—is a.n ugly parrot. This says something much stronger than the originalEnglish statement.Similarly, "There is an ugly parrot" is really saying **There exists something that is both aparrot and ugly." In symbolic form,(ilx>[P(¤r) A U(><)lVariations are ‘*Some parrots are ugly" and "'l'here are ugly pa.rrots." Here the quantifrer is theyr `" existential uantitier and the logical connective is conjunction; E and A almost always belong..r.t - .· 9together. The wff (E|1)[P(1) —> U(1)] is an incorrect translation. 'I’l·ris wff is true as long as thereis anything, call it 1, in the domain (the whole world) that is not a parrot, because then P(x) is° wi false and the implicadon is true. Indeed, this wff is true if there are no parrots in the world at all!S" To translate an English statement into a wff. it may help to iirst write some intermediateEnglish language statement and then symbolize that statement. We did this with the parrotr examples.

Page 57: Mathematical Structures for Computer Science

SECTION 1.3 Quantifiers, Predicates, and Validity 35The word "only" seems particularly troublesome in translations because its placement in asentence can completely change the meaning. For example. the English statementsJohn loves only Mary.Only John loves Mary.John only loves Mary.say three entirely different things. They can be rewritten asIf John loves any thing, then that thing is Mary,If any thing loves Mary. then that thing is John.If John does any thing to Mary. then that thing is love.These sentences are all of the "if-then” form, which means that they will be represented by awff using an implication. In each case, the consequent of the implication is the word following"only" in the original English statement.EXAMPLE 22 Given the predicate symbolsD(x) is "x is a dog."R(x) is "x is a rabbit."C(x, y) is "x chases y"Table 1.15 shows examples of an English statement, an intermediate English statement, and awff translation. Note that in wff 2, the connective associated with E is A and the connectiveassociated with V is —>. In wff 3, the first version shows two implications associated with thetwo V quantiiiers. The second version is equivalent because of the tautology [A A B —> C] <—>[A —> (B —> C)]. This version may appear to violate the rule that universal quantitiers should beused with implication, not conjunction. but this tautology is just another way to write twoimplications. The second version also shows more clearly that "dogs," the word following"only," is the conclusion.TABLE 1.15English Statement Intermediate Statement Wff1. A11 dogs chase For any thing, if it is a dog, then for (Vx)[D(x)—>(Vy)(R(y) —> C(x, y))]all rabbits. any other thing, if that thing is arabbit. then the dog chases it.2. Some dogs There is some thing that is a dog and. (Ex)[D(x) A (Vy)(R( y) —> C(x, y))]chase all for any other thing, if that thing is arabbits. rabbit, then the dog chases it.3. Only dogs For any thing, if it is a rabbit then, if (Vy)[R(y) —• (Vx)(C(x, y) —-> D(x))]chase rabbits. anything chases it. that thing is a dog.For any two things, if one is a rabbit (Vy)(Vx)[R( y) A C(x, y) —> D(x)]and the other chases it, then the otheris a dog.

Page 58: Mathematical Structures for Computer Science

40 CHAPTER 1 rormal logicOften more than one wff exists that is a correct representation of an English statement, as seenwith statement (3) in Table l.l5. Also, wff (2) is equivalent to(3¤)lD(x) A (Vy)(lR®>l' V €<x.y>>lbecause of the implication equivalence rule that says (R —> C) -» (R' V C), even though hereR and C are predicates instead of just statement letters,ln addition, it is legitimate to "slide" a quantiiier over a predicate that does nut involve thevariable of that quantitier. Because D(x) does not involve y, we could slide the universal quan-tiher in wff (2) to the front (but not past the existential quantifier), giving the equivalent wffGr) (Vy>lD(x> A (R<y> —> C<x.y Ul ta)In wff (a). we still have grouping symbols around R(y) —> C(x, y). Without the grouping sym-bols, this wff becomesGX) <Vy)lD(x) A R(y) —> CU. .v>l (ln)which. according to the order of precedence of connectives, is equivalent to(3-vi (Vy)[(D<>r> A R(y)) -> C(x. y>]A quick truth table exercise shows that D A (R —> C) is nur equivalent to (D A R) —> C, so wff(b) is not equivalent to wff (a) and thus does not represent statement (2) in the table. •Using the predicate symbols S(x) for "x is a student," I(x) for "x is intelligent,"and M(x) for "x likes music." write wffs that express the following statements. (The domain isthe collection of all people.)a. All students are intelligent.b. Some intelligent students like music.c. Everyone who likes music is a stupid student.d. Only intelligent students like music. •'}Q·`§j ````' _ __f;_,;`;,_`f_Q;§* Using the predicate symbols F(x) for "x is a fmit," V(x) for "x is a vegetable,"and S(x, y) for "x is sweeter than y," write wffs that express the following statements. (Thedomain is the whole world.)a. Some vegetable is sweeter than all fruits.b. Every fruit is sweeter than all vegetables.c. Every fruit is sweeter than some vegetable.d. Only fruits are sweeter than vegetables. •Negating statements with quantitiers, as in negating compound statements. requires care.

Page 59: Mathematical Structures for Computer Science

srcnotv 1.3 Quantifiers, Predicates, and Validity 41The negation of the statement "Everything is beautiful" is "It is false that everything is beauti-ful” or "Something is nonbeautiful." Symbolically,[(Vx)A(x)]' is equivalent to (Eix)[A(x)]’Note that "Everything is nonbeautiful," or (Vx)[A(x)]’, says something stronger than the nega-tion of the original statement.The negation of "Sornething is beautiful" is "Nothing is beautiful" or "Everything fails to bebeautiful." Symbolically,[(Ex)A(x)]’ is equivalent to (Vx)[A(x)]’In English, the statement "Everything is not beautiful" would often be misinterpreted as "Noteverything is beautiful" or "There is something nonbeaudfulf However, this misinterpretation.symbolized by (Ex)[A(x)]', is not as strong as the negation of the original statement.Which of the following expresses the negation of "Everybody loves somebodysometime"?a. Everybody hates somebody sometime.b. Somebody loves everybody all the time.c. Everybody hates everybody all the time.d. Somebody hates everybody all the time. •ValidityThe truth value of a propositional wff depends on the truth values assigned to the statement let-ters. The truth value of a predicate wff depends on the interpretation. Choosing an interpreta-tion for a predicate wff is thus analogous to choosing truth values in a propositional wff.However, there are an infinite number of possible interpretations for a predicate wff and only 2"possible rows in the truth table for a propositional wff with ri statement letters.A tautology is a propositional wff that is mie for all rows of the truth table. The analogue totautology for predicate wffs is validity—a predicate wff is valid if it is true in all possible inter-pretations. The validity of a wff must be derived from the form of the wff itself, since validityis independent of any particular interpretation; a valid wff is "intrinsica11y n·ue."An algorithm exists to decide whether a propositional wff is a tautology: constmct the truthtable and examine all possible tmth assignments. How can we go about deciding validity forpredicate wffs? We clearly cannot look at all possible interpretations, because there is an infi-nite number of them. As it tums out. no algorithm to decide validity exists. (This does not meansimply that no algorithm has yet been found—it means that it has been proved that there is nosuch algorithm.) We must simply use reasoning to determine whether the form of a wff makesthe wff true in all interpretations. Of course, if we can find a single interpretation in which thewff has the truth value false or has no truth value at all, then the wif is not valid.Table 1.16 on page 42 compares propositional and predicate wffs.

Page 60: Mathematical Structures for Computer Science

42 CHAPTER 1 Formal Lugar*1ux tu 1.16Propositional Wffs Predicate WffsTruth values True or false, depending on truth True, false, or perhaps (if thevalue assignments to statement wff has a free variable) neither.letters depending on interpretation"Int7rinsic u·uth” Tautol<>gy~—true for all truth Valid wff—t:rue for allvalue assignments interpretationsMethodology Algorithm (truth table) to determine No algorithm to determinewhether wff is a tautology whether wff is validNow let’s try our hand at determining validity.EXAMPL E 2 3a. The wff(Vx)P(x) —-> (Ex)P(x)is valid. In any interpretation, if every element of the domain has a certain property, thenthere exists an element of the domain tl1at has that property. (Remember that the domain ofany interpretation must have at least one object in it.) Therefore, whenever the antecedentis true, so is the consequent, and the implication is therefore tme.b. The wff(Vx)P(x) -> P(a)is valid because in any interpretation, a is a particular member of the domain and thereforehas the property that is shared by all members of the domain.c. The wff(V><)lP(><> A Q<x)l ·> (V¤¢)F<x) A (V><)Q(¤r>is valid. If both P and Q are crue for all the elements of the domain, then P is tme for allelements and Q is true for all elements, and vice versa.d. The wffPU) ··> [QU) ·> PCO]is valid, even though it contains a free variable. To see this, consider any interpretation, andlet x be any member of the domain. Then x either does or does not have property P. If xdoes not have property P, then P(x) is false; because P(x) is the antecedent of the mainimplication, this implication is true. lfx does have property P, then P(x) is true; regardlessof the truth value of Q(x), the implication Qtr) —-> P(x) is true, so the main implication isalso true. •

Page 61: Mathematical Structures for Computer Science

srcnoxv 1.2 Quanririers, predicates, and validity 43EXAMPLE 24 The wff ·(3x)P(x) -> (Vx)P(x)is not valid. For example, in the interpretation where the domain consists of the integers andP(x) means that x is even, it is true that there exists an integer that is even, but it is false thatevery integer is even. The antecedent of the implication is true and the consequent is false, sothe value of the implication is false. •We do not necessarily have to go to a mathematical context to construct an interpretation inwhich a wff is false, but it is frequently easier to do so because the relationships among objectsare relatively clear.is the WK<\‘/><)[P<x> V Q(¤<>l —> <Vx>P<x> V <Vx)Q(x>valid or invalid? Explain. •SECTION 1.3 Review

TECHNIQUES Z§§§ • Determine the truth value of a predicate wff in a giv interpretation.• Translate English language statements into predicate wffs, and vice versa.• Recognize a valid wff and explain why it is valid.• Recognize a nonvalid wff and construct an interpretation in which it is false orhas no uuth value.MAIN IDEAS• The u·uth value of predicate wffs depends on the interpretation considered.• Valid predicate wffs are "intrinsically true"—true in all interpretations.EXERCISES 1.3l. What is the truth value of each of the following wffs in the interpretation where thedomain consists of the integers, O(x) is "x is odd," L(x) is "x < l0," and G(x) is "x > 9"7a. (3¤¤>O<x> ¤· <3>¢)[l»<x> A G(><>lb. (Vx)[L(r) —> O(x)] d. (Vx)[L(x) v G(r)]2. What is the truth value of each of the following wffs in the interpretation where thedomain consists of the integers?* a. (Vx>Gy><>¤ + y = xl e. (VxJ<Vy)(¤¤ < y V y < ¤¤>* b. (3y)(Vx)(x + y = x) f. (Vx)[x < U —> (3y)(y > 0 A r + y = 0)]* ¢. (\‘/x)Gy><x + y = 0) s- (3xJ(5y><x“ = y>* d. (Ey)(Vx)(x + y = 0) h. (Vx)(x‘ > 0)

Page 62: Mathematical Structures for Computer Science

44 cuawrn 1 mma: Logic3, Give the truth value of each of the following wffs in the interpretation where the domainconsists of the states of the United States, Q(x, y) is "x is north of y," P(x) is "x starts withthe letter and :1 is "Massachusetts."a. (Vx)P(x)b- (Vx)(Vy)<Vz)[Q(¤¤. y) A Q(y. z) + Q<x. 2)]c. <3y)<5¤¤)Q(y. x) ~d. (Vx)<3y)[P<y> A Qtr. y>lc. (3y>Q<¤, y> .f- <3>¢)[P(¤<) A Q<x, a)l4, Give the truth value of each of the following wffs in the interpretation where the domainconsists of people: M(x, y) is "x is the mother ofy," F (x) is 5: is female," M(x) is "m is male."¤- <Vx><3y)(M<yt xD) rl. <3x)(5y)<M<¤¤. y) A M(y))b. <5x)(\‘/y)(M(¤<, y)) e. (5>¤)<Vy><M<¤¤. y) —> F <y))c. (\‘/x)(Vy)(M(x. y) —> M(y))5, For each wtf, find an interpretation in which it is true and one in which it is false.* a. (V><)<[A(x) V BOO] A [AG) A B(¤<)l’)lv- (Vx)(Vy)lP(1¤.y) —> F(y. ¤<)]¢· (Vx)[P<x> —> <E|y)Q(¤<, y)ld· <5x)lA<>¤) A <Vy>B<¤¤» y)le. [(V><)A(x) -> (V¤¤)B<¤=)l —> <V¤<)[A(J¢) -+ B(>¤)l6. Identify the scope of each of the quantifiers in the following wffs and indicate any freevariables.a. (V¤<)[P(x) —> Q(y)l c. G>¤>[<Vy)P(¤<. y) A Qtr. y)lb- Gx>[A(x) A (Vy)B0»)l d» <3¤)(5y)[A(x, y) A B<y, z) —> Aim z)l7. Which of the following are equivalent to the statementAll circles are round. AX : . » E — 1a. If it's round, it's a circle.b. Roundness is a necessary property of circles.c. Something that isn’t round can't be a circle.d. Some round things are circlesi8. Which of the following are equivalent to the statementCats are smarter than dogs.a. Some cats are smarter than some dogs.b. There is a cat that is smarter than all dogs.c. All cats are smarter than all dogs.d. Only cats are smarter than dogs.e. All cats are smarter than any dog, .9. Using the predicate symbols shown and appropriate quantifiers, write each Englishlanguage statement as a predicate wff. (The domain is the whole world.)D(x) is "x is a day."Sor) is wr is sunny."R(x) is We is rainy."M is "Monday."Tis "’Iuesday."

Page 63: Mathematical Structures for Computer Science

SECTION 1.2 Quanrifiers, Predieates, and Validity 45ir a. All days are sunny. ·ir b. Some days are not rainy.ir c. Every day that is sunny is not rainy.d. Some days are sunny and rainy.e. No day is both sunny and rainy.f. It is always a sunny day only if it is a rainy day.g. No day is sunny.h. Monday was surmy; therefore every day will be sunny.i. lt rained both Monday and Tuesday.j. lf some day is rainy, then every day will be sunny.l0. Using the predicate symbols shown and appropriate quantiiiers, write each English lan-guage statement as a predicate wff. (The domain is the whole world.)B(x) is "x is a ball."R(x) is "x is r<>und."S(x) is "x is a soccer ball."a. All balls are round.b. Not all balls are soccer balls.c. All soccer balls are round.d. Some balls are not round.e. Some balls are round but soccer balls are not.f. Every round ball is a soccer ball.g. Only soccer balls are round balls.h. lf soccer balls are round, then all balls are round.ll. Using the predicate symbols shown and appropriate quantifiers. write each English lan-guage statement as a predicate wff. (The domain is the whole world.)P(x) is "x is a person."T(x) is "x is a t.ime."F(x, y) is "x is fooled at y."a. You can fool some of tl1e people all of the time.b. You can fool all of the people some of the time.c. You can’t fool all of the people all of the time.l2. Using the predicate symbols shown and appropriate quantitiers, write each English lan-guage statement as a predicate wff. (The domain is the whole world.)L(x): x is a lion.R(x): x roars.P(x): x is a predator.Z(x): x is a zebra.E(x, y): x eats y.a. All lions are predators. d. Some lions eat all zebras.b. Some lions roar. e. All lions eat all zebras.c. Only lions roar.

Page 64: Mathematical Structures for Computer Science

46 CHAPTER 1 Formal Logic13. Using the predicate symbols shown and appropriate quantiiiers. write each English lan-guage statement as a predicate wff, (The domain is the whole world.)G(x): x is a game.M(x): is a movie. _F(x, y): x is more fun than y.a. Any movie is more fun than any game.b. No game is more fun than every movie.c. Only games are more fun than movies.d. All games are more fun than some movie,14. Using the predicate symbols shown and appropriate quantitiers, write each English lan-guage statement as a predicate wff. (The domain is the whole world.)J(x) is "x is ajudge."C(x) is **1: is a chemist."L(x) is "x is a lawyer."W(x) is "x is a woman."A(x, y) is "x admires y."a. There are some women lawyers who are chemists.t b. No woman is both a lawyer and a chemist.t c. Some lawyers admire only judges.d. All judges admire only judges.e. Only judges admire judges.f. All women lawyers admire some judge.g. Some women admire no lawyer.15. Using the predicate symbols shown and appropriate quantiEers, write each English lan-guage statement as a predicate wff. (The domain is the whole world.)C(x) is "x is a Corvette?P(x) is "x is a Porsche?F(x) is "x is a Ferrari."S(x, y) is "x is slower than y,"ir a. Nothing is both a Corvette and a Ferrari.* b. Some Porsches are slower than only Ferraris.e. Only Corvettes are slower than Porsches,d. All Ferraris are slower than some Corvette.e. Some Porsches are slower than no Corvette.f. lf there is a Corvette that is slower than a Ferrari, then all Corvettes are slower thanall Ferraris.16. Using the predicate symbols shown and appropriate quantiiiers. write each English lan-guage statement as a predicate wff. (The domain is the whole world.)

Page 65: Mathematical Structures for Computer Science

SECTION 1.a Quantiliers, Predicates, and Validity 47B(x) is "x is a bee." »F (x) is "x is a flower."L(x, y) is "x loves y."a. All bees love all flowers.b. Some bees love all flowers.c. All bees love some flowers.d. Every bee hates only flowers.e. Only bees love flowers.f. Every bee loves only flowers.g. No bee loves only flowers.h. Some bees love some flowers.i. Some bees love only flowers.j. Every bee hates some flowers.k. Every bee hates all flowers.l. No bee hates all flowers.l7. Using the predicate symbols shown and appropriate quantifiers, write each English lan-guage statement as a predicate wff. (The domain is the whole world.), S(x) is "x is a spy novel."L(x) is "x is long."M(x) is "x is a inystery,"B(x, y) is "x is better than y."a. All spy novels are long.b. Not every mystery is a spy novel.c. Only mysteries are long.d. Some spy novels are mysteries.e. Spy novels are better than mysteries.f. Some mysteries are better than all spy novels.g. Only spy novels are better than mysteries.18. Give English language translations of the following wffs ifL(x. y) is "x loves y."H(x) is "x is handson1e.”M(x) is "x is a rnan.”P(x) is "x is pretty?‘ j is “.Tohn."k is "Kathy."W(x) is “x is a woman."A ti. H(]) A L(k,j)t b. (Vx)[M(x) -> H(x)]€· (V¤¤)(W(J<)·> (V>’)lL(»‘¢» V) ·> MU} A H(y)]}d. (EIx)[M(x) A H(x) A L(x, k)]e- Gx>(W(x> A P<x> A (Vy>[L(¤¤. y) -> H(y> A M<y>l)f. (Vx)[W(x) A P(x) —> L(j, x)]

Page 66: Mathematical Structures for Computer Science

48 CHAPTER 1 Formal logicl9. 'Three forms of negation are given for each statement. Which is correct'!ik a. Some people like mathematics.l. Some people dislike mathematics.2. Everybody dislikes mathematics.3. Everybody likes mathematics.b. Everyone loves ice cream.l. No one loves ice cream.2. Everyone dislikes ice cream.3. Someone doesn't love ice cream. ‘c. All people are tall and thin.1. Someone is short and fat.2. No one is tall and thin.3. Someone is short or fat.d. Some pictures are old or faded.l. Every picture is neither old nor faded.2. Some pictures are not old or faded.3. All pictures are not old or not faded.20. Three forms of negation are given for each statement. Which is correct?a. Nobody is perfect.l. Everyone is imperfect.2. Everyone is perfect.3. Someone is perfect.b. All swimmers are tall.1. Some swimmer is not tall.2. 'There are no tall swimmers.3. Every swimmer is short.c. Every planet is cold and lifeless.1. No planet is cold and lifeless.2. Some planet is not cold and not lifeless.3. Some planet is not cold or not lifeless.d. No bears are hungry.l. Only bears are hungy.2. All bears are hungry.3. There is a hungry bear.2l. Write the negation of each of the following.a. Some Web sites feature audio.b. Every Web site has both audio and video.it c. Every Web site has either audio or video.d. Some Web sites have neither audio nor video.e. Every Web site either has text or else has both audio and video.22. Write the negation of each of the following.a. Only students eat pizza.b. Every student eats pizzac. Some students eat only pizza.23. Write the negation of each of the following.a. Some farmer grows only com.b. All farmers grow com.` c. Corn is grown only by farmers.

Page 67: Mathematical Structures for Computer Science

srcnozv 1.4 rreaime logic as_ 24. Explain why each wff is valid. »¤· <V¤¤><Vy>A(>¤. y) ·-· (Vy>(Vx>A<x, y)b- (3¤¤><5y>A<>¤. yi <—> <Ely>(3x>A(x. y)* ¢. G¤¤>(Vy>P(x. yl —> <Vy><3x)P(x, y>d. A(o) -> (Elx)A(x)e. tV>¤JlA(x> -> B<>¤>] —> [<Vx>A(x) + <Vx)B(x)]25. Give interpretations to prove that each of the following wffs is not valid:* a- <3¤)A(¤<> A (3x>B(x) —> G>¤>[A<x> A Bullb. (Vx>(5y)P(¤<, y> -> <5x)(\‘/y)P(x. ylct (V¤<>lP<x> —> Q<>¤>l —> [<3¤¤>P<x> —> <Vx>Q<>¤>]d- (V¤)lA<¤¤>l' —> [<Vx>A(><)l'26. Decide whether each of the following wffs is valid or invalid. Justify your answer.a. Gx>A<¤¤> ~» <<V¤¤>[A(x>]')'b· <Vx>P(»¤) V Gx>Q(x> —> <Vx>[P<>¤J V Q<x>l¤· <V¤¤>A(>¤) -> <<3>¤>[A<x)l')'d. (Vx>[F<x) V Q<x>l -> <V¤<)P(><J V (3y>Q<y>SECTION 1.4 Predicate LogicWe can imagine arguments of the formP, AP2/tP3A~-AP"—>Qwhere the wffs are built from predicates and quantifiers as well as logical connectives andgrouping symbols. For a valid argument. Q must follow logically from Pl, , Pn based solelyon the intemal structure of the argument, not on the truth or falsity of Q in any particular inter-pretation, In other words, the wffA P./tP2AP3A--·APn->Qmust be valid—true in all possible interpretations. No equivalent of the truth table exists to eas-ily prove validity, so we tum to a fomial logic system called predicate logic. We again use asystem of derivation rules to build a proof sequence leading from the hypotheses to the conclu-sion. The rules should once more be truth-preserving so that if in some interpretation all thehypotheses are true, then the conclusion will also be true in that interpretation. The system willthen be correct (only valid arguments will be provable). We also want the system to be complete(every valid argument should be provable), yet at the same time the rule set should be minimal.Derivation Rules for Predicate LogicThe equivalence rules and inference rules of propositional logic are still part of predicate logic.An argument of the formP A (P —> Q) -> Qis still valid by modus ponens, even if the wffs involved are predicate wfs.

Page 68: Mathematical Structures for Computer Science

sEcn0N 1.4 Pt-edicace Logic 51Now let’s examine these mles more closely, particularly the necessity for their restrictions. .Universal instantiationThis rule says that from (Vx)P(x) we can derive P(x), PQ:). P(z), P(a). and so on, thus strippingoff a universal quantifier. The justification is that if P is tnie for every element of the domain,we can name such an element by an arbitrary variable name like x, y, or z, or we can specify aparticular constant in the domain, and P is still tme for all of these things.EXAMPLE 26 Universal instantiation can be used to prove one of the classical "syllo-gisms" of the Greek philosopher and scientist Aristotle, who lived from 384 to 322 a.c. andwho first developed a system of formal logic.The argument has the form "All humans are mortal. Socrates is human. Therefore Socratesis mortal." Using the notationH(x) is "x is human."s is a constant symbol (Socrates).M(x) is **1 is mortal"the argument is _(Vx)[H(x) -> M(x)] A H(s) —> M(s)and a proof sequence is1· <V¤¤><H<x> —>M(x>> hyp2. H(s) hyp3. H(s) —-> M(.v) 1, ui4. M(s) 2, 3. mpIn step 3, a constant symbol has been substituted for x throughout the scope of the universalquantilier. as allowed by tmiversal instantiation. •Without the restriction on universal instantiation, a hypothesis of the form (Vx)(Ely)P(x, y)could lead to the wtf (Ey)P(y, y); here y has been substituted for x within the scope of a quanti-fier on y. This would be invalid. For example, in the domain of the integers, if P(x. y) means"y > x.“ then (Vx)(3y)P(x, y) is true (for every integer there is a bigger integer) but (Ely)P(y, y)is false (no integer has the property that it is bigger than itself).f Prove me argument (Vx)[P(x) Q Roo] A [R(y)]’ -> [1>(y)]*. •Existential InstantiationThis rule allows us to strip off an existential quantifier. It says that from (Ex)P(x) we can deriveP(u) or P(b) or P(c), provided that these are new constant symbols. The justification is that

Page 69: Mathematical Structures for Computer Science

50 CHAPTER 1 rwmal logicEXAMPLE 25 Use predicate logic to prove the validity ofthe argument(Vx)R(x) A [(Vx)R(x) —> (Vx)S(x)] —> (Vx)S(x)A proof sequence is1. (Vx)R(x) hyp2. (Vx)R(x) —> (Vx)S(x) hyp3, (Vx)S(x) 1, 2, mp •However, there are many arguments with predicate wffs that are not tautologies but are stillvalid because of their structure and the meaning of the universal and existential quantifiers (seeExample 23). The overall approach to proving these arguments is to strip off the quantitiers,manipulate the unquantiiied wffs. and then put the quantiiiers back in. The new rules of infer-ence provide mechanisms to strip off and insert quantifiers. Hence there are four new rules-one each to strip off the universal and existential quantifier. respectively. and one each to insertt.he universal and existential quantitier, respectively, The four rules are given in Table 1.17; theirdetails will be explained shortly. In 'Ilable 1.17, the notation P(x) does nat imply that P is aunary predicate with x as its only variable; it simply means that x is one of the variables in thepredicate P, Thus P(x) might actually be something like (Ely)(Vz)Q(x, y, z).TABLE 1.17Inference RulesName/AbbreviationFrom Can Derive for Rule Restrictions on Use(Vx)P(x) P(r) where tis a variable or Universal If x is a variable, it mustconstant symbol instantiation—ui not fall within the scopeof a quantifier for t.(Ex)P(x) P(a) where a is a constant Existential Must be the first rulesymbol not previously instantiation——ei used that introduces a.used in proof sequenceP(x) (Vx)P(x) Universal P(x) has not beengeneralization—ug deduced from anyhypotheses in which x isa free variable nor hasP(x) been deduced by eifrom any wff in whichx is a free variable.P(x) or P(u) (Ex)P(.r) Existential To go from P(a) towhere a is generalizarion—eg (Elx)P(x). x must nota constant appear in P(a).symbol `

Page 70: Mathematical Structures for Computer Science

sz CHAPTER 1 Formal logicif P is true for some element of the domain, we can give that element a specific name, but wecannot assume anythi.ng else about it.EXAMPLE 27 The following would be legitimate steps in a proof sequence:I- (VX)[P(1) ·> Q(J¢)] h>’P2. Gy>P<y> hyp3. P(a) 2, ei4. P(a) —> Q(a) 1, ui5. Q(a) 3, 4. mpIn step 3, the specific element with property P was given the name a. In step 4, ui was then_ 4: _’*‘ used to say that an implication that is universally true in the domain is certainly true for this atSteps 3 and 4 cannor be reversed. lf ui is First used on hypothesis l to name a constant a, thereis then no reason to assume that this particular a is the one that is guaranteed by hypothesis 2¤’V'*m*¤ “ to have property P. •The effect of the restriction on existential instantiation is that you should look at all yourhypotheses and, if you plan to use ei on any of them, do it Grst.Universal GeneralizationUniversal generalization allows a universal quantifier to be inserted. This must be done prettycarefully, however. If we know that P(x) is true and that the x is absolutely arbitrary, i,e., that xcould be any element of the domain, then we can conclude (Vx)P(x). But if x is supposed torepresent some specific element of the domain that has property P, then we can’t generalizethat every element of the domain has property P.EXAMPLE 28 Use predicate logic to prove<V>¤>[F(x) —> Q<x)l A (\‘/¤<)P<¤¤> —> (Vx>Q<¤¤>Here is a proof sequence:L (VX)[P(¤¤) -> Q(>¤)] h>’P2. (Vx)P(x) hyp3. P(x) —> Q(x) l, ui4. P(x) 2, ui Note that there is no restriction on ui about reusing a name.5- Q(><) 3, 4, mp6. (V¤¤>Q<x> 5. usThe use of universal generalization at step 6 is legitimate because x was not a free variable inany hypothesis nor was ei used anywhere in the proof, The variable x in steps 3 and 4 is just anarbitrary name, representative of any element in the domain. •

Page 71: Mathematical Structures for Computer Science

srcnorv 1,4 Predicate Logic saThere are two restrictions on universal generalization. Without the f.rst restriction. thesequence1. P(x) hyp2. (Vx)P(x) 1, incorrect ug; x was free in the hypothesis.would be a proof of the wff P(x) —> (Vx)P(x), but this is not a vmid wff. Element x ofthe domain may have property P. but that does not mean that every element of the domain hasproperty P. In the hypothesis, x is naming some fixed if unspecified element of the domain. Forinstance, in the interpretation where the domain consists of automobiles and Ptx) means "x isyellow," some particular car may be yellow but it is certainly not true that all cars are yellow.Without the second restriction. the sequence1. <\1xJGy>Q<x. y) hypit <3y)Q(¤¤. y) 1. ui3- QU. 41) 2. ei4. (Vx)Q(x, a) 3, incorrect ug; Q(x, rr) was deduced by ei from the wff in step 2,in which x is free.would be a proof of the wff (Vx)(Ely)Q(x, y) -> (Vx)Q(x, a). This is also not a valid wff. Forinstance. in the interpretation where the domain consists of the integers and Q(x. Y) means thatx + y = 0, then it is the case that for every integerx there is an integer y (the negative of x) suchthat x + y = 0. However, if a is a particular iixed element in the domain, tl1en it will not be truethat adding that same integer a to every x will always produce zero.Prove the argument (Vx)[P(x) A Q(x)] —• (Vx)[Q(x) A P(x)]. •Existential GeneralizationThe last rule allows insertion of an existential quantitier. From P(x) or P(a) we can derive(Ex)P(x); something has been named as having property P, so we can say that there existssomething that has property P,EXAMPLE 29 Prove the argument (Vx)P(x) —> (Ex)P(x).Here is a proof sequence:1. (Vx)P(x) hyp2. P(x) 1, ui3. (Elx)P(r) 2, eg •Without the restriction on existential generalization, from P(a, y) one could derive (3y)P(y. y);here the quantified variable y, which replaced tl1e constant symbol rz, already appeared in thewff to which existential generalization was applied. But the argument P(a, y) —> (3y)P(y, y) isnot valid. In the domain of integers, if P(x, y) means "y > x" and a stands for 0, then if y > 0,this does nut mean that there is an integer y that is greater than itself.

Page 72: Mathematical Structures for Computer Science

54 CHAPTER 1 Formal LogicMore Work with RulesAs is the case with propositional logic rules, predicate logic rules can be applied only when theexact patoern of the rule is matched (and, of course, when no restrictions on use of the rule areviolated). In particular, notice that tl1e instantiation rules strip off a quantilier from the front ofan entire wff that is in the scope of that quantiiier. Both of the following would be illegal usesof existential instantiation:1- (BXJPUO V (3x>Q(x) hy?2. P(a) V Q(a) I, incorrect ei. The scope of the first existential quantiiier instep l does not extend to the whole rest of the wff.1. (V¤¤><3,v)Q<x· y) hy?2. (Vx)Q(x. a) l, incorrect ei. The existential quantilier in step 1 is not at thefront.Similarly, the rules to insert a quantifier put that quantifier in the front of a wff that is thenentirely within its scope.Even though we have added only four new derivation rules, the rule set is complete andcorrect. We can prove every valid argument and only valid arguments using these rules. Appli-cation of the niles. as in the case of propositional logic, is somewhat mechanical because thereare only a limited number of options at each step. Again, the general plan of attack is usually asfollows:• Strip off the quantifiers.• Work with the separate wffs.• Insert quantifiers as necessary.EXAMPLE 30 Using predicate logic, prove the argument<Vx>IP(x> A Q(x)l —-> <Vx)P(>¤> A (Vx)Q<x>In Example 23(c) we noted that this wff is valid, so if all valid arguments are provable, weshould be able to fmd a proof sequence, As usual, the hypothesis gives us a starting point:1. (V¤¤)[P(¤¢) A Q(x)l hy?Stripping off the universal quantilier that appears in step l will yield access to P(x) A Q(x),which can then be separated. The universal quantiiier can then be inserted separately on each ofthose two wffs using universal generalization. The conclusion (Vx)P(x) A (Vx)Q(x) will follow.A proof sequence isl. (Vx)[P(x) A Q(x)] hyp 5. (Vx)P(x) 3, ug2. P(x) A Q(x) l. ui 6. (Vx)Q(x) 4, ug3. P(x) 2, sim 7. (Vx)P(x) A (Vx)Q(x) 5, 6, con4. Q(x) 2, sim

Page 73: Mathematical Structures for Computer Science

sscnotv 1.4 Predicate Logic 55Neither restriction on universal generalization has been violated because x is not free in thehypothesis a.nd existential instantiation has not been used. •Using predicate logic, prove the following argument. (Him.- The deductionmethod still applies.)<Vy)lP(¤<) e QU. y>l —> [FOO —> (Vy)Q(x. y)l •As an extension to the deduction method. we can insert a "temporary" hypothesis into aproof. If some wff T is introduced into a proof sequence as a temporary hypothesis. and even-tually a wff Wis deduced from Tand other hypotheses, then the wtf T —> Whas been deducedfrom the other hypotheses and can be inserted in the proof sequence.EXAMPLE 31 The argument[Pix) -> (Vy>Qt¤. y>l —> <VyJlP(¤> e Qtr. y>lis valid. In the following proof sequence, P(x) is introduced at step 2 as a temporary hypothesis,which allows us to deduce Q(x, y) at step 4. The indented steps show that these wffs depend onthe temporary hypothesis. At step 5, the temporary hypothesis is "discharged,” as the depend-ency of Q(x, y) on the temporary hypothesis is explicitly acknowledged as an implication. Ofcourse, the entire wff at step 5, P(x) —> Q(x, y), still depends on the hypothesis of step l. Atstep 6, neither restriction on universal generalization is violated because y is not a free variablein step 1 (the only hypothesis at this point) and existential instantiation is not used in the proof.lt P(¤t) —> (Vy)Q(>¢t Y) h>'P2. P(x) temporary hyp3. <Vy)Q(x, y) 1. 2. mp4- Q(x. Y) 3. ui5, P(x) —> Q(x, y) temp. hyp discharged6~ (VY)lP(¤¤) —> QU. YH 5. UENotice how the temporary hypothesis gives us enough ammunition to make something happen.Without this technique. it would be difficult to know what to do after step l. •The technique of introducing a temporary hypothesis is seldom needed. Again, think of thisas an extension to the deduction method. If the desired conclusion is of the form P —> Q, thededuction method says we can assume P as a hypothesis and deduce Q as the conclusion. If thedesired conclusion is of the form (\‘/x)(P(x) —> Q(x)) or (3x)(P(x) —> Q(X)). then the deductionmethod does not apply. but P(x) can be used as a temporary hypothesis.Practice 24 and Example 31 show that the wff<\‘/y)lP(¤<> -> Qtr. y>l —> [Por) —> <\‘/y)Q(x, y>lis valid. It says that the universal quantifier can "slide over" subwffs that do not contain thequantified variable; in this case, (Vy) is passed over P(x), A similar result holds for the existential

Page 74: Mathematical Structures for Computer Science

ss cwarrsn 1 Formal logicquantiiier. We noted this feature in Example 22, and here is the formal justification. This is onereason why there may be two or more equivalent ways of expressing English language sen-tences as predicate wffs, as in Exercises 9 through l7 of Section l.3.Frm the argument .(Vx)[<B(>¤> V C(x)) + A(¤<)l —> (Vx)[B<>¤> + A(¤<)l •In Section 1,3 we observed that, based on our understanding of negation and the meaning ofthe quantiiiers, [(3x)A(x)]’ is equivalent to (Vx)[A(x)]'. We should be able to formally prove[G¤¤>A(¤¤)l' ·-> (Vx)[A<x)l'EXAMPLE 32 Prove that[Gx)A(x)l’ ~ (V¤<)[A(Jr)l'We must prove the implication i.n each direction.a- [(3x>A(¤<>l’ —> <\‘/¤<>lA<>¤>l'The hypothesis alone gives us little to work with, so we introduce a (somewhat surprising) tem-porary hypothesis. A proof sequence isI- [(3¤¤)A(¤¢)l' hyp2. A(x) temporary hyp3. (Eix)A(x) 2, eg4. A(x) —> (3x)A(x) temporary hyp discharged5. [A(x)]' 1, 4, mt6- (\‘/¤¤)lA<¤¤>l' 5. usb- (V¤¤>[A(><Jl' —> l(3x)A(x)l'This proof also requires a temporary hypothesis. It is even more surprising than case (a)because we assume the exact opposite of the conclusion we are trying ro reach.1- (Vx)[A(><)l' hyp2. (Eix)A(x) temporary hyp3. A(a) 2, ei4. [A(¤)]' 1, ui5- [<Vx>[A(x)l’l' 3. 4- inc6. (Eix)A(x) —-> [(Vx)[A(x)]']' temporary hyp discharged7- [((V%)[A(¤¤)]')'l' 1.d¤8- [(5»<)A(¤)l' 6. 7. mt •

Page 75: Mathematical Structures for Computer Science

sscnorv 1.4 Predicate Logic 57The proofs of Example 32 are rather difficult because they require considerably more imag-ination than most and an unexpected use of a temporary hypothesis. As a result. however. we dohave the following equivalence, to which we've given a name:[(3x)A(x)]' ·-> (Vx)[A(x)]’ (Negation—neg)This might be useful in a proof sequence, As an extension of the equivalence rules, wheneverP r»» Q is valid, Q can be substituted for P within an expression in a proof sequence. The argu-ment in Practice 23, (Vx)[P(x) A Q(x)] -> (Vx)[Q(x) A P(x)]. can be proved in two steps simplyby substituting Q(x) A P(x) for P(x) A Q(x) in the hypothesis. since these wffs are equivalent.EXAMPLE 33 Is the wff<Vx>[P(>¤) V Q(x)l —> G>¤)P<>¤) V <\‘/>¤>Q<xla valid argument? Prove or disprove.Let’s first consider whether the wff seems valid. lf so, we should try to find a proof sequencefor it; if not, we should try to find an interpretation in which it is not true, This wff says that ifevery element of the domain has either property P or property Q. then at least one element musthave property P or else all elements have property Q. This seems very reasonable, so we‘ll tryto find a proof.First we’ll use an equivalence to rewrite the conclusion in a more useful form. Changing theV to an implication will allow use of the deduction method. Thus we want to prove(Vx>lP<x> V Q<x>] —> [ [<3x>P(=<>l' -> (Vx>Q<x>lA proof sequence is1- <V><)lP(¤<) V Q(><)l hyp2- lG¤¤JP<xJl' hw3- (VX)[P(X)l' 2. neg4. [P(x)]' 3, ui5. P(x) V Q(x) l. ui6· [lP(X)]'l' V QQ) 5. dn7. lP<¤<>l’ —> Qtr) 6. imp8. Q(x) 4, 7, mp9. (Vx)Q(x) S, ug •EXAMPLE 34 ls the wff(El>¤>P(>¤> A (3¤<)Q(><) -> (3¤¤>[P(x) A Q(x)la valid argument? Prove or disprove.If something in a domain has property P and something has property Q, that does not meanthat some one thing has both property P and Q. For example, in the domain of integers. if P(x)

Page 76: Mathematical Structures for Computer Science

ss CHAPTER 1 formal Logicmeans "x is even" and Q(x) means "x is odd," then the hypotheses are true, but the conclusionis false because there is no single integer that is both even and odd. One interpretation in whichthe wff is false is enough to disprove it.It is useful, however, to see where a potential proof sequence goes wrong. We begin with thetwo hypotheses and then remove one of the existential quantifiers.l. (EIx)P(x) hypZ- (31<)Q(>¤) hYP3. P(a) 1, eiNow here‘s the problem. The next step would be to remove the existential quantiiier from thewff at step 2. but, according to the rules for ei, we have to name the object that has property Qby some different name, not u. So we could eventually get to a wff in the proof sequence thatlooks likeP(¤) A Qfb)but this does us no good. Existential generalization could not be used to replace both constantsymbols with a single variable. At best, we could arrive atGy)<Eb¤)[P(x) A 90*)]which is not what we want. •is me wtfG¤<)R<>r> A [(E|><)iR<x> A 5(x)]]' —> (3¤¤)[S<x>]’a valid argument? Prove or disprove. •Verbal ArgumentsTo prove the validity of a verbal atgurncnt, we proceed much as before. We cast the argument insymbolic form and show that the conclusion can be deduced from the hypotheses. lf the argu-ment involves predicate wffs. then the derivation rules of predicate logic are available.EXAMPLE 35 Show that the following argument is valid: "Every microcomputer has aserial interface port. Some microcomputers have a parallel port, Therefore some microcomput-ers have both a serial and a parallel port." UsingM(x) is "x is a microcomputer?$(1) is "x has a serial port.”P(x) is "x has a parallel port."the argument is<Vx>[M(>¤> ——> S<x)l A (3x>[M(x) A P<x>l —> (3x)[M(x) A SU) A P(x>l

Page 77: Mathematical Structures for Computer Science

SECTION 1.4 Predicate Logic asNote that if we attempt to symbolize this argument in propositional logic, we getA A B —> C, »which is not a valid argument. Propositional logic is simply not expressive enough to capturethe interrelationships among the parts of this argument that serve to make it valid.A proof sequence is1, (Vx)[M(x) —> S(x)] hyp2· (3X)lM(>¤> A FOO] h>’P3. M(a) A P(u) 2. eiA. M(a) -> S(u) 1. ui5. M(a) 3, sim6, S(a) 4, 5. mp7. M(a) A P(a) A S(a) 3, 6, con8. M(a) A S(a) A P(a) 7. comm9. (Elx)[M(x) A S(x) A P(x)] 8, egOnce again, it is the form of the argument that matters, not the content. •§ show that the rottawang argument is vaua; mu mk music as loud music.Some rock music exists; therefore some loud music exists." Use predicates R(x) and L(x). •ConclusionWe've now finished our study of formal logic. What has been accomplished? The goal of for-mal logic, often called symbolic logic. is to make argmments as meaningless as possible! Thesymbolic notation of propositional and predicate logic allows us to symbolize arguments. Anargument cast in symbolic notation removes any possibility that we will be swayed by ouropinions or our external knowledge about the topic of the argument, and we can concentratesolely on its structure to determine its logical validity. Furthermore. the derivation rules allowthe proof of an argument’s validity to be produced by symbol manipulation. (A table of all thederivation rules is given in Appendix A,) A proof requires no external knowledge. only a care-ful adherence to the forms and restrictions of the rules, In theory. then. producing a proofsequence should be almost mechanical. Again. one objective of practice with this mechanicalprocess of applying rules is that it will ultimately transform into a habit of logical thinking ineveryday life.Nonetheless, you may still feel that it is difficult to produce a proof sequence. Practice doesmake the process easier, because after a while. you become familiar with the various forms anargument might take and you recognize which mles you should u·y to apply. At any rate. youshould at least find it easy at this point to check whether a proposed proof sequence is logi-cally correct.Philosophers through the ages believed logical thinking to be one of the highest achieve-ments of human existence. One additional example, however, will show how even the mostcareful application of logic can be frustrated.

Page 78: Mathematical Structures for Computer Science

60 CHAPTER 1 Formal LogicEXAMPLE 36 The hunting grounds of a medieval king were forbidden to commoners, andanyone caught poaching the royal deer was subject to death. The hapless poacher was, however,granted a means to choose the manner of death. He (or she) was allowed to make a final state-rnent. I.f the statement were judged to be true, death would be by beheading with a sword; iffalse, death would come by arrow shot from rhe bow of the best royal marksman. One day aparticularly clever poacher was apprehended and allowed the usual final statement. Thepoacher said, "I shall be shot to death by an arrow."The king‘s court faced a conundrum. Lf the poacher were shot to death by an arrow. then thestatement he made would prove to be tme, in which case he should have been beheaded. But ifhe were beheaded, then the statement he rnade would be false, in which case he should havebeen shot by an arrow. Unable to decide the manner of death, the court appointed the cleverpoacher to the post of king’s press secretary, where he served happily for many years.This sort of pamdox—a riddle with no solution—has to be carefully constructed. and wewill not spend any more time reflecting on the potential shortcomings of classical logic systemsthat it may reveal. •In addition to logical thinking in its pure sense. tl1e notions of formal rules of inference havetwo very direct applications to computer science. An entire system of programming and someprogramrning languages are based on applying rules of inference. We will see such a languagein Section 1.5. Similarly, rules of inference can be applied to formally prove program correcr-ness, leading to increased confidence that code is error-free. We’ll look at some of the inferencerules for program correctness in Section 1.6.SECTION 1.4 ReviewTECHNIQUES• Apply derivation rules for predicate logic.• Use predicate logic to prove the validity of a verbal argument.MAHV IDEA• The predicate logic system is correct and complete; valid arguments and onlyvalid arguments are provable.EXERCISES 7.4For Exercises l-5, decide what conclusion, if any, can be reached from the given hypothesesand justify your answer.l. All flowers are plants. Pansies are flowers.ir 2. All flowers are red or purple. Pansies are flowers. Pansies are not purple.3. Some flowers are purple. All purple flowers are small.4. Some flowers are red. Some flowers are purple. Pansies are flowers.5. Some flowers are pink and have thoms. All thorny flowers smell bad. Every flower thatsmells bad is a weed.

Page 79: Mathematical Structures for Computer Science

sscnotv 1.4 pmdsmer logic 616. Justify each step in the following proof sequence of ·(3¤¢)[P(>¢) —> Q(>¢)] —> l(V-<)P<x> -> <3»¤)Q(>¢)]1. Gx>[P(xJ —> Q<¤<)l2. P(a) —> Q(u)3. (Vx)P(x) ‘4. P(a)5. QM)6. <Elx>Q(x>7. Justify each step in the following proof sequence ofG>¤>P<¤¤> A (V¤¤)<P(x> —> Q(x>> -> (3xJQ<xJ1. (Elr)P(x>2. (V¤<)(P<>¤> —> Q(>¤>>3. Pm)4. P(a) -> Qu)5- QM)6. (E|>¤>Q(>¤>* 8. Considerthe w1‘f(V¤)l(Ely)P(¤. y) A (Ely)Q<¤¤.y)l —> (Vx>(3y)[P(x. y) A Q<x. y)l-n. Find an interpretation to prove that this wff is not valid.li. Find the flaw in the following ‘*pr¤¤f’ of this wif.1. (V¤¤>l(3y)F(x. y) A <3y)Q<x. y>l hy?2. (\‘/x)lP<x. ¤> A Qtx. all 1. ei3. (V¤<>(3y>[P(x.y> A Q<x.y>l 2. es9. Consider the wff (Vy)(3x)Q(x. y) —>·(Eix)(Vy)Q(x, y).a. Find an interpretation to prove that this wff is not valid.b. Find the flaw in the following "proof’ of this wff.1. (Vy>(3x>Q<x. y> hyv2. (3>¤JQ<x. y) 1. ui3- Q(¤. y) 2. ei4. (Vy)Q(a. y) 3. us5. (ilx)(Vy)Q<x. y) 4. esIn Exercises 10-14, prove that each wff is a valid argument.* 10. (Vx)P(x) —> (Vx)[P(x) v Q(x)]11. <Vx>P(><) A Gx>Q<¤¤> —> <3v)lP<x> A Q<x>l12. <i\><)(5y>P(x. y) —> Gy)(5¤¤>P<x. y)13. (V¤¤><Vy)Q<¤¤. yl —> (\‘/y)(V>¤>Q(>¤. y)14. (Vx)P(x) A (3x)[P(x)]’ —> (Ex)Q(x)I.n Exercises 15-27, either prove that the wff is a valid argument or give an interpretation i.nwhich it is false,1- 15. (3x)[A(x) A B(x)] ->(Elx)A(x) A (3x)B(x)16. (3x)[R(x) v s(x)] -» (Elr)R(r) v (Elx)s(x)

Page 80: Mathematical Structures for Computer Science

62 cuawsn 1 rama: iagir17, (VX)[P(·¤) -> Q(x)] —>[(V>¢)P(¤¢) —> (V¤¢)Q(¤¤)]18, (Vx)(P<x>>' —> <Vx)<P(¤> —> Qw)19, i(Vx>P<x> —> <Vx>Q(x>l —> <Vx)lP(x) —> Q(><>l* 20, (3x><VyQ<x. yi —> (\‘/y)(El¤<>Q<x. YJ21, (Vx>P<x> V <ilx)Q(>¢J —> (\‘/x)iP<x> V Qtx>l22, (\‘/x>[A<x> —> Btxll —> l(3x>A(x> —> (5¤¤>B<x>l23, (\‘/y>[Q(x, y> —> P(xJl —> iGy>Q(r. y) —> P(><)l* Z4, [P0) -> (5y)Q(>¢, y)] -> (3y)[P(>¢) -> Qtr. yl]25, (\‘/x>(P<x> V Q<x>> A <5x>Q(¤<) —> Gx>P(x))26, (3x>[P<xJ —> Q(>¤>l A (\‘/y>iQ(,v) —> RLv>l A (V¤<>P<x) —> (3>¤>R(x>27- (V¤¤)(Vy)i<P(¤<) A 5(¤¢, yl) —> Q(y)l A G>¤)B(>¤) A (V¤¤>(B(x) —> PDO) A <V>¤>(Ely)$<>¤. y)->Gx>Q(¤<)28. The Greek philosopher Aristotle (384-322 ec.) studied under Plato and tutored Alexanderthe Great. His studies of logic influenced philosophers for hundreds of years. I-lis four"perfe<:t" syllogisms are identified by the names given them by medieval scholars. Foreach, formulate the argument in predicate logic notation and then provide a proof.a. "Barbara"All M are P.All S are M.Therefore all S are P.b. "Celarent"No M are P.All S are M.Therefore no S are P.c. "Darii"A11 M are P.Some S are M.Therefore some S are P.d. "Ferio"No M are P.Some S are M.Therefore some S are not P.Using predicate logic, prove that each argument in Exercises 29-37 is valid. Use the predicatesymbols shown.29. Some plants are flowers. All flowers smell sweet. Therefore, some plants smell sweet.Ptx). F (X). S<x)t 30. Every crocodile is bigger than every alligator. Sam is a crocodile. But there is a snake,and Sam isn't bigger than that snake. Therefore, something is not an alligator.C(x). AU). Btw. y). S, 50031. There is an astronomer who is not nearsighted. Everyone who wears glasses is near-sighted. Furthermore, everyone either wears glasses or wears contact lenses. Therefore.some astronomer wears contact lenses. A(x), N(x), G(x), C(x)ir 32. Every member of the board comes from industry or govemment. Everyone from govem-ment who has a law degree is in favor of the motion. John is not from industry, but he. does have a law degree. Therefore, if John is a member of the board, he is in favor ofthemotion. M(x), I(x), G(x), L(x), F(x), jV I

Page 81: Mathematical Structures for Computer Science

sscnotv 1.s mgie Programming 6333. There is some movie star who is richer than everyone. Anyone who is richer than anyoneelse pays more taxes than anyone else does. Therefore. there is a movie star who paysmore taxes than anyone. M(x), R(x, y), T(x. y)34. Every computer science student works harder than somebody, and everyone who worksharder than any other person gets less sleep than that person. Maria is a computer sciencestudent. Therefore, Maria gets less sleep than someone else, C(x), W(x. y), S(x, y), m35. Every ambassador speaks only to diplomats, and some ambassador speaks to someone.Therefore, there is a diplomat. A(x), S(x, y), D(x)36. Some elephants are afraid of all mice. Some mice are small. Therefore there is an ele·phant that is afraid of something small. E(x), M(x), A(x, y), S(x)37. Every farmer owns a cow. No dentist owns a cow. Therefore no dentist is a fanner.F<x>» Cir). 0<x, y). D<x>38. Prove thatl<V¤<JP(><)l' <—> G>¤>[F(>¢)l'is valid. (Hint: Instead of a proof sequence, use Example 32 and substitute equivalentexpressions.)39. The equivalence of Exercise 38 says that if it is false that every element of the domain hasproperty P, then some element of the domain fails to have property P, and vice versa. Theelement that fails to have property P is called a counterexample to the assertion that everyelement has property P. Thus a counterexample to the assertion(Vx)(x is odd)in the domain of integers is the number l0. an even integer. (Of course, there are lots ofother counterexamples to this assertion.) Find counterexamples in the domain of integersto the following assertions. (An integerx > l is prime if the only factors of x are l and x.)a. (Vx)(x is negative)b. (Vx)(x is the sum of even integers)c. (Vx)(x is prime —> x is odd)d. (Vx)(xprime —> (-1)* = -1)e. (Vx)(x prime —> 2* — l is prime)SECTION 1.5 Logic Programming

The programming languages with which you are probably familiar, such as C+ + and J ava, areknown as procedural languages. Much of the content of a program written in a procedural lan-guage consists of instructions to carry out the algorithm the programmer believes will solve theproblem at hand. The programmer, therefore, is telling the computer how to solve the problemin a step-by-step fashion.Some programming languages, rather than being procedural, are declarative languages ordescriptive languages. A declarative language is based on predicate logic; such a languagecomes equipped with its own rules of inference, A program written in a declarative languageconsists only of statements—actually predicate wffs—that are declared as hypotheses. Execu-tion of a declarative program allows the user to pose queries, asking for infomration about

Page 82: Mathematical Structures for Computer Science

64 CHAPTER 1 For-malmgicpossible conclusions that can he derived from the hypotheses. After obtaining the user’s query,the language turns on its "inference engine" and applies its rules of inference to the hypothesesto see which conclusions fit the user‘s query. The program, remember, contains only thehypotheses, not explicit instructions as to what steps to perform in what order. The inferenceengine of the language acts behind the scenes, so to speak, to construct a proof sequence. It isthe mechanical nature of applying inference rules that makes this "automated theorem proving“possible,PrologThe programming language Prolog, which stands for PROgrarnming in LOGic, is a declarativeprogramming language. The set of declarations that constitutes a Prolog program is also knownas a Prolog database. Items in a Prolog database take on one of two forms, known in Prolog asfacts and ruler. (Prolog rules, however, are just another kind of fact and should not be confusedwith a rule of inference.)Prolog facts allow predicates to he denned by stating which items in some domain of inter-pretation satisfy the predicates. As an example, suppose we wish to create a Prolog programthat describes food chains in a given ecological region. We might begin with a binary predicateeat. We then describe the predicate by giving the pairs of elements in the domain that make eartrue. Thus we might have the factsear(bear, fish)ear(bear, fox)eat(deer, grass)in our database. (The exact details of Prolog statements vary from one Prolog implementationto another, so we are only giving the spirit of the language here by using a Prolog-likepseudocode.) Here "bear," "fish," "fox," “deer," and "grass" are constants because they repre-sent specific elements in the domain. Because the domain itself is never specified except bydescribing predicates, at this point we may take the domain to consist of "bear," "fish," "fox,""deer," and "grass," It is up to the user to maintain a consistent understanding and use of thepredicates in a Prolog program. Thus .eut(bear, fish)can be used either to represent the fact that bears eat f'rsh or the fact that fish eat bears! Weimpose the convention that eur(x, y) means "x eats y."We could add descriptions of two unary predicates, animal and plant to the database byadding the factsanimal(bear)arrimal(fish)animal(fox)animal(deer)plant(grass)Armed with this Prolog program (database), we can pose some simple queries.

Page 83: Mathematical Structures for Computer Science

SECTION 1.5 mgic Programming 65EXAMPLE 37 The query `is(animal(bear))merely asks if the fact anim¤l(bear) is in the database, Because this fact is in the database,Prolog would respond to the query by answering yes, (This is a one-step proof sequence—norules of inference are required.) Further dialogue with Prolog could includeis(eut(deer, grass))yesis(eat(bear, rabbit))no •Queries may include variables, as shown in the next example.EXAMPLE 38 The querywhich(x: eat(bear, x))producesEshfoxas a response. Prolog has answered the query by searching the database for all facts that matchthe pattem ear(bear, x), where x is a variable. The answer "tish" is given first because the rulesare searched in order from top to bottom. •Queries may contain the logical connectives and, ur, and not.g Given the databaseeat(bear. fish)eat(bear, fox)eattdeer. grass)unimal(bear)animal(fish)animal(fox)arrimal(deer)plam(grass)what will be Prolog‘s response to the querywhlch(x: mr(x, y) and planz(y)) •

Page 84: Mathematical Structures for Computer Science

ss cuavren 1 Formal LogicThe second type of item in a Prolog database is a Prolog rule. A rule is a description of apredicate by means of an implication. For example, we might use a rule to detine a predicate ofPfeyfprey(x) if eatty, x) and animal(x)This says thatx is a prey if it is an animal that is eaten. Ifwe add this nrle to our database, thenin response to the querywhlch(x: prey(x))we would getfishfoxHorn Clauses and ResolutionHow do Prolog facts and rules relate to more formal predicate logic'! We can describe the factsin our database by the wffsE(b./i)E(b. fn)E<d. glA(b)AU?}A(f<>>A(d)P(s>and the rule by the wffE(y r) A A(x) —> Pr(x)Universal quantiiiers are not explicitly part of the rule as it appears in a Prolog program. butProlog treats the rule as being universally quantified.(\‘/yJ(V><>lELv. ><J A Aw) —> Pr<x>land repeatedly uses universal instantiation to strip off the universal quantiiiers and allow thevariables to assume in nlm each value ofthe domain.Both facts and rules are examples of Hom clauses. A Horn clause is a wff composed ofpredicates or the negations of predicates (with either variables or consrants as arguments)joined by disjunctions, where at most one predicate is unnegated. Thus the factEid, g)is an example of a Horn clause because it consists of a single unnegated predicate. The wff` [Em ¤<>]' V lA(x)]' V Pwr)

Page 85: Mathematical Structures for Computer Science

sscnotv 1.5 regis Programming svis an example of a Hom clause because it consists of three predicates joined by disjunctionwhere only Pr(x) is unnegated. By De M0rgan’s law, it is equivalent to[E(y x) A A(x)]' V Pr(x)which in tum is equivalent toE(y x) A A(x) —> Pr(x)and therefore represents the rule in our Prolog program._; The rule of inference used by Prolog is called resolution. 'l\vo Hom clauses in a Prolog"‘ " database are resolved into a new Hom clause if one contains an unnegated predicate thatjj matches a negated predicate in the other clause. The new clause elimi.nates the matching termand is then available ro use in answering the query. For example,ae is warm A(a)earsvene [A(“)]t V BU,)—e~i mss‘°‘“"*° resolves to B(b), This says that fromAM), [A(u)l' V Blk)which is equivalent toA(a),A(z1) —> B(b)Prolog infersBtb)which is just an application of modus ponens. Therefore Prolog‘s rule of inference includesmodus ponens as a special case,l.n applying the resolution rule, variables are considered to "match" any constant symbol.(This is the repeated application of universal instantiation,) In any resulting new clause, thevariables are replaced with their associated constants i.n a consistent manner. Thus, in responseto the query "Which x is a prey'?" Prolog searches the database for a rule with the desired pred-icate Pr(x) as the consequent. It finds[E0, x)l' V [A(x)l' V Prtx)It then proceeds through the database looking for other clauses that can be resolved with thisclause, The first such clause is the fact E(b,ji). These two clauses resolve into[At/7>l’ V PHJ?)(Note that the constant ji has replaced x everywhere.) Using this new clause, it can be resolvedwith the fact A(/i) to conclude Pr(fi). Having reached all conclusions possible from resolutionwith the fact E(b, fi), Prolog backtracks to search for another clause to resolve with the ruleclause; this time around it would find E(b.fa).As a more complex example of resolution, suppose we add the rulehunxed(x) if prey(x)`

Page 86: Mathematical Structures for Computer Science

ss CHAPTER 1 rama! logicto the database. This rule in symbolic form is[Pr<¤¤>] —» H0:)or, as a Hom clause.[Pr(><)l' V HU)It resolves with the rule detining prey[EU. x>l' V [A(¤¤)l’ V Pr(x>to give the new rule[E(y. >¤>l' V [A(¤<Jl' V HOOThe querywhich(x: hunrea'(x))will use this new rule to concludeHshfoxEXAMPLE 39 Suppose that a Prolog database contains the following entries:eaz(bear, Hsh)euz(iish, Little-fish)euz(little-{ish, algae)euz(racco0n, Fish)euz(bear, raccoon)ear(bear, fox)eaz(f0x, rabbit)ear(rabbit, grass)ear(bear. deer)ear(deer. grass)eat(wildcat. deer)animal(bezu·)animal(iish)animal(little-fish)animal(racc¤on)animul(fox)animul(rabbit)animul(deer)animul(wildcat)planz(grass)planz(a1gae)_ przy(x) if eur(y x) and unimal(x)

Page 87: Mathematical Structures for Computer Science

SECTION 1.: logic Programming G9Then the following dialog with Prolog could take place: ’ls(anima1(1·¤bbit))ls(eur{wildcat, grass))

which(x: ear(x, fish))bearraccoonwhich(x. y: eat(x, y) and plunx(y))little-fish algaerabbit grassdeer grasswhich(x: prey(x)){ishLittle·fishfishraccoonfoxrabbitdeerdeerNote that fish is listed twice as satisfying the last query because fish are eaten by bear (fact l)and by raccoon (fact 3). Similarly. deer are eaten by both bear and wildcat. •a. Formulate a Prolog rule that defines the predicate predator.b. Adding this rule to the database of Example 39, what would be the response to the fol-lowing query?whlch(x: pmdamr(x)) •RecursionProlog rules are implications. Their antecedents may depend on facts, as inprey(x) if ear(y 1) and anima1(x)

Page 88: Mathematical Structures for Computer Science

70 CHAPTER 1 rormaltngieor on other rules, as inhunred(x) lfprey(x)The antecedent of a mle may also depend on that rule itself, in which case the rule is defined interms of itself. A definition in which the item being defined is itself part of the definition iscalled a recursive deinition.As an example, suppose we wish to use the ecology database of Example 39 to study foodchains. We can then define a binary reladon infaad·chain(x, y). meaning "y is in x's foodchain." This, in tum, means one of two things:1. x eats y directly

2. x eats something that eats something that eats something that eats y.Case 2 can be rewritten as follows:2'. x eats z and y is in z’s food chain.Case 1 is simple to test from our existing facts. but without (Z'), infoad-chain means nothingdifferent from ear. On the other hand, (Z') without (1) sends us down an infinite path of some-thing eating something eating something and so on. with nothing telling us when to stop,Recursive definitions always need a stopping point that consists of specific information.The Prolog rule for inf0¤d·chuirr incorporates (1) and (2'):infand-ch¤in(x. y) if euttx, y)infood-chnin(x. y) if eat(x, z) and infoud-chzzin(z, y)It is a recursive rule because it defines the predicate inybad-chain in terms of infoadwhuin. Arecursive rule is necessary when the predicate being described is passed on from one object tothe next. The predicate infaod-chain has this property:infnod-chain(x, y) A infuud-chuin(y, z) —> infood-chain(x. z)EXAMPLE 40 After the infoud-chain rule is added to the database of Example 39, the fol-lowing query is made:which(y: infoad-chain(bear, y))The response follows (numbers are added for reference purposes):1. fish 5. little-fish 9. algae2. raccoon 6. algae l0. rabbit3. fox 7. fish ll. grass4. deer 8. little-fish 12. grass

Page 89: Mathematical Structures for Computer Science

szcrio/v 1.5 Logic Programming 71Prolog applies the simple case of ’infoud-chuin(bear, y) if eat(bear, y)First, obtaining answers l through 4 directly from the facts eut(bear. fish), ear(bear, raccoon),and so on. Moving to the recursive case.infond-chain(bear, y) if ear{bear, z) and infoad-chain(z, y)a match of eat(bear, z) occurs with z equal to "Flsh." Prolog then looks for all solutions to therelation infoad-chain(tish, y). Using first the simple case of infaod-chain, a match occurs withthe fact eat(iish, little-fish). This results in response 5, little-fish. There are no other facts of theform eat(fish, y), so the next thing to try is the recursive case of infaod-chain(fish. y):infzmd-chain(fish. y) if ear(fish, z) and iny’¤ad·chzzin(z, y)A match of eaz(fish. z) occurs with z equal to "l.ittle-f1sh." Prolog then looks for all solutions tothe relation infaari-chain(litt.le-fish, y). Using the simple case of infood-chain, a match occurswith the fact eat(little-fish, algae). This results in response 6, algae. There are no other facts ofthe form eat(little-Hsh, y), so the next thing to try is the recursive case of infood-chain(little-fish, y):iny"oad-chuin(little-fish, y) if eat(little-Esh. z) and infood-chain(z, y)A match of ear(little-Fish. z) occurs with z equal to "algae." Prolog then looks for all solutionsto the relation infoad-chain(a1gae, y). A search of the entire database reveals no facts of theform zat(a1gae, y) (or ear(algae. z)), so neither the simple case nor the recursive case of infaod-chain(algae, y) can be pursued further.bear @bear raccoonbear grbear egbear fishfish lirtle -fishlittle -iish algaeFIGURE 1.2Figure 1.2 shows the situation at this point. Prolog has reached a dead end witl1 infavd-chairr(algae. Y) and will backrrack up the path. Because there are no other facts of the formeat(litt.le-fish, z), the search for solutions to infaad-chain(little-fish, y) terminates. Then, becausethere are no other facts of the form ear(fish, z), the search for solutions to infand-chain(iish, y)terminates. Backing up still further, there is another match of eat(bear, z) with z equal to "rac-coon" that will generate another search path. •

Page 90: Mathematical Structures for Computer Science

72 ctnrtza 1 Formal LogicIn Example 40, once Prolog began to investigate infood-chain(tish, y), all query answersthat could be obtained from exploring this path (responses 5 and 6) were generated before otheranswers (responses 7-12). Exploring as far as possible down a given path and then backtrack-ing up that path before exploring other paths is called a depth-Erst search strategy,Trace the execution of the Prolog program of Example 40 and explain whyresponses 7-12 occur. •Expert SystemsMany interesting applications programs have been developed, in Prolog and similar logicprogramming languages, that gather a database of facts and rules about some domain andthen use this database to draw conclusions. Such programs are known as expert systems,knowledge-based systems, or rule-based systems. The database in an expert system attemptsto capture the knowledge ("elicit the expertise”) of a human expert in a particular Held, includ-ing both the facts known to the expert and the expert‘s reasoning path in reaching conclusionsfrom those facts. The completed expert system not only simulates the human expert’s actionsbut can be questioned to reveal why it made certain choices and not others.Expert systems have been built that simulate a medical specialist's diagnosis from a patient’ssymptoms, a factory manager`s decisions regarding valve control i.n a chemical plant based onsensor readings, the decisions of a fashion buyer for a retail store based on market research, thechoices made by a consultant specifying a computer system configuration based on customerneeds, and many more. The challenging part of building an expert system lies in extracting allpertinent facts and rules from the human expert.SECTION 1.5 ReviewTECHNIQUESW • Fomiulate Prolog-like facts and mles.W • Formulate Prolog-like queries.• Determine the answer(s) to a query using a Prolog database.MAIN IDEA• A declarative language incorporates predicate wffs and rules of inference to drawconclusions from hypotheses. The elements of such a language are based onpredicate logic rather than instmctions that carry out an algorithm.EXERCISES 1.5Answers 1-6 refer to the database of Example 39; find the results of the query in each case.1. Is(ear(bear, little-fish))2. Is(eat(fox, rabbit))* 3. which(x: eaz(raccoon, x))

Page 91: Mathematical Structures for Computer Science

sEcn0N 1.5 login Programming 734, which(x: eat(x, grass)) .5, which(x: ea1(bear, x) and eux(x, rabbit))6, which(x: prey(,r) and n0t(eat(fox, x)))* 7, Formulate a Prolog mle that defines "herbivore" to add to the database of Example 39.8, If the rule of Exercise 7 is included in the database of Example 39, what is the response tothe following query?which(x: hcrbivore(x))9. A Prolog database contains the following, where horror, y) means "x is y’s boss" and.ruperviror(x, y) means "x is y’s supervisor":ba.v.r(lvfike, Joan) , »'bos.r(Judith, Mike) — ·boss(Anita, Judith) , `bo:.r(Judith, Kim) "‘ ”‘imqmm, Em-aqua) ·_ f .boss(Anita, Sam)lms.v(Enrique, Jefferson) .r {Klm.v.v(Mike, I-Jamal)supervi.v0r(x, y) if boss(x. y)rupervi.rm·(x, y) if boss(x, 1) and .rupervisur(z, y)Find the results of the following queries:a. which(x: borr(x, Sarn))b. which(x: bo:.v(Judith, x))c. which(x: supervisar(Anita, x))10. Declare a Prolog database that gives information about states and capital cities. Somecities are big, others small. Some states are eastem, others are westem.a. Write a query to find all the small capital cities.b. Write a query to find all the states with small capital cities.c. Write a query to find all the eastern states with big capital cities.d. Formulate a rule to define cosmopolitan cities as big capitals of westem states.e. Write a query to fmd all the cosmopolitan cities.it 11. Suppose a Prolog database exists that gives information about authors and the books theyhave written. Books are classified as either fiction. biography, or reference.a. Write a query to ask whether Mark Twain wrote Hound of the Barkervilles.b. Write a query to find all books written by William Faulkner.c. Formulate a rule to define nonfiction authors.d. Write a query to find all nonfiction authors.12. Suppose a Prolog database exists that gives infom1ation about a family. Predicates ofmale, female, and parent-of are included.a. Formulate a rule to define father-ofb. Formulate a rule to define daughzenofat c. Formulate a recursive rule to define uncertor-of

Page 92: Mathematical Structures for Computer Science

74 cHAP‘rEa 1 Harmal Logic13. Suppose a Prolog database exists that gives information about the parts in an automobileengine. Predicates of big, small, and part-of are included.a. Write a query to find all small items that are part of other items.b. Write a query to find all big items that have small subitems.c. Fomrulate a recursive rule to define component-of14, Suppose a Prolog database exists that gives information about the ingredients i.n the menuitems of a restaurant. Predicates of dry liquid, perishable, and ingredient-of are included.a. Write a query to find all the dry ingredients of other ingredients.b. Write a query to find all perishable ingredients that contain liquid subingredients.c. Formulate a recursive rule to defrnefaund-in.SECTION 1.6 Proof of Correctness

As our society becomes ever more dependent on computers, it is more and more important thatthe programs computers run are reliable and error-free. Program verlhcation attempts toensure that a computer program is correct. "Correctness“ has a narrower definition here than ineveryday usage. A program is correct if it behaves in accordance with its specifications, How-ever. this does not necessarily mean that the program solves the problem that it was intended tosolve; the progranfs specifications may be at odds with or not address all aspects of a client'srequirements. Program validation, which we won’t discuss further, attempts to ensure that theprogram indeed meets the client's original requirements, In a large program development proj-ect, "program V & V" or "software quality assurance" is considered so important that a groupof people separate from the programmers is often designated to carry out the associated tasks.Program verification may be approached both through program testing and through pmofofcorrectness. Program testing seeks to show that particular input values produce acceptableoutput values. Program testing is a major part of any software development effort, but it is well-lmown folklore that "testing can prove the presence of errors but never their- absence." If a testrun under a certain set of conditions with a certain set of input data reveals a "bug" in the code,then the bug can be corrected. But except for rather simple programs, multiple tests that revealno bugs do not guarantee that the code is bug-free, that there is not some error lurking in tl1ecode waiting to strike under the right circumstances.As a complement to testing, computer scientists have developed a more mathematicalapproach to "prove" that a program is correct. Proof of correctness uses the techniques of a for-mal logic system to prove that if the input variables satisfy certain specified predicates or proper-ties, the output variables produced by executing the program satisfy other specified properties.To distinguish between proof of correctness and program testing, consider a program tocompute the length c of the hypotenuse of a right triangle, given positive values rz and b for thelengths of the legs. Proving the program correct would establish that whenever rz and b satisfythe predicates u > 0 and b > O, then after the program is executed, the predicate az + bz = czis satisfied, Testing such a program would require taking various specific values for u and b,computing the resulting c, and checldng that az + bz equals cz in each case. However, only rep-resentative values for n and b can be tested, not all possible values.Again, testing and proof of correctness are complementary aspects of program verification.All programs undergo program testing; they may or may not undergo proof of correctness aswell. Proof of correctness generally is applied only to small and critical sections of code rather· than to the entire program,

Page 93: Mathematical Structures for Computer Science

SECTION 1.6 Prool of Corrermess 75Assertions ·Describing proof of correctness more fonnally. let us denote by X an arbitrary collection ofinput values to some program or program segment P. The actions of P transform X into a cor-responding group of output values Y; the notation Y = P(X) suggests that the Y values dependon the X values through the actions of program P.A predicate Q(X) describes conditions that the input values are supposed to satisfy. Forexample, if a program is supposed to find the square root of a positive number. then X consistsof one input value, x, and Q(x) might be **1 > 0." A predicate R describes conditions that theoutput values are supposed to satisfy. These conditions will often involve the input values aswell. so R has the fonri R(X. Y) or R[X. P(X)]. In our square root case, if y is the single outputvalue, then y is supposed to be the square root of x, so R(x. y) would be "y’ = x." Program P iscorrect if the implication(VX>(Q(X) —> RIX. P(X)l) (1)is valid. In other words, whenever Q is true about the input values, R should be true about theinput and output values. For the square root case, (1) is(Vx)(x > O -> [P(x)]7 = x)The implication (1) is standard predicate wff notation, but the traditional program correctnessnotation for (1) is( Q)P(Rl (2){Q}P(R) is called a Hoare triple, named for the British computer scientist Anthony Hoare,Condition Q is called the precondition for program R and condition R is the prmtcundition. Inthe Hoare notation, the universal quantifier does not explicitly appear; it is understood.Rather than simply having an initial predicate and a final predicate. a program or programsegment is broken down into individual statements si. with predicates inserted between state-ments as well as at the beginning and end. These predicates are also called assertions becausetl1ey assen what is supposed to be true about the program variables at that point in the program.Thus we have(Q}so{R,)sr(R,)sr-1(Rlwhere Q, Rl. R1. , Rn = R are assertions. The intermediate assertions are often obtained byworking backward from the output assertion R.

Page 94: Mathematical Structures for Computer Science

76 cuarraa 1 rwmal LogicP is provably correct if each of the following implications holds:{Ql¤.,(R,l{R,ls.(R2l(R,ls,(R3l(Ri-. 15.-MR}A proof of correctness for P consists of producing this sequence of valid implications, that is.producing a proof sequence of predicate wffs. Some new rules of inference can be used, basedon the nature of the program statement rl.Assignment RuleSuppose that statement sl is an assignment statement of the form x = e; that is, the variable xtakes on the value of e, where e is some expression. The Hoare triple to prove correctness ofthis one statement has the form{RJ X = ¤ (Ri,.)For this triple to be valid, the assertions Rl and R, +1 must be related in a particular way.EXAMPLE 41 Consider the following assignment statement together with the given pre-condition and postcondition:(x -1 > 0}x = x - l(x > 0)For every x, if x — 1 > 0 before the statement is executed (note that this says thatx > 1), then after the value of x is reduced by l. it will be the case that x > 0. Therefore(x7l>0}x=x—1(x>0}is valid. •In Example 41. we just reasoned our way through the validity of the wff represented by theHoare triple. The point of predicate logic is to allow us to determine validity in a more mechan-ical fashion by the application of rules of inference. (After all. we don‘t want to just "reason ourway through" the entire program to convince ourselves of its correctness; the programmeralready did that when the program was written!)The appropriate rule of inference for assignment statements is the assignment rule, given inTable 1.18. It says that if the precondition and postcondition are appropriately related, theHoare triple can be inserted at any time in a proof sequence without having to be inferred fromsomething earlier in the proof sequence. This makes the Hoare triple for an assignment state-ment akin to a hypothesis in our previous proofs.

Page 95: Mathematical Structures for Computer Science

srcrroxv 1.6 rmi of canmness 77TABLE 1.18 »From Can Derive Name of Rule Restrictions on Use{R,)s,(RM) Assignment 1. sl has the form x = e.2. R, is Rm with e substitutedeverywhere for x.EXAMPLE 42 For the case of Example 41,(X -1 > 0)x=x-1{X > 0)the triple(x-1>0)x=x- 1 (x>0)is valid by t.he assignment rule. The postcondition isx > 0Substituting x —l for x throughout the postcondition results inx - 1 > 0 or x > 1which is the precondition. Here we didn‘t have to think at all; we just checked that the assign-ment rule had been followed. •According to the assignment rule. what should be the precondition in the fol-\ lowing program segment'?(precondition)·. nt tm lr = yl •eats, Because the assigmnent rule tells us what a precondition should look like based on what apostcondition looks like, a proof of correctness often begins with the final desired postcondi-tion and works its way back up through what the earlier assertions should look like accordingto the assignment rule. Once it has been determined what the topmost assertion must be, acheck is done to see that this assertion is really true.EXAMPLE 43 Verify the correctness of the following program segment to exchange thevalues of x and y:temp = xy = temp

Page 96: Mathematical Structures for Computer Science

7E cnavrsk 1 romwiragir `At the beginning of this program segment. x and y have certain values. Thus we may expressthe actual precondition as x = a and y = b. The desired postcondition is then x = b and y = a.Using the assignment rule, we can work backward from the postcondition to find the earlierassertions (read the following from the bottom to the top).(Y = I1. x = a}temp = xty = b. temp = ul(x = b, temp = u)y = wmvtx = In y = alThe first assertion agrees with the precondition; the assignment rule, applied repeatedly,assures us that the program segment is correct. •Verify the correctness of the following program segment with the preconditionand postcondition shown:(X = 3} `y = 4z=x+y{z = 7} •Sometimes the necessary precondition is trivially true, as shown in tl1e next example.EXAMPLE 44 Verify the correctness of the following program segment to computey = x - 4.Y = ity=y·4Here the desired postcondition is y = x — 4. Using the assignment rule to work backward fromthe postcondition, we get (again. read bottom to top)(x - 4 = x - 4)Y = X{y - 4 = x - 4)y=y·4ty = x · 4)The precondition is always true; therefoéj by the assignment rule, each successive assertion,including the postcondition, is true. •

Page 97: Mathematical Structures for Computer Science

srcmm 1. s Proof uf Currectness 79Conditional Rule .A conditional statement is a program statement of the formif condition B thenP.elsePrend lfWhen this statement is executed, a condition B that is either true or false is evaluated. If B isu·ue, program segment Pl is executed, but if B is false, program segment P2 is executed.A conditional rule of inference, shown in Table 1.19. deterrnines when a Hoare triple(Q)s,lRlcan be inserted in a proof sequence if sl is a conditional statement. The Hoare triple is inferredfrom two other Hoare triples. One of these says that if Q is true and B is true and program seg-ment Pl is executed, then R holds; the other says that if Q is true and B is false and program seg-ment P2 is executed, then R holds. This simply says that each branch of the conditionalstatement must be proved correct.`[XBLE 1.19From Can Derive Name of Rule Restrictions on Use(Q A B) Pl (R), (Q)sl(R) Conditional sl has the form(Q/xB’) P1 (R) it`conditionBthenP.elseP zend ifEXAMPLE 45 Verify the correctness of the following program segment with the precondi-tion and postcondition shown.in = 5}if n > = 10 theny = 100elsey = rr + 1end ifly = 6)

Page 98: Mathematical Structures for Computer Science

so cuavrea 1 rmmaltogieHere the precondition is n = 5. and the condition B to be evaluated is n >= 10. To apply theconditional rule, we must first prove that{n=5a.ndn2l0}y=100(y=6)holds. Remember that this stands for an implication, which will be true because its antecedent,n = 5 and n 2 10, is false. We must also show that(n=5andn<10)y=n+1(y=6)holds. Working back from the postcondition, using the assignment rule. we get»(n+l=6orn=5)y = n + 1ly = 6)Thus{n=5)y=n+l(y=6)is true by the assignment rule and therefore{n=5andn<10)y=n+1(y=6)is also true because the condition rr < 10 adds nothing new to the assertion. The conditionalrule allows us to conclude that the program segment is correct. •,__v Verify the correctness of the following program segment with the preconditionand postcondition shown.lx = 4}lfx < 5 theny=x-1elsey = 7end ifly = 31 •EXAMPLE 46 Verify the correctness of the following program segment to compute max(x, y),the maximum of two distinct values x and y.lx # yllfx >= y thenmax = xelsemax = yendif

Page 99: Mathematical Structures for Computer Science

srcnc/v 1.6 Pmaf of correctness 81The desired postcondition reflects the definition of the maximum, (J: > y and max = x) or(x < y and max = y), The two implications to prove are(xséyandxzy) max =x ((x>yandrnax =x)or(x<yandmax =y))and(x#yandx<y)max=y{(x>yandmax=x)or(x<yandmax=y))Using the assignment rule on the first case (substituting x for max in the postcondition) wouldgive the precondition(x>yAx=x)V(x<yAx=y)Since tl1e second disjunct is always false, this is equivalent to(x > y A x = x)which in tum is equivalent tox > y or x se y and x 2 yThe second implication is proved similarly. •In Chapter 2, we will see how to verify correctness for a loop statement, where a section ofcode can be repeated many times,As we have seen, proof of correctness involves a lot of detailed work. It is a difficult tool toapply to large programs that already exist, It is generally easier to prove correctness while theprogram is being developed. Indeed, the list of assertions from beginning to end specify theintended behavior of the program and can be used early in its design, In addition, the assertionsserve as valuable documentation after t.l1e program is complete.SECTION 1.6 Review

TECHNIQUESW • Verify the correctness of a program segment that includes assignment statements.W • Verify the correctness of a program segment that includes conditional statements.MAIN IDEA• A formal system of rules of inference can be used to prove the correctness ofprogram segments.

Page 100: Mathematical Structures for Computer Science

sz cHAPTER 1 rome: LogicEXERCISES 1.6Note: In the following exercises. * denotes multiplication.* 1, According to the assignment rule, what is the precondition in the following programsegment?{precondition)x=x+llx = y r U2. According to the assignment rule, what is the precondition in the following programsegment?[precondition)x = 2 * .1lx > yl3. According to the assignment rule, what is the precondition i.n the following programsegment?(precondition}x = 3 * x - 1lx = 2 * y · 1)4. Verify the correctness of the following program segment with the precondition and post-condition shown.lx = 1)y=1+3y = 2 * yly = 8}5. Verify the correctness of the following program segment with the precondition and post-condition shown,lx > 0)y=x+2z=y+llz > 3)6. Verify the correctness of the following program segment with the precondition and post-condition shown.lr = 0}z = 2 ·l· x + 1y = z - 1ly = 0)

Page 101: Mathematical Structures for Computer Science

srcnow 1.6 Pmofnf correctness sz7. Verify the correctness ofthe following program segment with the precondition and post-condition shown.(x < 8}z=x-1Y=z-5U<2)·k 8. Verify the correctness of the following program segment to compute y = x(x — l).y = x - 1Y = X * Y9. Verify the correctness of the following program segment to compute y = Zx + 1.Y = 1Y=Y+YY=Y+l* 10. Verify the correctness of the following program segment with the precondition and post-condition shown.U=®if y < 5 thenY=Y+1elseY=5end ifw=1)ll. Verify the correctness of the following program segment with the precondition and post-condition shown.h=Uifx <= 0 thenY = 1elsey = 2 * 1endif(Y = 14)12. Verify the correctness of the following program segment with the precondition and post-condition shown.(x se 0)it`1 > O theny = 2 t XelseY = (*2) * Jr .end ifU>0)

Page 102: Mathematical Structures for Computer Science

84 cHAvTER 1 Formal Logic13. Verify the correctness of the following program segment to compute min(x, y), the mini-mum of two distinct values x and y.{X ie yllf x < = y thenmin = xelsemin = yend lf14. Verify the correctness of the following program segment to compute [xi, the absolutevalue of x, for a nonzero number x.{x we 0)ifx > = 0 thenabs = xelseabs = -xend if15. Verify the correctness of the following program segment with the assertions shown,(z = 3}x = z + 1y = x + 2(y = 6)ify > 0 thenz=y+1elsez = 2 == yend if(z = 7}Chapter 1 ReviewTerminologyalgorithm (p. 11) conjunct (p. 2)antecedent (p. 3) conjunction (p. 2)assertion (p. 75) consequent (p. 3)assignment rule (p. 76) contradiction (p. 8)binary connective (p. 3) correct formal logic system (p. 24)binary predicate (p. 36) correct program (p. 74)complete formal logic system (p. 24) De Morgan's laws (p. 9)conclusion (p. 21) declarative language (p. 63)conditional rule of inference (p. 79) depth-first search (p, 72)conditional statement (p. 79) derivation rule (p. 23)

Page 103: Mathematical Structures for Computer Science

Chapter 1 Review 85descriptive language (p. 63) program testing (p. 74) .disjunct (p. 3) program validation (p. 74)disjunction (p. 3) program verification (p. 74)domain (p. 37) Prolog database (p. 64)dual of an equivalence (p. 9) Prolog fact (p. 64)equivalence (p. 3) Prolog rule (p. 66)equivalence rule (p. 24) proof of correctness (p. 74)equivalent wffs (p. 8) proof sequence (p. 23)existential generalization (p. 53) proposition (p. 2)existential instantiation (p. 51) propositional calculus (p. 21)existential quantiiier (p. 36) propositional logic (p. 21)expert system (p. 72) propositional wff (p. 21)free variable (p. 37) pseudocode (p, 12)Hoare triple (p. 75) recursive definition (p. 70)Hom clause (p. 66) resolution (p. 67)hypothesis (p. 21) rule-based system (p. 72)implication (p. 3) scope (p. 37)inference rule (p. 25) statement (p. 2)interpretation (p. 37) statement letter (p. 2)knowledge-based system (p. 72) statement logic (p. 21)logical connective (p. 2) tautology (p. 8)main connective (p. 6) temary predicate (p. 36)n-ary predicate (p. 36) unary connective (p. 3)negation (p. 3) unary predicate (p. 36)posticondition (p. 75) universal generalization (p. 52)precondition (p. 75) universal instantiation (p. 51)predicate (p, 35) universal quantiiier (p, 35)predicate logic (p. 49) valid argument (pp. 22, 49)predicate wff (p. 37) valid predicate wff (p. 41)procedural language (p. 63) well-formed fomrula (wfl) (p. 6)Self-TestAnswer the following true—false questions without looking back in the chapter.SECTION 1.1l. A contradiction is any propositional wff that is not a tautology.2. The disjunction of any propositional wff with a tautology has the truth value true.3. Algorithm Tuurol0gyTest determines whether any propositional wff is a tautology.4. Equivalent propositional wffs have the same truth values for every truth value assign-ment to the components.5. One of Dc Morgan’s laws states that the negation of a disjunction is the disjunction ofthe negations (of the disjuncts).SECTION 1.26. An equivalence rule allows one wff to be substituted for another in a proof sequence.7. If a propositional wff can be derived using modus ponens, then its negation can bederived using modus tollens.

Page 104: Mathematical Structures for Computer Science

86 CHAPTER 1 rwmal logic8. Propositional logic is complete because every tautology is provable.9. A valid argument is one in which the conclusion is always true.10. The deduction method applies when the conclusion is an implication.SECTION 1.3ll. A predicate wff that begins with a universal quantifrer is universally true, that is, truein al1 interpretations.12. In the predicate wf (Vx)P(.r, y), y is a free variable.13. An existential quantilier is usually found with the conjunction connective.14. The domain of an interpretation consists of the values for which the predicate wffdefined on that interpretation is true.15. A valid predicate wff has no interpretation in which it is false.SECTION 1.416. The inference rules of predicate logic allow existential and universal quantifiers to beadded or removed during a proof sequence.17. Existential instantiation should be used only after universal instantiation.18. P(x) A (Elx)Q(x) can be deduced from (Vx)[P(x) A (3y)Q(y)] using universalinstantiation.19. Every provable wff of propositional logic is also provable in predicate logic.20. A predicate wff that is not valid cannot be proved using predicate logic.SECTION 1.521. A Prolog rule describes a predicate.22. Horn clauses are wffs consisting of single negated predicates.23. Modus ponens is a special case of Prolog resolution.24. A Prolog recursive rule is a rule of inference that is used more than once.25. A Prolog inference engine applies its rule of inference without guidance from eitherthe programmer or the user.SECTION 1.626. A provably correct program always gives the right answers to a given problem.27. lf an assertion after an assignment statement is y > 4. then the precondition must bey 2 4.28. Proof of correctness involves careful development of test data sets.29. Using tl1e conditional rule of inference in proof of correctness involves proving thattwo different Hoare triples are valid.30. The assertions used in proof of correctness can also be used as a program design aidbefore the program is written, and as program documentation.

Page 105: Mathematical Structures for Computer Science

Chapter 1 Review B7Ou the Computer ·

For Exercises 1-5, write a computer program that produces the desired output from the giveninput.1. Input.· Tnith values for two statement lettersA and BOutput: Corresponding truth values (appropriately labeled, of course) forAAB,AVB,A—>B,A<-»B.A'2. Input: Truth values for two statement letters A and BOunzut: Corresponding trutl1 values for the wffsA—>B' and B’A[Av(AAB)]3. Input: Truth values for three statement letters A, B. and COutput: Corresponding truth values for the wffsAv(BAC')->B' and AVC'·»(AvC)'4. Input: Truth values for three statement letters A. B, and C, and a representation of a sim-ple propositional wff. Special symbols can be used for the logical conneetives, and post-fix notation can be used; for example,ABACV for (AAB)vCorA' B A for A ' A BOutput: Corresponding truth value of the wff5. Input: Representation of a simple propositional wff as in the previous exerciseOutput: Decision on whether the wff is a tautology6. If you have a version of Prolog available, enter the Prolog database of Example 39 and per-form the queries there. Also add the recursive rule for infood-chain and perform the querywhiclt(y: in-food-chain(bear, y))

Page 106: Mathematical Structures for Computer Science

4I C H A P T E RPI'OO'fS, RGCUTSIOI1, andAnalysis of AlgorithmsCHAPTER OBJECTIVESAfter studying this chapten you will be able 10:• Attack the proofs of conjectures using the techniques of direct proof, proof bycontraposition, and proof by contradiction.• Recognize when a proof by induction is appropriate and carry out such a proof using eitherthe first or second principle of induction.• Mathematically prove the correctness of programs that use loop statements.• Understand recursive definitions of sequences. collections of objects. and operations on objects.• Write recursive definitions for certain sequences, collections of objects, and operationson objects.• Understand how recursive algorithms execute.• Write recursive algorithms to generate sequences defined recursively.• Find closed-forrn solutions for certain types of recurrence relations.• Analyze algorithms by counting the number of executions of a basic unit of work, eitherdirectly or by solving a recurrence relation.Yon are serving on the city connci1’s Board of Land Management, which is considering a proposalby a private contractor to manage a chemical disposal site. The material to he stored at the sitedegrades to inert matter at the rate or 5% per year The contractor claims that, at this rate or stabi-lization, only about une·third ofthe original active material will remain at the end of 20 years.¤ u E s ·r| 0 ri ls the contractor’s estimate correct?lt is possible to check this estimate by doing some brute-force calculations: if there is this muchinitially, then there will be that much next year, then so rnuch the following year, and so onthrough the 20 years. But a quick and elegant solution can be obtained by solving a recurrencerelation; recurrence relations are discussed i.n Section 2.5.First, however, we consider how to prove "real-world" arguments as opposed to the formalarguments of Chapter l. It is helpful to have an arsenal of techniques for attacking a proof.Direct proof, proof by contraposition. and proof by contradiction are examined in Section 2.l.Section 2.2 concentrates on mathematical induction. a proof technique with particularly wideapplication in computer science. In Section 2.3 we see how, using induction, proof of correct-ness can be extended to cover looping statements.

Page 107: Mathematical Structures for Computer Science

SECTION 2.1 Proof rcchniques 89Section 2.4 discusses recursion, which is closely related to mathematical inductionand isimportant in expressing many definitions and even algorithms. Some sequences defmed recur-sively can also be defined by a formula. Finding such a formula involves solving a recurrencerelation; solution methods for several types of recurrence relations are developed in Section 2.5.The use of recurrence relations to determine the amount of work a particular algorithm must dois explored in Section 2.6.SECTION 2.1 Proof TechniquesTheorems and Informal ProofsThe formal arguments of Chapter l have the form P —> Q, where P and Q may represent com-pound statements. The point there was to prove that an argument is valid-true in all interpre-tations by nature of its intemal form or structure, not because of its content or the meaning ofits component parts. However, we often want to prove arguments that are not universally true,just true within some context. Meaning becomes important because we are discussing a partic-ular subject—graph algorithms or Boolean algebra or compiler theory or whatever—and wewant to prove that if P is true in this context, then so is Q. lf we can do this, then P —> Qbecomes a theorem about that subject. To prove a theorem about subject XXX, we can intro-duce facts about XXX i.nto the proof; these facts act like additional hypotheses. Note that as weadd more hypotheses, the universe of discourse shrinks; we are no longer considering univer-sally valid arguments. only arguments that are true within the context in which the hypotheseshold.'It may not be easy to recognize which subject-specific facts will be helpful or to arrange asequence of steps that will logically lead from P to Q. Unfortunately, there is no formula forconstructing proofs and no practical general algorithm or computer program for proving theo-rems. Experience is helpful, not only because you get better with practice, but also because aproof that works for one theorem can sometimes be modified to work for a new but similartheorem.Theorems are often stated and proved in a somewhat less formal way than the propositionaland predicate arguments of Chapter l. For example, a theorem may express the fact that everyobject in the domain of interpretation (the subject matter under discussion) having property Palso has property Q. The fonnal statement of the theorem would be (Vx)[P(x) —> Q(x)]. But thetheorem would be informally stated as P(x) —> Q(x). If we can prove P(x) —> Q(x) where x istreated as an arbitrary element of the domain, universal generalization would then give(V¤¤>[F(¤¤) ·> Q<¤<)]tAs another example, we may know that all objects in the domain have some property; that is,something of the form (Vx)P(x) can be considered as a subject-specific fact. An informal proofmight proceed by saying "Let x be any element of the domain. Then x has property PJ' (For-mally, we are making use of universal instantiation to get P(x) from (Vx)P(x).)lin the world cr ··prue predicate logic," which is a correct and complete formal system, every true (valid) argument isprovable. But in these more restrictive conrexrs, not everything that is *·true" is neoessarily provahle, no matter howclever we are in adding addirional hypotheses or **axioms." In other words, these systems may not be complete. atthe age or 25, the German logician Kurt Gadel proved in l93l that, using reasonable hypotheses, even elementaryarithmetic is an incomplete system. This shocked the mathematical community ofthe time, which had been depending' on axiomatic sysrerns since the days or Euclid.

Page 108: Mathematical Structures for Computer Science

90 CHAPTER 2 Proofs, Recwsian, and Analysis al AlgorithmsSimilarly, proofs are usually not wiitten a step at a time with fomial justifications for eachstep. Instead, the important steps and their rationale are outlined in narrative form. Such a nar-rative, however, can be translated into a formal proof if required. In fact, the value of a formalproof is that it serves as a sort of insurance—if a narrative proof cannot be translated into a for~mal proof, it should be viewed with great suspicion.To Prove or Not to ProveA textbook will often say, "Prove the following theorem," and the reader will know that thetheorem is true; furthermore, it is probably stated in its most polished form. But suppose youare doing research in some subject. You observe a number of cases in which whenever P is true,Q is also true. On the basis of these experiences. you may formulate a conjecture: P —+ Q. Themore cases you find where Q follows from P, the more confident you are in your conjecture.This process illustrates inductive reasoning, drawing a conclusion based on experience,_ . No matter how reasonable the conjecture sounds, however, you will not be satisfied untilyou have applied deductive reasoning to it as well. In this process, you ¤·y to verify the truthor falsity of your conjecture. You produce a proof of P —> Q (thus making it a theorem), or elseyou find a counterexample that disproves the conjecture, a case in which P is true but Q iseapaévea li false. (We were using deductive reasoning i.n predicate logic when we either proved that a wffrsnjeewe. was valid or found an interpretation in which the wff was false.)If you are simply presented with a conjecture. it may be difficult to decide which of the twoapproaches you should try-to prove the conjecture or to disprove itl A single counterexarnpleto a conjecture is sufficient to disprove it. Of course. merely hunting for a counterexample andbeing unsuccessful does not constitute a proof that the conjecture is true.EXAMPLE 1 For a positive integer rc n factorial is defined as n(n-l)(n-2) I and isdenoted by ni. Prove or disprove the conjecture "For every posiuve integer n, nl S n’."Let’s begin by testing some cases:n nl ni nl S n2l 1 N yes2 2 4 yes3 6 9 yesSo far. this conjecture seems to be looking good. But for the next casen n! nz n! S ni4 24 I6 nowe have found a counterexample./Thefactihat the conjecture is true for n = 1, 2, and 3 doesnothing to prove the conjecture. but the single case n = 4 is enough to disprove it. •Provide counterexamples to the following conjectures:a. All animals living in the ocean are fish.. b. Every integer less than l0 is bigger than 5. •

Page 109: Mathematical Structures for Computer Science

SECIION 2,1 Proof Techniques 91ir a eounterexample is not swimming, what techniques wt we use to try to prove a comjecture7 For the rest of this section, we’ll examine various methods of attacking a proof.Exhaustive ProofWhile "disproof by counterexample" always works, "proof by example" seldom does. The oneexception to this situation occurs when the conjecture is an assertion about a finite collection.ln this case. the conjecture can be proved true by showing that it is true for each member of thecollection, Proof by exhaustion means that all possible cases have been exhausted, although itoften means that the person doing the proof is exhausted as well!EXAMPLE 2 Prove the conjecture "If an integer between 1 and 20 is divisible by 6, then itis also divisible by 3." ("Divisible by 6" means "evenly divisible by 6," that is, the number is anintegral multiple of 6.)Because there is only a finite number of cases. the conjecture can be proved by simply show-ing it to be true for all the integers between l and 20. Table 2.1 is the proof.TABLE 2.1Number Divisible by 6 Divisible by 3199156 yes:6=1><6 yes:6=2><3999101112 yes: l2= 2><6 yes: l2=4><3131415161718 yes:18 = axe yes: is =6x;s1920 .

Page 110: Mathematical Structures for Computer Science

92 CHAPTER 2 Proofs, Recursion, and Analysis nf AlgorithmsEXAMPLE 3 Prove the conjecture "It is not possible to trace all the lines in Figure 2.l with-out lifting your pencil and without retzracing any Linesr"There is only a iinite number of different ways to trace the lines in the figure. Bycareful bookkeeping, each of the possibilities can be attempted, and each will fail. lnChapter 6, we will learn a much less tedious way to solve this problem than proof byFrcunu 2.1 exhaustion. •a. Prove the conjecture "For any positive integer less than or equal to 5, the square of theinteger is less than or equal to the sum of 10 plus 5 times the integer."lz. Disprove the conjecture "For any positive integer, the square of the integer is less thanor equal no the sum of 10 plus 5 times the integer.” •Direct ProofIn general (where exhaustive proof won’t work), how can you prove that P —> Q is true? Theobvious approach is the direct pr0of—assume the hypothesis P and deduce the conclusion Q.A formal proof would require a proof sequence leading from P to Q.Example 4 shows a formal proof that if two numbers are even (t.hat’s the hypothesis P), thentheir product is even (that‘s the conclusion Q). Recall that an even number is a number that isan integral multiple of 2: for example. 18 is even because 18 = 2(9). An odd number is 1 morethan an integral multiple cf 2; for example, 19 = 2(9) + 1.EXAMPLE 4 Consider the conjecturex is an even integer A y is an even integer —> the productxy is an even integerA complete formal proof sequence might look like the following:1. x is an even integer A y is an even integer hyp2. (Vx)[x is even integer —> (3k)(k an integer A x = 2k)] number fact (definitionof even integer)3. at is even integer —> (3k)(k an integer A x = 2k) 2, ui4. y is even integer —> (3k)(k an integer A y = 2k) 2,ui5. x is an even integer 1. si.rn6. (Ek)(k is an integer A x = 2k) 3, 5, mp7. m is an integer A x = 2m 6, ei84 y is an even integer 1, sim9. (3k)(k an integer A y = 2k) t 4, 8,mp10. n is an integer and y = Zn 9, eill. x = 2m 7. siml2. y = 2n l0, sim13. xy = (2m)(2n) ll, 12, substitution ofequals14. xy = 2(2mn) 13, multiplication fact15. m is an integer 7. sim

Page 111: Mathematical Structures for Computer Science

SECTION 2.1 Proof Techniques 9316. n is an integer 10, si.tn g17, 2mn is an integer 15, 16, number fact18, xy = 2(2mn) A 2mn is an integer l4, 17, con19. (Eik)(kan integer A xy = 2k) 18, eg20. (Vx)((3k)(k an integer A x = 2k) —> x is even integer) number fact (definitionof even integer)21. (Elk)(k an integer A xy = 2k) —> xy is even integer 20, ui22. xy is an even integer 19, 2l, mpIt is understood that x and y are arbitrary, but this could be stated by expressing the conjectureas(Vx)(Vy)(x is an even integer A y is an even integer —> the productxy is an even integer)Universal generalization can be applied twice to the result we already have in order to put theuniversal quantitiers on the front. •We‘ll never again do a proof like the one in Example 4. and you won't have to either! Amuch more informal proof would be perfectly acceptable in most circumstances.EXAMPLE 5 Following is an informal direct proof that the product of two even integers iseven. Let x = Zm and y = 2n, where rn and n are integers. Then xy = (2m)(2n) = 2(2mn),where 2mn is an integer. Thus xy has the form Zk, where k is an integer, and xy is therefore even.Notice that we setx = 2m for some integer m (the definition of an even number), but we sety = 2n. In tl1e formal proof of Example 4, the restriction on the use of ei required that we use adifferent multiple of 2 for y than we used for x. Inforrnally, if we were also to set y = 2m, wewould be saying that x and y are the same integers, which is a very special case. •The proof in Example 5 does not explicitly state the hypothesis (that x and y are even), andit makes implicit use of the definition of an even integer. Even in informal proofs, however. it isimportant to identify the hypothesis and the conclusion, not just what they are in words butwhat they really mean, by applying appropriate definitions. If we do not clearly understandwhat we have (the hypothesis) or what we want (the conclusion), we cannot hope to build abridge from one to the other. That's why it is important to know definitions.Give a direct proof (informal) of the theorem "If an integer is divisible by 6,then twice that integer is divisible by 4." •Contrapusition. lf you have tried diligently but failed to produce a direct proof of your conjecture P -> Q, andyou still feel that the conjecture is true, you might try some variants on the direct proof tech-nique. if you can prove the theorem Q' -» P', you can conclude P —> Q by making use of thetautology (Q' —> P') —» (P —> Q). Q' —> P’ is the contrnpositive of P —> Q, and the techniqueof proving P —> Q by doing a direct proof of Q' —> P' is called proof by contraposition. (Thecontraposition rule of inference in propositional logic, Table 1.14, says that P —> Q can bederived from Q’ —> P'.)

Page 112: Mathematical Structures for Computer Science

94 CHAPTER 2 Proofs, Recursion, and Analysis of AlgorithmsEXAMPLE 6 Prove that if the square of an integer is odd. then the integer must be odd.The conjecture is nz odd —> n odd, We do a proof by contraposition and prove n even —>ni even. Let n be even. Then nz = n(n) is even by Example 5, •EXAMPLE 7 Prove that if n + l separate passwords are issued to n students, then some stu-dent gets 2 2 passwords.The contrapositive is "lf every student gets < 2 passwords, then n + l passwords were notissued." Suppose every student has < 2 passwords; then every one of the n students has at mostl password. The total number of passwords issued is at most n, not n + 1, •Example 7 is an illustration of the pigeonhole principle. which we will see in Chapter 3.Q Write the contrapositive of each statement in Practice 5 of Chapter l. •Practice 7 of Chapter 1 showed that the wffs A —> B and B -> A are not equivalent. B —>A isthe converse of A —> B. If an implication is true, its converse may be true or false. Therefore,you cannot prove P —> Q by looking at Q -+ P.EXAMPLE 8 The implication "lfa > 5, then zz > 2" is true, but its converse, "lfa > 2. thena > 5," is false. •% Write the converse of each statement in Practice 5 of Chapter l. •Theorems are often stated in the form "P if and only if Q," meaning P if Q and P only if Q,or Q —> P and P —> Q. To prove such a theorem, you must prove both an implication and itsconverse. Again, the truth of one does not imply the truth of the other,EXAMPLE 9 Prove that the product xy is odd if and only if both x and y are odd integers.We first prove that if x and y are odd, so is xy, A direct proof will work. Suppose that bothx and y are odd. Thenx = Zn + 1 andy = 2m + l, where m and n are integers. Thenxy =(2n + l)(2m + 1) = 4nm + 2m + 2n +1= 2(2nm + m + n)+1.Thishastl1eform2k+l,where lc is an integer. so xy is odd.Next we prove that if xy is odd, bothx and y must be odd, orxyodd —>xoddandyoddA direct proof would begin with the hypothesis that xy is odd, which leaves us little more to say.A proof by contraposition works well because we'll get more useful information as hypotheses.So we will prove(x odd and y odd)' —> (xy odd)’By De Morgan’s law (A A B)’ ¢ A' V B', we see that this can be written asx even or y even —> xy even (1)

Page 113: Mathematical Structures for Computer Science

SECTION 2.1 Proof Techniques 95The hypothesis "x even or y even" breaks down into three cases. We consider each case in tum.l. xeven,y odd: Herex = 2m, y = 2n + 1, and thenxy = (2m)(2n + l) = 2(2mn + m),which is even.2. x odd, y even: This works just like case l.3. x even, y even: Then xy is even by Example 5.This completes the proof of (1) and tl1us of the theorem, •The second part of the proof of Example 9 uses proof by cases, a form of exhaustive proof.It involves identifying all the possible cases consistent with the given information and thenproving each case separately.ContradictionIn addition to direct proof and proof by contraposition, you might use the technique of proof bycontradiction. (Proof by contradiction is sometimes called indirect proof but this term moreproperly means any argument that is not a direct proof.) As we did in Chapter 1, we will let 0stand for any contradiction, that is, any wff whose truth value is always false. (A A A' would besuch a wff.) Once more, suppose you are trying to prove P —> Q. By constructing a truth table,we see that(PAQ'—>0)->(P—>Q)is a tautology, so to prove the theorem P —> Q, it is sufficient to prove P A Q' —> 0. Therefore,in a proof by contradiction you assume that both the hypothesis and the negation of tl1e conclu~sion are true and then try to deduce some contradiction from these assumptions,EXAMPLE 10 Let's use proof by contradiction on the statement "If a number added toitself gives itself, then tl1e number is 0." Let xrepresent any number. The hypothesis is x + x =x and the conclusion is x = 0, To do a proof by contradiction, assume x + x = x and x se 0.Then 2x = x and x se 0. Because x ae 0, we can divide both sides of the equation 2x = 1 by xand arrive at the contradiction 2 = l. Hence. (x + x = x) —> (x = O). •www Example l0 notwithstanding, a proof by contradiction most immediately comes to mind°'°°Z"{ when you Want to prove that something is nor true. It‘s hard to prove that something is not true:mul mn it’s much easier to assume it is true and obtain a contradiction.EXAMPLE 1 1 A well-known proof by contradiction shows that \/2 is not a rational num-ber. Recall that a rational number is one that can be written in the form p/q where p and q areintegers, q si 0, and p and q have no common factors (other than : l).Let us assume that \/2 is rational, Then \/2 = p/q, and 2 = pz/q2, or 2qZ = pz. Then 2 dividesP2, so—since 2 is itself indivisible—2 must divide p. T`his means that 2 is a factor of p, hence4 is a factor of pz, and the equation 2q* = pl can be written as 2q’ = 4x, or qZ= Zx. We see fromthis equation that 2 divides qz; hence 2 divides q. At this point, 2 is a factor of q and a factorof p. which contradicts the statement that p and q have no common factors. Therefore \/2 isnot rational, •

Page 114: Mathematical Structures for Computer Science

96 CHAPTER 2 Proofs, kecursion, and Analysis of AlgorithmsThe proof of Example ll involves more than just algebraic manipulations. It is often neces-sary to use lots of words in a proof.''°’ }·;`, Prove by contradiction that the product of odd integers is not even. (We did adirect proof of an equivalent statement in Example 9.) •Proof by contradiction can be a valuable technique, but it is easy to think we have done aproof by contradiction when we really haven’t. For example. suppose we assume P A Q' andare able to deduce Q without using the assumption Q'. Then we assert Q A Q' as a contradic-tion. What really happened here is a direct proof of P —> Q, and the proof should be rewritten inthis form. Thus in Example l0, we could assmricx + x = x andx ae 0, as before. Then we couldargue that fromx + x = xwe get lx = x and. after subtracting x from both sides, x = 0. We thenhave x = 0 and x ve 0, a contradiction. However, in this argument we never made use of theassurnptionx ee 0; we actually proved directly that x + x = x implies x = 0.Another misleading claim of proof by contradiction occurs when we assume P A Q' and areable to deduce P' without using the assumption P. Then we assert P A P' as a contradiction.What really happened here is a direct proof of Q' —> P', and we have constructed a proof bycontraposition, not a proof by contradiction. In both this case and the previous one, it is not thatthe proofs are wrong; they are just not proofs by contradiction.Table 2.2 summarizes useful proof techniques we have discussed so far.TABLE 2,2Proof Technique Approach to Prove P —> Q RemarksExhaustive proof Demonstrate P —> Q for al.l May be used only to provepossible cases. a Finite number of cases.Direct proof Assume P, deduce Q. The standard approach—usua.llythe thing to try.Proof by contrapositiou Assume Q', deduce P'. Use tl1is if Q' as a hypothesisseems to give more ammunitionthan P would.Proof by contradiction Assume P A Q'; deduce a Use this when Q sayscontradiction. something is not true.SerendipitySerendipity means a fortuitcus happening, or good luck. While this isn‘t really a general prooftechnique, some of the most interesting proofs come from clever observations that we canadmire, even if we would never have thought of them ourselves. We‘ll look at two such proofs,just for fun.EXAMPLE 12 A tennis totmiament has 342 players. A single match involves 2 players.The winner of a match plays tl1e winner of a match in the next round, while losers are elimi-_ nated from the tournament. The 2 players who have won all previous rounds play in the iinal

Page 115: Mathematical Structures for Computer Science

SECTION 2,1 Pmof Techniques 97game. and the winner wins tl1e toumarnent. Prove that the total number of matches to be playedis 341.The hard way to prove this result is to compute 342/2 = 171 to get the numberof matches in the first round, resulting in 171 winners to go on to the second round. For thesecond rotmd, 171/2 = 85 plus 1 left over; there are 85 matches and 85 winners, plus the oneleft over, to go on to the third round. The third round has 86/2 = 43 matches, and so forth. Thetotal number of matches is the sum of 171 + 85 + 43 + ,The clever observation is to note that each match results in exactly l loser, so there must bethe same number of matches as losers in the toumarnent. Because there is only 1 winner, thereare 341 losers, and therefore 341 matches. •EXAMPLE 1 3 A standard 64-square checkerboard is arranged in 8 rows of 8 squares each.Adjacent squares are altemating colors of red and black. A set of 32 1 >< 2 tiles, each covering2 squares, will cover the board completely (4 tiles per row, 8 rows). Prove that if the squares atdiagonally opposite corners of the checkerboard are removed, the remaining board cannot becovered with 31 tiles.The hard way to prove this result is to try all possibilities with 31 tiles and see that they allfail, The clever observation is to note that opposing comers are the same color, so the checker-board with the corners removed has two less squares of one color than of the other. Each tilecovers one square of each color, so any set of tiles must cover an equal number of squares ofeach color and cannot cover the board with the corners removed. •Common DefinitionsMany of the examples in this section and many of the exercises that follow involve elementarynumber theory, that is, results about integers, It's useful to work in number theory when firststarting to construct proofs because many properties of integers, such as what it means to be aneven number, are already familiar. The following definitions may be helpful in working some ofthese exercises.• A perfect square is an integer n such that n = k2 for some integer k.• A prime number is an integer rt > 1 such that n is not divisible by any integersother than 1 and n.• A composite number rr is a nonprime integer; that is, n = ab where a and b areintegerswith 1 <u<nandl<b<n,• For two numbers x andy. 1 < y means y - x > 0.• For two integers n and m, n divides m (n I m) means that m is divisible by n; that is,m = k(n) for some integer k.• The absolute value of a number x. Ixl. is x ifx 2 0 and is -x ifx < 0.SECTION 2.1 ReviewTECHNIQUES• Look for a counterexample.W • Construct direct proofs, proofs by contraposition, and proofs by contradiction.

Page 116: Mathematical Structures for Computer Science

sa CHAPTER 2 Proofs, Recursion, and Analysis ohtgnrithmsMAIN IDEAS• Inductive reasoning is used to formulate a conjecture based on experience.• Deductive reasoning is used either to refute a conjecture by finding a counter-exarnple or to prove a conjecture.• In proving a conjecture about some subject, facts about that subject can be used.• Under the iight circumstances, proof by contraposition or contradiction maywork better than a direct proof.EXERCISES 2.1* 1. Write the converse and the contrapositive of each statement in Exercise 4 of Section 1.1.2. Given the implication P —> Q, then Q' —> P' is the contrapositive of the implication andQ —> P is the converse of the implication. The only remaining variation is the inverse ofthe implication, defined as P' —> Q'. To which of the other three (implication, contraposi-tive, converse) is the inverse equivalent']3. Provide counterexamples to the following statements.a. Every geometric figure with four right angles is a square.b, If a real number is not positive, then it must be negative.c. All people with red hair have green eyes or are tall.d. All people with red hair have green eyes and are tall.4, Provide counterexamples to the following statements.a. The number n is an odd integer if and only if 3n + 5 is an even integer.b. The number ri is an even integer if and only if 3n + 2 is an even integer.5. a, Find two even integers whose sum is not a multiple of 4.b, What is wrong with the following "proof" that the sum of two even nmnbers is amultiple of 47Let x and y be even numbers. Then x = 2m and y = 2m, where m is an integer, sox + y = 2m + 2m = 4m, which is an integral muldple of 4.6. a. Find an example of an odd numberx and an even number y sucb thatx - y = 7.b. What is wrong with the following "proot" that an odd number minus an even numberis always 17Letx be odd and y be even. Then x = 2m + 1, y = 2m, where m is an integer, andx—y=2m+1-2m=l,For Exercises 7-43, prove the given statement.* 7, If n = 25, 100, or 169, then n is a perfect square and is a sum of two perfect squares.8. If n is an even integer, 4 S n S 12, then n is a sum of two prime numbers,9. For any positive integer vi less than or equal to 3. nl < 2".IO. For2s¤s4,n’z2”.11, 0 is an even number.12. The sum of even integers is even (do a direct proof).13. The sum of even integers is even (do a proof by contradiction).ic I4. The sum of two odd integers is even.

Page 117: Mathematical Structures for Computer Science

SECTION 2.1 Pmnf Techniques 9915. The sum of an even integer and an odd integer is odd.16. An odd integer minus an even integer is odd.17. The product of any two consecutive integers is even.18. The sum of an integer and its square is even.* 19. The square of an even number is divisible by 4.20. For every integer n, the number3(nz + 2n + 3)- Zn;is a perfect square.21. If a number x is positive. so is x + 1 (do a proof by contraposition).22, The number n is an odd integer if and only if 3n + 5 = 6k + 8 for some integer k.23. The number n is an even integer if and only if 3n + 2 = 6k + 2 for some integer k.* 24. Forx and y positive numbers. x < y if and only if xz < yl.25.lfx1+ 2x-3 =0,thenx# 2.26. lf n is an even prime number, then rt = 2.* 27. Iftwo integers are each divisible by some integer n, then their sum is divisible by n.28. If the product of two integers is not divisible by an integer n, then neither integer isdivisible by n.29. If n. m, andp are integers and nim and mlp, then nip.20. is n, m, p, and q are integers and nlp and mlq, men nmlpq.31. The sum of three consecutive integers is divisible by 3.* 32. The square of an odd integer equals Sk + 1 for some integer k.33. The difference of two consecutive cubes is odd.34. The sum of the squares of two odd integers cannot be a perfect square. (Hint.- UseExercise 32.)* 35. The product of the squares of two integers is a perfect square.36. For any two numbers x and y, =* 37. For any two nunibersx andy, fx + yl S ixl +38. The value A is the average of the n numbers xl, ::2. ,xn. Prove that at least one ofxl, xl, . xx is greater than or equal to A.39. Suppose you were to use the steps of Example ll to attempt to prove that \/4 is not arational number. At what point would the proof not be valid?40. Prove that \/3 is not a rational number,41. Prove that \/.5 is not a rational number.42. Prove that {/2 is not a rational number.43, Prove that log! 5 is not a rational number (log; 5 = x means 2* = 5).For Exercises 44-65. prove or disprove the given statement.44. 91 is a composite number.* 45. 297 is a composite number.46. 83 is a composite number.47. The difference between two odd integers is odd.48. The difference between two even integers is even.* 49. The product of any three consecutive integers is even.50. The sum of any three consecutive integers is even.

Page 118: Mathematical Structures for Computer Science

100 CHAPTER 2 Proofs, Recursion, and Analysis of Algorithms5l. The product of an integer and its square is even.* 52. The sum of an integer audits cube is even.53. The number n is an even integer if and only if ns + 13 is odd.54, Any positive integer can be written as the sum of the squares of two integers.55. For a positive integer n, rr + é 2 2.56. For every prime number n, n + 4 is prime.57. Forn apositive integer, n > 2, nz — l is not prime.* 58. For every positive integer n, nz + vt + 1 is prime.59. For every positive integer n, 2” + 1 is prime.60. Forn an even integer, n > 2, 2" - 1 is not rime.61. If n, m, andp are integers and nlmp, then arm or nip.62. The product of two rational numbers is rational,* 63. The sum of two rational numbers is rational.64. The product of two irrational numbers is irrational.65. The sum of a rational number and an irrational number is irrational.For Exercises 66-69, use the following facts from geometry and the accompanying figure.• The interior angles of a triangle sum to l80°.• Vertical angles (opposite angles forrned when two lines intersect) are the same size.• A straight angle is 180°.• A right angle is 90°..O66. Prove that the measure of angle 4 is the sum of the measures of angles 1 and 2.*67. Prove that the measure of angle 5 plus the measure of angle 3 is 90°.68. If angle 1 and angle 5 are the same size, then angle 2 is a right angle.69. Prove that the sum of the integers from 1 through 100 is 5050. (Hint: Instead of actuallyadding all the numbers, try to make the same clever observation that the Germanmathematician Karl Frederick Gauss [1777-1855] made as a schoolchild: Group thenumbers into pairs. using 1 and 100. 2 and 99, etc.)SECTION 2 .2 Induction

First Principle 0f InductionThere is one final proof technique especially useful in computer science. To illustrate how thetechnique works. imagine that you are climbing an infinitely high ladder. I-low do you knowwhether you will be able to reach an arbitrarily high rung? Suppose we make the following twoassertions about your climbing abilities:

Page 119: Mathematical Structures for Computer Science

SECTION 2.2 Induction 1011. You can reach the first rung. .2. Once you get to a rung, you can always climb to the next one up. (Notice that thisassertion is an implication.)If both statement 1 and the implication of statement 2 are t:rue, then by statement 1 you can getto the first rung and therefore by statement 2 you can get to the second; by statement 2 again,you can get to the third rung; by statement 2 again. you can get to the fourth; and so on. You canclimb as high as you wish. Both assertions here are necessary. If only statement 1 is tnie, youhave no guarantee of getting beyond the first rung, and if only statement 2 is true, you may pnever be able to get started. Let’s assume that the rungs of tl1e ladder are numbered by positiveintegers—1. 2, 3, and so on.Now think of a specific property a number might have. Instead of "reaching an arbitrarilyhigh rung," we can talk about an arbitrary, positive integer having that property. We use theshorthand notation P(n) to mean that the positive integer n has the property P. How can we usethe ladder-climbing technique to prove that for all positive integers rr, we have P(n)? The twoassertions we need to prove are1, P(1) (1 has property P.)2. For any positive integer k, P(k) —>P(k + 1). (If any number has property P, so doesthe next number.)If we can prove both assertions l and 2, then P(n) holds for any positive integer n, just as youcould climb to an arbitrary rung on the ladder.The foundation for arguments of this type is the first principle of mathematical induction.The first principle of mathematical induction is an implication. The conclusion is a statement ofi ` the form "P(n) is true for all positive integers n." Therefore, whenever we want to prove thatsomething is true for every positive integer rz, it is a good bet that mathematical induction is an. Y. appropriate proof technique to use.=·** To know that the conclusion of this implication is true, we show that the two hypotheses,"* statements l and 2, are true. To prove statement 1, we need only show that property P holds forI the number 1, usually a nivial task. Statement 2 is also an implication that must hold for all k.To prove this implication, we assume for an arbitrary positive integer k that P(k) is true andshow, based on this assumption, that P(k + 1) is true. You should convince yourself that assum-ing that property P holds for the number k is not the same as assuming what we ultimately wantto prove (a frequent source of confusion when one first encounters proofs of this kind). It ismerely the way to proceed with a direct proof that the implication P(k) —» P(k + 1) is true.In doing a proof by induction, establishing the truth of statement l, P(1), is called the basis,or basls step, for the inductive proof. Establishing the truth of P(k) —> P(k + 1) is called the

Page 120: Mathematical Structures for Computer Science

1¤2 CHAPTER 2 Proofs, Recursion, and Analysis nr Algorithmsinductive step. When we assume P(k) to be true to prove the inductive step, P(k) is calledthe inductive assumption, or inductive hypothesis.All the proof methods we have talked about i.n this chapter are techniques for deductivereasoning—ways to prove a conjecture that perhaps was formulated by inductive reasoning.Mathematical induction is also a deductive technique, not a method for inductive reasoning(don’t get confused by the terminology here). For the other proof techniques, we can begin witha hypothesis and string facts together until we more or less stumble on a conclusion. In fact,even if our conjecture is slightly incorrect, we might see what tl1e correct conclusion isin the course of doing the proof. In mathematical induction, however, we must know right at theoutset the exact form of the property P(n) that we are trying to establish. Mathematical induc-tion. therefore, is not an exploratory proof technique—it can only confirm a correct conjecture.Proofs by Mathematical InductionSuppose that the ancestral progenitor Smith married and had two children. Let‘s call these twochildren generation l. Now suppose each of those two children had two children; then in gen-eration 2, there were four offspring. This continued from generation unto generation. Tl1eSmith family tree therefore looks like Figure 2.2. (This looks exactly like Figure l.lb, wherewe looked at the possible T-F values for rt statement letters.)Generation Offspring1 2 = 212 4 = 2*2 s = 23From 21lt appears that generation n contains 2** offspring. More formally, if we let P(n) denote thenumber of offspring at generation n. then we guess that .P(n) = 2**We can use induction to pmve that our guess for P(n) is correctThe basis step is to establish P(l), which is the equation_ P(1) = 2* = 2This is true because we are told that Smith had two children. We now assume that our guess iscorrect for an arbitrary generation k, k 2 l, that is, we assumeP(k) = 2*and try to show thatP(k + 1) = 2***

Page 121: Mathematical Structures for Computer Science

SECTION 2.2 Induction 103In this family. each offspring has two children; thus the number of offspring at generation k + 1 ·will be twice the number at generation k. or P(k + 1) = 2P(k). By the inductive assumption,P(k) = 2*. soPtk + 1) = 2P(k) = 2(2") = 2**so indeedP(k + 1) = 2***This completes our proof. Now that we have set our mind at ease about the Smith clan, we canapply tl1e inductive proof technique to less obvious problems.EXAMPLE 1 4 Prove that the equation1+3+5-+···+(2n-1)=n’ (l)is true for any positive integer n. Here the property P(n) is equation (1). (Notice that P(n) is aproperty of rz. or—in language from Chapter l—a unary predicate. It is a statement about n,expressed here as an equation. Thus it is incorrect to write something like P(n) = 1 -1- 3 + 5 ++ (2n -1).)The left side of this equation is the sum of all the odd integers from 1 to 2n - 1. Althoughwe can verify the truth of this equation for any particular value of n by substituting that valuefor n, we cannot substitute all possible positive integer values. Thus a proof by exhaustion doesnot work. A proof by mathematical induction is appropriate.The basis step is to establish P(1), which is equation (1) when rt has the value 1. orP(1): 1 = 12This is certainly true. For the inductive hypothesis. we assume P(k) for an arbitraiy positiveinteger k. which is equation (1) when n has the value k. orP(k); 1+3+5+·~·+(2k—1)=k2 (2)(Note that P(k) is rw: the equation (2k — 1) = kz, which is tnie only for k = 1.) Using the induc-tive hypothesis. we want to show P(k + 1), which is equation (1) when n has the valuek + 1, orP(k-+1): 1+3+5-1-···+[2(k+1)—1]¥(k+1)2 (3)(The question mark over the equals sign is to remind us that this is the fact we want to prove asopposed to something we already know.)The key to an inductive proof is to find a way to relate what we want to show—P(k + 1),equation (3)-to what we have assumed—P(k), equation (2). The left side of P(k + 1) can berewritten to show the next-to-last term:1+3+5+···+(2k-1)+[2(k+1)-1]

Page 122: Mathematical Structures for Computer Science

104 cuavrea 2 mars, Remain", and Analysis an/gmirhmsThis expression contains the left side of equation (2) as a subexpression. Because we haveassumed P(k) to be true, we can substitute the right side of equation (2) for this subexpression.Thus,1+3+5+-—-+[2(k-1-1)-1]=1-+3+5+--·+(2k-1)+[2(k+ 1)—1]=k’+[2(k+1)-1]=k’+[2k+2-1]= k2 + 2k + 1= (k + 1)*Therefore.1+3+5+---+[2(k+1)-1]=(k+1)’which verifies P(k + l) and proves that equation (1) is true for any positive integer rx. •Table 2.3 summarizes the three steps necessary for a proof using the first principle of inductionTABLE 2.3To prove by first principle of inductionStep l Prove base case.Step 2 Assume P(k).Step 3 Prove P(k + 1).EXAMPLE 15 Prove thatl+2+27+···+2"=2"*‘—1for any n 2 1.Again. induction is appropriate. ]’(1) is the equation1+2=21*l-1 or 3=2’—1V. p M `—¤: which is true. We take PU<>. 1+ 2 + 22 + + 2* = z*·¤ -1

uzmwmr as the inductive hypothesis and try to establish P(k + 1):an me».»»1».m 1+2+21+ ·-·+2'<**é2*****—1Again, rewriting the sum on the left side of P(k + 1) reveals how the inductive assumption canbe used:

Page 123: Mathematical Structures for Computer Science

srcnorv 2.2 Induction 1051+2+2¤+·»·+2*+* .=1+2+2¤+~·-+2*+2*+*= 2*** — 1 + 2*** (from the inductive assumption P(k))= zum _ 1Therefore,1+2+2*+-··+2***=2****‘*-1which veriiies P(k + 1) and completes the proof. •Prove that for any positive integer n,1+2+3-i-·»-+n=w •Not all proofs by induction involve formulas with sums. Other algebraic identities about thepositive integers. as wel.l as nonalgebraic assertions like the number of offspring in generationn of the Smith family, can be proved by induction.EXAMPLE 1 6 Prove that for any positive integer n, 2" > n.P(1) is the assertion 2* > 1, which is surely true. Now we assume P(k). 2* > k. and try toconclude P(k + 1), 2*** > k + 1. Beginning with the left side of P(k + 1), we note that2*** = 2* - 2. Using the inductive assumption 2* > k and multiplying both sides of this inequal-ity by 2. we get 2* - 2 > k - 2. We complete the argument2***=2*·2>k·2=k+k2k+l

2*** > k + 1 •EXAMPLE 1 7 Prove that for any positive integer n, the number 2*” — 1 is divisible by 3.The basis step is to show P(1), that 2**** - 1 = 4 - 1 = 3 is divisible by 3. Clearly this is true.We assume that 2** — 1 is divisible by 3. which means that 2** - 1 = 3m for some integer rn.or 2** = 3m + 1. We want to show that 2****** — 1 is divisible by 3.= 2*-2** — 1= 2*(3m + 1) - 1 (by the inductive hypothesis)= 12m + 4 - 1= 12m + 3= 3(4m + 1) where 4m + 1 is an integerThus 2*** * ** — 1 is divisible by 3. •

Page 124: Mathematical Structures for Computer Science

106 CHAPTER 2 Proofs, Recursian, and Analysis of AigmirhmsFor the 5.rst step of the induction process, it may be appropriate to begin at 0 or at 2 or 3instead of at 1. The same principle applies, no matter where you first hop on the ladder.EXAMPLE 18 Prove that ni > Sn for n 2 4.Here we should use induction and begin with a basis step of P(4). (Testing values ofn = 1. 2. and 3 shows that the inequality does not hold for these va1ues.)P(4) is the inequality4* > 3(4), or 16 > 12. which is mie. The inductive hypothesis is that kz > 3k and that Ic 2 4.and we want to show that (k + 1)2 > 3(Ic + 1).(k+1)’=k’+2k+1> 3k + 21: + 1 (by the inductive hypothesis)23k+8+l (sincek24)> 3k + 3= 3(k + 1) •Prove that 2¤** < 2~ for au rt > 1. •Misleading claims of proof by induction are also possible. When we prove tl1e truth ofP(k + 1) without relying on the truth of P(k), we have done a direct proof of P(k + l) wherek + 1 is arbitrary. The proof is not invalid, but it should be rewritten to show that it is a directproof of P(n) for any n, not a proof by induction.An inductive proof may be called for when its application is not as obvious as in the aboveexamples. This usually arises when there is some quantity in the statement to be proved that cantake on arbitrary nonnegative integer values.EXAMPLE 1 9 A programming language might he designed with the following conventionregarding multiplication: A single factor requires no parentheses, but the product "u times b"must be written as (a)b. So the productl a·b·c·d·e·f·gcould be written in tl1is language as((((<<¤>1¤)c)d>e)f>zor as, for example,(<¢>b)(((v)d)<¤)f)gdepending on the order in which tl1e products are formed. The result is tl1e same in either case.We want to show that any product of factors can be written with an even number of paren-theses. The proof is by induction on the number of factors. For a single factor. there are

Page 125: Mathematical Structures for Computer Science

szcnmv 2.2 mdurzitm 1 070 parentheses. an even number. Assume that for any product of lc factors there is an even num- »ber of parentheses. Now consider a product P of k + 1 factors. P can be thought of as rtimes swhere r has k factors and s is a single factor. By the inductive hypothesis, r has an even numberof parentheses. Then we write r times s as (r):. This adds 2 more parentheses to the even num-ber of parentheses in r. giving P an even number of parentheses. •EXAMPLE 20 A tiling problem gives a nice illustration of induction in a geometric setting.An angle imn is an L-shaped piece t1·rat can cover three squares on a checkerboard (Fig-ure 2.3a). The problem is to show that for any positive integer n, a 2" >< 2" checkerboard withone square removed can be ti1ed—comp1ete1y covered—by angle irons.The base case is n = 1, which gives a 2 >< 2 checkerboard. Figure 2.3b shows the solution tothis case if the upper right comer is removed. Removing any of the other three comers worksthe same way. Assume that any 2* X 2* checkerboard with one square removed can be tiledusing angle irons. Now consider a checkerboard with dimensions 2*** X 2***. We need to showthat it can be tiled when one square is removed. To relate the k + 1 case to the inductive hypot.h·esis, divide t.he 2*** >< 2*** checkerboard into four quarters. Each quarter will be a 2* >< 2*checkerboard, and one will have a missing square (Figure 2.3c). By the inductive hypothesis,this checkerboard can be tiled. Remove a corner from each of the other three checkerbcards, asin Figure 2.3d. By the inductive hypothesis. the three boards with the holes removed can betiled. and one angle iron can tile the three holes. Hence the original 2*** >< 2*** board with itsone hole can be tiled.(al (b) <¤> l ld)Frcurte 2.3 •Second Principle of InductionIn addition to the first principle of induction, which we have been using,1. P(l)istn1e __ _—> P(n) true for all positive integers n2. (Vk)[P(k) true —> P(k + 1) n·ue]there is a second principle of induction.

Page 126: Mathematical Structures for Computer Science

108 CHAPTER 2 Proofs, Recursion, and Analysis of AlgorithmsThese two induction principles differ in statements 2 and Z'. In statement 2, we must be able toprove for an arbitrary positive integer k that P(k + 1) is true based only on the assumption thatP(k) is u·ue. In statement 2', we can assume that P(r) is true for all integers rbetween l and anarbitrary positive integer k in order to prove that P(k + 1) is true. This seems to give us a greatdeal more "ammunition," so we might sometimes be able to prove the implication in 2’ whenwe cannot prove the implication in 2.What allows us to deduce (Vn)P(n) in either case? We sha.ll see that the two induction prin-ciples themselves, that is, the two methods of proof, are equivalent. In other words, if we acceptthe first principle of induction as valid, then the second principle of induction is valid, and con-versely. In order to prove the equivalence of the two induction principles, we’ll introduceanother principle. which seems so obvious as to be unarguable.We shall see that the following implications are true:second principle of induction —> first principle of inductionfirst principle of induction -> well-orderingwell-ordering -» second principle of inductionAs a consequence, all three principles are equivalent, and accepting any one of them as truemeans accepting the other two as well.To prove that the second principle of induction implies the first principle of induction, sup-pose we accept the second principle as valid reasoning. We then want to show that the iirst prin-ciple is valid, that is, that we can conclude P(n) for all n from statements 1 and 2. lf statement1 is true. so is statement 1'. If statement 2 is true, then so is statement 2’, because we can saythat we concluded P(k + 1) from P(r) for all r between 1 and k, even though we used only thesingle condition P(k), (More precisely, statement 2' requires that we prove P(l) A P(2) A AP(k) —> P(k + 1), but P(l) A P(2) A A P(k) —> P(k), and from statement 2, P(k) -> P(k + 1),so P(l) A P(2) A A P(k) —» P(k + 1).) By the second principle of induction, we concludeP(n) for all n. The proofs that the first principle of induction implies well-ordering and thatwel.l·ordering implies the second principle of induction are left as exercises in Section 3.1.To distinguish between a proof by the first principle of induction and a proof by the secondprinciple of induction, let’s look at a rather picturesque example that can be proved both ways.

Page 127: Mathematical Structures for Computer Science

srcnozv 2,2 Induction 109EXAMPLE 21 Prove that a straight fence with n fence posts has n - 1 sections for any na 1(see Figure 2.4a).- - -Fence with 4 fenceposts, 3 sections Fence with l fencepost, 0 sections<¤> (b). *1** ...... ....... ll i »..... .....Fence with last post and Fence with one section removedlast section removed(cl (dlFrom zaLet P(n) be the statement that a fence with rr fence posts has n - 1 sections, and prove P(n)true for all vt z 1.We‘11 start with the first principle of induction. For the basis step, P(l) says that a fence withonly l fence post has 0 sections. which is clearly true (see Figure 2,4b). Assume that P(k) istrue:a fence with k fence posts has k - 1 sectionsand Lry to prove P(k +1):(7) a fence with k + l fence posts has k sectionsGiven a fence with k + 1 fence posts. how can we relate that to a fence with k fence posts sothat we can make use of the inductive hypothesis? We can chop off the last post and the lastsection (Figure 2.4c). The remaining fence has k fence posts and. by the inductive hypothesis.E — l sections. Therefore the original fence had k sections.Now we’1l prove the same resu.lt using the second principle of induction. The basis step isthe same as before. For the inductive hypothesis, we assumefor all r, l S r s k, a fence with r fence posts has r — l sectionsand try to prove P(k + 1):('J) a fence with k + l fence posts has k sections

Page 128: Mathematical Structures for Computer Science

110 CHAPTER 2 Proofs, Recursiun, and Analysis of AlgorithmsFor a fence with k + 1 fence posts, split the fence into two parts by removing one section (Fig-ure 2.4d). The two parts of the fence have r, and rz fence posts, where 1 S rl S k, 1 S rz S k,and r, + rz = k + 1. By t.he inductive hypothesis, the two parts have. respectively, r, — 1 andrz - 1 sections, so t.he original fence has(r, - 1) + (ri — 1) + 1 sections('l'he extra 1 is for the one that we removed.) Simple arithmetic then yieldsr,+r2—1=(k+1)-1=ksectionsThis proves that a fence with k + 1 fence posts has k sections, which verifies P(k + 1) andcompletes the proof using the second principle of induction. •Example 21 allowed for either form of inductive proof because we could either reduce thefence at one end or split it at an arbitrary point. The problem of Example 19 is similar.EXAMPLE 22 We again want to show that any product of factors can be written in this pro-gramming language with an even number of parentheses, this time using the second principleof induction. The base case is the same as in Example 19: A single factor has 0 parentheses, aneven number. Assume that any product of r factors, l S r S k, can be written with an even num-ber of parentheses. Then consider a product P witl1 k + 1 factors. P can be written as (S)T, aproduct of two factors S and T, where S has rl factors and Thas ri factors. Then 1 S rl S kand 1 S rz S k, with rl + rz = k + 1. By the inductive hypothesis, S and T each have an evennumber of parentheses, and therefore so does (S)T = P. •Most problems do not work equally well with either fomr of induction; the fence post andthe programming language problem were somewhat artificial. Generally, the second principleof induction is called for when the problem "spl.its" most naturally in the middle instead ofgrowing from the end.EXAMPLE 23 Prove that for every integer n 2 2. n is a prime number or a product of primenumbers.We will postpone the decision of whether to use the first or the second principle of induc-tion; the basis step is the same in each case and need not start with 1. Obviously here we shou.1dstan with 2. P(2) is the statement that 2 is a prime number or a product of primes. Because 2 isa prime number, P(2) is true. Jumping ahead, for either principle we will be considering thenumber k + 1. lf k + 1 is prime, we are done. lf k + 1 is not prime, then it is a composite num-ber and can be written as k + l = ab. Here k + 1 has been split into two factors. Maybe neitherof these factors has the value k, so an assumption only about P(k) isn`t enough. Hence, we'lluse the second principle of induction.So let’s start again. We assume that for all r, 2 S r S k, P(r) is true—r is prime or the prod-uct of primes, Now consider the number k + 1. If k + 1 is prime, we are done. lf k + l is notprime, then it is a composite number and can be written as k + 1 = ab, where 1 < rr <k + land 1 < b < k + 1. (This is a nontrivial factorization, so neither factor can be 1 or k + 1.)

Page 129: Mathematical Structures for Computer Science

sscnorv 2.2 induction 1 1 1Therefore 2 S a s k and 2 s b s k. The inductive hypothesis applies to both a and b. so a and .b are either prime or the product of primes. Thus, k + 1 is the product of prime numbers. Thisverifies P(k + l) and completes the proof by the second principle of induction. •The proof in Example 23 is an existence proof rather than a czmsrructiva proof, Knowingthat every nonprime number has a factorization as a product of primes does not make it easy tofind such a factorization. (We will see in Section 3.7 that there is, except for the order of the fac-tors. only one such factorization.) Some encryption systems for passing information in a securefashion on the Web depend on the difficulty of factoring large numbers into their prime factors(see the discussion on public-key encryption in Section 4.5).EXAMPLE 24 Prove that any amount of postage greater than or equal to 8 cents can be builtusing only 3~cent and 5-cent stamps.Here we let P(n) be the statement that only 3-cent and 5-cent stamps are needed to buildn cents worth of postage, and prove that P(n) is true for all n 2 8. The basis step is to establishP(8), which is done by the equation8 = 3 + 5For reasons that will be clear momentarily, we'l1 also establish two additional cases, P(9) andP(10), by the equations9=3+3+310 = 5 + 5Now we assume that P(r) is true for any r, 8 s r s k. and consider P(k + l). We may assumethat k + 1 is at least 11, since we have already proved P(r) true for r = 8, 9, and 10. Ifk + 12 11. then (k + 1)- 3 = k — 2 2 8, and by the inductive hypothesis, P(k — 2) is true.Therefore k - 2 can be written as a sum of 3s and 5s, and adding an additional 3 gives us k + 1as a sum of 3s and 5s. This verifies that P(k + 1) is true and completes the proof. •a. Why are the additional cases P(9) and P(10) proved separately in Example 247`E b. Why can't the first principle of induction be used in the proof of Example 247 •As a general rule, the first principle of mathematical induction applies when informationabout "one position back" is enough, that is. when the truth of P(k) is enough to prove the ¤·uthZ'; of P(k + l). The second principle applies when information about "one position back" isn‘tmy good enough; tl1at is. you can't prove that P(k + 1) is true just because you know P(k) is true.but you can prove P(k + 1) true if you know that P(r) is true for one or more values of r that arenw · "farther back" than k.

Page 130: Mathematical Structures for Computer Science

112 cmxrrss 2 rmrs, Recursion, and Analysis ufA@orithmsSECTION 2.2 ReviewTECHNIQUESW • Use the First principle of i.nduction in proofs.W • Use the second principle of induction in proofs.MAIN IDEAS• Mathematical induction is a technique to prove properties of positive integers.• An inductsive proof need not begin with 1.• Induction can be used to prove statements about quantities whose values arearbitrary nonnegative integers.• The ti.rst and second principles of induction each prove thc same conclusion, butone approach may be easier to use than the other in a given situation.EXERCISES 2.2In Exercises 1-22, use mathematical induction to prove that the statements are true for everypositive integer vi.*1. 2+6+10+··-+(4n-2)=2n’2. 2+4+6-1-···+2n=n(n+1)*3. 1+5+9+···+(4n-3)=n(2n-1)4. I+3+6+___+n(n;1)=n(n+1£(n+2)*5. 4+10+16+-·-+(6n-2)=n(3n+1)6. 5+10+15-1-·--+5n=@7. l1+22+_4_+”2=n(n+1§2n+t)

s.1=+z¤+-·-+n¤=—"(”;'1),,9_11+3¤+_,,+(2,,_1)¤= ,2 -` 10. 14 + 24 + + H4 = n(n+ 1)(2r1+;2)(3n +3n 1)11. l·3+2·4+3·5+···+n(n+2)=a" - 112,1+a+a’+···+a""=§fora#0,u#I1 1 1 1 n*13*1·2+z-2+s·4+"'+n(¤+1)"n+1l 1 1 1 nM 1~2 +z·5 +E+m+(2n-1)(2n+1)_2n+1

Page 131: Mathematical Structures for Computer Science

secnotv 2.2 Induction 113- +¤ .,,15 12 _ 22 + 32 _ 42 + + (-0.+.,,216. 2+6+ 1B+··~+2·3”"=3"-117. 22 +42 + + (Zn); = 2n(n +1;(2n +1)18.1-2*+2-22+3·2’+···+n-2”=(n—l)2"*‘+219.1·2+2·3+3·4+···+n(n+l)=1 + 220.1·2·3+2·3·4+·-·+n(n+1)(n+2)=1 1 1 1 nZL 1-4+4·7+7·10+m+(3n—2)(3n+1)*3n+122.1-11+ 2 · 21 + 3 · 3! + + n·n! = (n +1)1-lwherenlistheproductofthepositive integers from 1 to n.vr 23. A geometric progression (geometric sequence) is a sequence of terms where there is aninitial terrn tz and each succeeding term is obtained by multiplying the previous term bya common ratio r. Prove the formula for the sum of the Hrst n terrns of a geometricsequence (n 2 1): ’a — nr"a+ar+ar2+ +ar"`1=1 — r24. An arithmetic progression (arithmetic sequence) is a sequence of terms where there isan initial term u and each succeeding term is obtained by adding {common dgference dto the previous term. Prove the formula for the sum of the frst n terms of an arithmeticsequence (vt 2 1):a+(a+d)+(n+2d)+···+[a+(n- 1)d]=;[2a+(n· 1)d]25. Using Exercises 23 and 24, Find an expression for_the value of the following sums.a. 2+2·5+2-5’+···+2·5°11. 4-7+4·72+4·7’+···+4·7‘Zc. 1+7+13+··-+49d. 12+17+22+27+-»+92I6. Prove that<—2>° + <—2>‘ + <—2>’ + + <—2>” = ?for every positive odd integer n.i'. Prove that nz 2 2n + 3 forn 2 3.* ZS. Prove that nz > n + 1 for n 2 2.Z9. Prove that nz > 5n + 10 for n > 6.Sit. Prove that 2” > ni for n 2 5.

Page 132: Mathematical Structures for Computer Science

114 CHAPTER 2 Proofs, Rccursinn, and Analysis of AlgorithmsIn Exercises 31-35. nl is the product of the positive integers from 1 to rr.31. Prove that nl > nl for n 2 4.32. Prove that nl > 3** for rt 2 7.* 33. Prove that 2** < nl for rr 2 4.34. Prove that 2**** S nl forn 21.35. Prove thatn! < n'* forn 2 2.36. Provethat(1+x)” >1+x'*forn >1,x>0.37. Prove that < forn 2 land0 < u < b.it 38. Provethatl + Z + + vt <nZforn> 1.39. Provemat1 +*4-L+ +%<2—lt*¤rn22.4 9 J n n40. a. Try to use induction to prove that1+%+%+--·+%<2f¤rn21What goes wrong?b. Prove that1+;-+%-+·-·+%=2—%f0rrt21thus showing that1+%+%+---+%<2forn211 1 1 n .41. Prove tl1at1+ E + 5 + + ? 21+ E forn 2 l. (Note that the denominatorsincrease by 1, nut by powers of 2.) _For Exercises 42-53, prove that the statements are true for every positive integer.* 42. 2**** - 1 is divisible by 7.43. 32** + 7 is divisible by 8.44. 7** - 2** is divisible by 5.45. 13** - 6** is divisible by 7.* 46. 2** + (-1)**** is divisible by 3.47. 25**** +5**** is divisible by 27.48. 3‘***2 + 5**** is divisible by 14.49. 77** + 16n - l is divisible by 64.ic 50. 10** + 3 · 4**** + 5 is divisible by 9.51. ni · n is divisible by 3.52. rf + 2n is divisible by 3.53. x** - lis divisible byx -1forx #1.

Page 133: Mathematical Structures for Computer Science

sscnorv 2.2 Induction 115ik 54. Prove DeMuivre'.t theorem: ·(cos 9+ isin 0)** = cos n8 + isin n8` for all n 2 1. Him: Recall the addition formulas from trigonometry:cos(a + B) = cos rr cos B — sin rx sin Bsin(¤t + B) = sin or cos B + cos at sin B55. Prove that_ 1san 0 + sin 39 + + stntzn —1)a =sin 9for all n 2 1 and all 6 for which sin 9 ve 0.it 56. Use induction to prove that the product of any three consecutive positive integers is divis-ible by 3.57. Suppose that exponentiation is defined by the equationxl · nr = xmfor anyj 2 l. Use induction to prove that x" · x"‘ = x"*'”, forn 2 1, m 2 1.(Hint: Do induction on m for a fixed, arbitrary value of n.)58. According to Example 20, it is possible to use angle irons to tile a 4 >< 4 checkerboardwith the upper iight corner removed. Sketch such a tiling.59. Example 20 does not cover the case of checkerboards that are not sized by powers of 2.Determine whether it is possible to tile a 3 >< 3 checkerboard.60. a. Prove that it is possible to use angle irons to tile a 5 >< 5 checkerboard with the upperleft comer removed.b. Find a configuration for a 5 X 5 clieckerboard with one square removed that is notpossible to tile; explain why this is not possible.61. Consider n infinitely long straight lines, none of which are parallel and no three of whichhave a common point of intersection. Show that for n 2 1, the lines divide the plane into(n’ + n + 2)/2 separate regions.62. A string of Us and ls is to be processed and converted to an even-parity string by addinga parity bit to the end of the string. (For an explanation of the use of parity bits, seeExample 17 in Chapter B.) The parity bit is initially 0. When a 0 character is processed,the parity bit remains unchanged. When a 1 character is processed. the parity bit isswitched from 0 to 1 or from 1 to 0. Prove that the number of ls in the final suing, that is,including the parity bit, is always even. (Hint.- Consider various cases.)* 63. What is wrong with the following "proof" by mathematical induction? We will prove thatfor any positive integer n, n is equal to l more than n. Assume that P(k) is true.k = k + 1Adding 1 to both sides of this equation, we getIc + 1 = k + 2Thus,P(k + 1) is true

Page 134: Mathematical Structures for Computer Science

116 CHAPTER 2 Proofs, Recursinn, and Analysis ofAlgorithms64. What is wrong with the following "proof " by mathematical induction? We will provethat all computers are built by the same manufacturer. In particular, we will prove that inany collection of n computers where n is a positive integer, all of the computers are builtby the same manufacturer. We first prove P(l), a trivial process, because in any collectionconsisting of one computer, there is only one manufacturer. Now we assume P(k); that is,in any collection of k computers, all the computers were built by the same manufacturer.To prove P(k + l), we consider any collection of k + l computers. Pull one of thesek + l computers (call it HAL) out of the collection. By our assumptionithe remainingk computers all have tl1e same manufacturer. Let HAL change places with one ofthese k computers. ln the new group of k computers, all have the same manufacturer.Thus, I-lAL’s manufacturer is the same one that produced all the other computers, andall k + l computers have the same manufacturer.65. An obscure tribe has only three words in its language, muon, noon, and soon. New wordsare composed by juxtaposing these words in any order, as in suvrmoonmoommon. Anysuch juxtaposition is a legal word.a. Use the first principle of induction (on the number of subwords in the word) to provethat any word in this language has an even number of o’s.b. Use the second principle of induction (on the number of subwords in the word) toprove that any word in this language has an even number of o‘s.66. A simple closed polygon consists of n points in the plane joined in pairs by n line segments;each point is the endpoint of exactly two line segments. Following are two examples.a. Use the first principle of induction to prove that the sum of the interior angles of ann-sided simple closed polygon is (rt - 2)l80° for all n 2 3.b. Use the second principle of induction to prove that the sum of the interior angles ofan n-sided simple closed polygon is (n - 2)lS0° for all ri 2 3.67. The Computer Science Club is sponsoring a jigsaw puzzle contest. Jigsaw puzzles areassembled by fitting two pieces together to form a small block, adding a single piece to ablock to form a bigger block. or fitting two blocks together. Each of these moves is con-sidered a step i.n the solution. Use the second principle of induction to prove that the num-ber of steps required to assemble an mpiece jigsaw puzzle is ri - l.* 68. Consider propositional wffs that contain only the connectives A, V, and -·> (no negation) andwhere wffs must be parenthesized when joined by a logical connective. Count each state-ment letter, connective. or parenthesis as one symbol. For example, ((A) A (B)) V ((C) A(D)) is such a wff with 19 symbols. Prove that a.ny such wff has an odd number of symbols.

Page 135: Mathematical Structures for Computer Science

sscnorv 2.2 Induction 117* 69. Prove that any amount of postage greater than or equal to 2 cents can be built using only2-cent and 3-cent stamps.70. Prove that any amount of postage greater than or equal to l2 cents can be built using only4-cent and 5-cent stamps.71. Prove that any amount of postage greater than or equal to l4 cents can be built using only3-cent and 8-cent stamps.* 72. Prove that any amount of postage greater than or equal to 64 cents can be built using only5-cent and 17-cent stamps.73. Your bank ATM delivers cash using only $20 and $50 bills. Prove that you can collect, inaddition to $20, any multiple of $10 that is $40 or greater.74. In any group of k people, k 2 l, each person is to shake hands with every other person.Find a formula for the ntunber of handshakes, and prove the formula using induction.Exercises 75-76 require familiarity with ideas from calculus. Exercises l-22 give exact fonnulasnfor the surn of terms in a sequence that can be expressed as 2 f(m). Sometimes it is difficult tofind an exact expression for this summation, but if the value of f(m) increases monotoni-cally, integration can be used to ind upper and lower bounds on the value of the summation.Specifically,f f<x>d» S E fom sf fmdxu vt - 1 inUsing the following figure, we can see (on the left) that I f(x)dx underestimates the value ofnthe summation while (on the right) I f(x)dx overestimates it.1fix) Jia)P" Al _’ ( fil).? KZ} A3) JW0 t 2 3 4 1 2 3 4 5`at vs. shaw that-J. Zz dx S E 2m sf amt (see Exercise 2),a mei i76. Show that"} 1* dx S 2 m’ S I zz dx (see Exercise 7).

Page 136: Mathematical Structures for Computer Science

11s CHAPTER 2 Proofs, Recursion, and Analysis or AlgorithmsSECTION 2.3 More on Proof of Correctness

In Section 1.6, we explained the use of a fonnal logic system to prove mathematically the cor-recmess of a program. Assertions or predicates involving the program variables are inserted atthe beginning, at the end, and at intermediate points between the program statements. Thenproving the correctness of any particular program statement si involves proving that the impli-cation represented by the Hoare triple(Qi S, (R) (1)is true. Here Q and R are assertions known, respectively, as the precondition and postconditionfor the statement. The program is provably correct if all such implications for the statements inthe program are true.In Chapter 1, we discussed rules of inference that give conditions under which implication(l) is true when sl is an assignment statement and when si is a conditional statement. Now wewill use a rule of inference that gives conditions under which implication (1) is true when si isa loop statement. We have deferred consideration of loop statements until now because mathe-matical induction is used in applying this rule of inference,Loop RuleSuppose that sl is a loop statement in the formwhile condition B doPend whilewhere B is a condition that is either true or false and P is a program segment. When this state-ment is executed. condition B is evaluated. If B is true, program segment P is executed and thenB is evaluated again. If B is still true, program segment P is executed again, then B is evaluatedagain, and so forth. lf condition B ever evaluates to false, the loop terminates.The form of implication (l) that can be used when si is a loop statement imposes (like theassignment rule did) a relationship between the precondition and posteondition. The precondi-tion Q holds before the loop is entered; strangely enough. one requirement is that Q must con-tinue to hold after the loop terminates (which means that we should look for a Q that we wantto be t:rue when the loop terminates). ln addition, B'—the condition for loop termination—mustbe true then as well. Thus (l) will have the form{Q)-MQ/~B') (2)EXAMPLE 25 Consider the following pseudocode function, which is supposed to returnthe value x * y for nonnegative integers x and y. ·Product(nonnegative integer x; nonnegative integer y)Local variables:integers i, ji = 0

Page 137: Mathematical Structures for Computer Science

sl=cr10~ 2.J More on Proof of Oorreclness 119while i ee x do ’I =j + yi = i + lend while//j now has the value x ·t yretum jend function ProductThis function contains a loop; the condition B for continued loop execution is i ee x. The conditionB' for loop termination is i = x. When the loop terminates, it is claimed in the comment that j hasthe value x * y. Given that i = x when the loop terminates, the assertion that j = i * y would alsohave zo be true. Thus, on loop termination. if the function does what is claimed, the assertionj = i * y A i = xis true. This matches the form Q A B' if we take the assertionj = i * yas Q. (Notice that Q is a predicate; that is, it states a relationship between variables in theprogram. It is never part of an equation such as Q = j.) To match the fonn of (2). the assertionj = i * y would have to be true before the loop statement. This is indeed the case because rightbefore the loop statement, i = j = 0.It would seem that for this example we have a candidate assertion Q for implication (2), butwe do not yet have the rule of inference that allows us to say when (2) is a true implication.(Remember that we discovered our Q by "wishful thinking" about the correct operation of thefunction code.) •Assertion Q must be true before the loop is entered. lf implication (2) is to hold, Q mustremain tnie after the loop terminates. Because it may not be known exactly when the loop willterminate, Q must remain true after each iteration through the loop, which will include the finaliteration. Q represents a predicate, or relation, among the values of the program variables. Ifthis relation holds among the values of the program variables before a loop iteration executesand holds among the values after the iteration executes, then the relation among these variablesis unaffected by the action of the loop iteration, even though the values themselves may bechanged. Such a relation is called a loop invariant.The loop rule of inference allows the truth of (2) to be inferred from an implication statingthat Q is a loop invariant, that is, that if Q is true and condition B is true, so that another loop iter-ation is executed, then Q remains true after that iteration. The rule is formally stated in Table 2.4.TABLE 2.4From Can Derive Name of Rule Restrictions on Use{QAB)P(Q) (Q}sl(QAB') loop rihasthefomiwhile condition B doPend while

Page 138: Mathematical Structures for Computer Science

120 crmrrea 2 Proofs, recursion, and Analysis uf AlgorithmsTo use this rule of inference. we must find a usefid loop invariant Q—one that asserts whatwe want and expect to have happen—and then prove the implication{Q A Bi P (QiHere is where induction comes into play. We denote by Q(n) the statement that a proposedloop invariant Q is true after n iterations of the loop. Because we do not necessarily know howmany iterations the loop may execute (that is, how long condition B remains true), we want toshow that Q(n) is true for all n 2 0. (The value of n = 0 corresponds to the assertion uponentering the loop. after zero loop iterations.)EXAMPLE 26 Consider again the pseudocode function of Example 25. In that example, weguessed that Q is the relationj = i * yTo use the loop rule of inference. we must prove that Q is a loop invariant.The quantities x and y remain unchanged throughout the function. but values ofi and j change within the loop. We let in and jk denote the values of i and j. respectively. after niterations of the loop. Then Q(n) is the statement jk = ik * y.We prove by induction that Q(n) holds for all n 2 0. Q(0) is the statementja = ic * Ywhich, as we noted in Example 25. is true. because after zero iterations of the loop, when wefirst get to the loop statement. both i and j have been assigned the value 0. (Formally, the assign-ment rule could be used to prove that these conditions on i and j hold at this point.)Assume Q(k):jk = ik * yShow Q(k + l);jk+k = ikkk *yBetween the time j and i have the values j k and ik and the time they have the values jk kk and i k H,one iteration of the loop takes place. In that iteration, j is changed by adding y to the previousvalue, and i is changed by adding l. Thus.fa. = it + v G)ik +1 = ik + 1 (4)Thenjk., = ik + N (by (3))= ik * y + y (by the inductive hypothesis)= (ik + l)y= im * y (by (4))We have proved that Q is a loop invariant.The loop rule of inference allows us to infer that after the loop statement is exited, the con-dition Q A B' holds, which in this case becomesj = i * y A i = x

Page 139: Mathematical Structures for Computer Science

SECTION 2.3 More on Pmui of cmreerness 121Therefore at this point the statement .I = >¤ * yis true. which is exactly what the function is intended to compute. •Example 26 illustrates that loop invariants say something stronger about the program tha.nwe actually want to show; what we want to show is the special case of the loop invariant on ter-mination of the loop. Finding the appropriate loop invariant requires working backward fromtl1e desired conclusion, as in Example 25.We did not, in fact, prove that the loop in this example actually does terminate. What weproved was partial correctness—the program produces the correct answer, given that executiondoes terminate, Since x is a nonnegative integer and i is an integer that starts at O and is thenincremented by l at each pass through the loop. we know that eventually i = x will become true.Show that the following function retums the value x + y for nonnegativeintegers x and y by proving the loop invariant Q: j = x + i and evaluating Q when the loopterminates.Sum(nonnegative integer x; nonnegative integer y)Local variables:integers i, ji = 0I = Xwhile i we y doJ =i + 1i = i + 1end while/0 now has the value x + yreturn jend function Sum •The two functions of Example 25 and Practice l0 are somewhat unrealistic; after all, if wewanted to compute x * y or x + y, we could no doubt do it with a single program statement.However, the same techniques apply to more meaningful computations, such as the Euclideanalgorithm.Euclidean AlgorithmThe Euclidean algorithm was devised by the Greek mathematician Euclid over2300 years ago, making it one of the oldest known algorithms. This algorithm finds the greatestcommon divisor of two positive integers a and lz. The greatest common divisor of a and b,denoted by gcd(u, lz), is the largest integer n such that nin and nib. For example, gcd(l2, 18) is 6and gcd(420. 66) = 6.The Euclidean algorithm works by a succession of divisions. To find gcd(a, b), assumingthat a 2 b, you first divide a by b, getting a quotient and a remainder. More formally, at thispoint zz = qib + rl, where 0 S rl < b. Next you divide the divisor. Iz, by the remainder, ri. get-ting I: = qzrl + rz, where 0 S rz < rl. Again divide the divisor, rl, by the remainder, rz, getting

Page 140: Mathematical Structures for Computer Science

122 tuawrn 2 moon, aa. arm", .mrm,.i1tsn ..r.i1gw:llmi,rl : qgrz + rz, where 0 s rg < rz. Clearly, there is a looping process going cn. The processterminates when the remainder is 0, at which point the greatest common divisor is the last divi-sor used.EXAMPLE 2 7 To find gcd(420, 66) the following divisions are pertonnedz6 2 l 3GGHE6 24% mW 6EE E E E24 18 6 0The answer is 6, the divisor used when the remainder became 04 •A pseudocode version of the algorithm follows, given in the form of a function to returng¤d<¤. b). ` *ALGORITHM Euclidean AlgorithmGCD(positive integer a; positive integer b)lla 2 bLocal variables:integers i.ji = a1 = bwhile j ¢ 0 docomputei=qj+r,0sr<ji = jJ = rend while//i now has the wtlue gcd(a. b)return i;end function GCDWe intend to prove the correctness of this function. but we will need one additional fact first,namely(V integers a, b. q. r)[(u = qb + r) —> (gcdta, b) = gcd (b, r))] (5)To prove (5), assume that a = qb + r, and suppose that c divides both rz and b, so that a = qlcand b = qzc. Thenr = ¤ 7 qb = q,¢ 7 qq; ; ¤(q. — qq,)

Page 141: Mathematical Structures for Computer Science

sscnow 2.3 More on mmf of correctness 123so that c divides r as well. Therefore anything that divides a and b also divides b and r. Nowsuppose d divides both b and r so that b = qld and r = qéd, Then¤ = qb + V = 41141 + q,d = dtqq, + q,)so that d divides a as well. Therefore anything that divides b and r also divides a and b. Because(u, b) and (b, r) have identical divisors, they must have the same greatest common divisor.EXAMPLE 28 Prove the correctness of the Euclidean algorithm,Using function GCD. we will prove the loop invariant Q: gcd(i, j) = gcd(u, b) and evaluateQ when the loop terminates. We use induction to prove Q(n): gcd(in, ju) = gcd(a, b) for alln 2 O. Q(0) is the statementgcd(i°, jo) = gcd(a, b)which is true because when we first get to the loop statement, i and j have the values a and Iz,respecdvely.Assume Q(k): gcd(ik, jk) = gcd(a, b)Show Q(k + l): gcd(im,jH1) = gcd(u, b)By the assignment statements within the loop body, we know thatQt. = itim = nThens¤d<ia.·fr+.> = g¤d<it» *0= s¤<1<ik. J,.) by (5)= gcd(u. b) by the inductive hypothesisQ is therefore a loop invariant. At loop termination, gcd(i. j) = gcd(a, b) and j = 0, so gcd(i. 0) =gcd(u, b), But gcd(i, 0) is i. so i = gcd(a, b). Therefore function GCD is correct. •SECTION 2.3 Review

TECHNIQUESW • Verify the correctness of a program segment that includes a loop statement.• Compute gcd(a, b) using Euclid’s algorithm.MAIN IDEAS• A loop invariant. proved by induction on the number of loop iterations, can beused to prove correctness of a program loop.• The classic Euclidean algorithm for finding the greatest common divisor of twopositive integers is provably correct.

Page 142: Mathematical Structures for Computer Science

124 c|·me*rea 2 Proofs, Recursiun, and Analysis ufAQ;ori!hmsEXERCISES 2.3In Exercises 141, prove that the pseudocode program segment is correct by proving the loopinvariant Q and evaluating Q at loop termination.l. Function to retum the value of xi forx 2 1Square(positive integer x)Local variables:integers i. ji = 11 = 1while i ae x doj= j + 2i + 1i = i + 1end while//j now has the value xlretum jend function SquareQ=j = i2t 2. Function to return the value ofx! for x 2 1Facrorial(posit.ive integer 1)Local variables:integers i, ji = 2i = 1whileiséx-1-lduj = j * 5i = i + 1end while//j now has the value xlreturn jend function FactorialQ=j = (i — U!3. Function to return the value of xy for x, y 2 1Power(positive integer x; positive integer y)Local variables:integers i, ji = 1J = xwhile i se y doJ =i * ¤¤i = i + 1end while

Page 143: Mathematical Structures for Computer Science

sscncrv 2.a More tm Proof of correctness 125IU now has the value xy .retum jend function PowerQ=i = ¤<' V4. Function to compute and write out quotient q and remainder r when x is divided by y,X 2 0, y 2 1Divide(nonnegative integer x; positive integer y)Local variables:nonnegative integers q, r#1 = 0while r 2 y doq=q+1end while//q and r are now the quotient and remainderwrite("'I`he quotient is" q "and the remainder is" r)end function DivideQ: x = q * y + rFor Exercises 5-B, use the Euclidean algorithm to find the greatest common divisor of the givennumbers.5. (2.420, 70)it 6. (735, 90)7. (1326. 252)8. (1018215, 2695)In Exercises 9-14, prove that the program segment is correct by finding and proving the appro—priate loop invariant Q and evaluating Q at loop termination.9. Function to retum the value x — y for x, y 2 0Differencemonnegative integer x; nonnegative integer y)Local variables:integers i, ji = Oj = xwhile i ae y doJ =j · 1i=i+lend whileIU now has the value x - yretum jend function Difference

Page 144: Mathematical Structures for Computer Science

1 26 CHAPTER 2 Pmais, Recursion, and Analysis nhlgorithms* 10. Function to retum the value x #= y'* for n 2 0C0mputation(integer x; integer y: nonnegative integer ri)Local variables:integers i.jJ = OJ = Xwhlle i ¢ n duJ =J * yi = i + 1end while/0 now has the value x * y”retum jend function Computation11, Function to retum the value (x + l)2 forx 2 1IncrementSquare(posidve integer x)Local variables:integers i, ji = 1J = 4while i se x doj = j + 2i+ 3i = i + 1end whlle/0 now has the value (x + 1)*retum jend function lncrementSquare12. Function to retum the value 2" for n 2 1TwosPower(p¤sitive integer n)Local variables:integers i.ji = 1J = 2while i ¢ n doj = j ss Zi = i + 1end while/0 now has the value 2”retum jend function T\v¤sPcwervc l3. Function to retum the value x * nl for n 2 1_ AnotherOne(integer x; positive integer n)

Page 145: Mathematical Structures for Computer Science

sscnorv 2.1 Mme an Pmnf nr Cnrrectness 127Local variables: —integers i, ji = 1j = Xwhile i ee n doJ = i * <i + 1)i = i + 1end while/0 now has the value x * rr!return jend function AnotherOne14. Function to return the value of the polynomialanx" + am rm + + aix + anat a given value of xPol1y(real an; ; real au; real x)Local variables:integers i.ji = nf = awhile i ve 0 doj = j * x + ami = i - 1end while/0 now has value of the polynomial evaluationretum jend function Polly15. Function to retum the maximum value from the first n entries a[l], u[2]. ··., a[n] in anarray of integersArrayMax(integers n, a[l], a[2]. ··i, a[n])Local variables:integers i.ji = 1j = all]while i ¢ n d0i = i + 1if a[i] >j thenj = u[i]end while/0 now has the value of the largest array elementretum jend function ArrayMax

Page 146: Mathematical Structures for Computer Science

12s cnnvrek 2 Proofs, Recursinn, and Analysis nmrgmirhms16. Following are four functions i.ntended to retum the value a[1] + a[2] + + a[n] forn 2 l (the sum of the first n entries in an array of integers). For those that do not producecorrect results. explain what goes wrong. For those that do produce correct results, do aproof of correctness.a. A:·raySumA(integers n. u[1], u[2], , a[n])Local variables:integers J, jJ = 0J = 0while i S n doi=i+lJ =j + ¢[i]end while/0 now has the value u[1] + a[2] + + u[n]retum jend function ArraySumAlu. ArraySumB(integers n. a[1]. a[2], , a[n])Local variables:integers J, jJ = 1J = 0while i S n doJ =J + alilJ = J + lend while/0 now has the value a[1] + u[2] + + a[n]retum jend function ArraySumBc. ArraySumC(integers n. a[l]. a[2]. . a[n])Local variables:integers J, jJ = OJ = 0while i S n doJ =J + ¤[ilJ = J + lend while/0 now has the value a[1] + a[2] + + a[rJ]return jend function ArraySumCd. A1-raySumD(int.egers n. a[1]. a[2l. .a[n])Local variables:` integers J, j

Page 147: Mathematical Structures for Computer Science

SECTION 2.4 Recursive Definitions 129i = 1 »i = ull]while i S n doj = j + ali + 1]i = i + lend while/0 now has the value a[1] + a[2] + + a[n]return jend funct.ion ArraySumDSECTION 2.4 Recursive Definitions

A definition in which the item being defined appears as part of the definition is called an induc-tive definition or a recursive definition. At first this seems like nonsense—how can we definesomething in terms of itself? This works because there are two parts to a recursive definition:1. A basis, where some simple cases of the item being denned are explicitly given2. An inductive or recursive step, where new cases of the item being defined are given interms of previous casesPan l gives us a place to start by providing some simple, concrete cases; part 2 allows us toconstruct new cases from these simple ones and then to construct still other cases from thesenew ones, and so forth. (The analogy with proofs by mathematical induction accounts for thename "inductive definitjon." In a proof by induction, there is a basis step, namely, to show thatP(l)—or P at some other initial value—holds, and there is an inductive step where the truth ofP(k + 1) is deduced from the truth of P at previous values.)Recursion is an important idea that can be used to define sequences of objects, more generalcollections of objects, and operations on objects. (The Prolog predicate infuod-chain of Sec-tion 1.5 was defined recursively.) Even algorithms can be recursive.Recursively Defined SequencesA sequence S is a list of objects that are enumerated in some order; there is a first such object,then a second, and so on. $(1:) denotes the kth object in the sequence. A sequence is definedrecursively by explicitly naming the first value (or the first few values) in the sequence and thendefining later values in the sequence in terms of earlier values.EXAMPLE 29 The sequence S is defined recursively byl. $(1) = 22. $(n) = 2$(n - 1) forn 2 2By statement 1, $(1), the first object in S, is 2. Then by statement 2, the second object in S is$(2) = 2$(l) = 2(2) = 4. By statement 2 again, $(3) = 2$(2) = 2(4) = 8. Continuing in thisfashion, we can see that S is the sequence2,4, 8, 16, 32. •

Page 148: Mathematical Structures for Computer Science

1so CHAPTER 2 Pmuis, Recursiun, and Analysis or AlgorithmsA rule like that of statement 2 in Example 29, which d.e5nes a sequence value in terms ofone or more earlier values, is called a recurrence relation..'.» The sequence Tis aennea recursively as rettcws:l. T(l) = 12. Tln)=T(n-1)-t-SfornzzWrite the first tive values in the sequence T. •EXAMPLE 30 The famous Fibonacci sequence of numbers, introduced in the thirteenthcentury by an Italian merchant and mathematician, is defined recursively byF(1) = 1F(2) = 1F(n) = F(n —— 2) + F(n —1)forn> 2Here the first two values of the sequence are given, and the recurrence relation defmes the nthvalue in terms of the two preceding values. lt's best to think of the recurrence relation in itsmost general fonn, which says that F at any value—except 1 and 2·—is the su.m of F at the twoprevious values. •Write the Grst eight values of the Fibonacci sequence. •EXAMPLE 31 Prove that in the Fibonacci sequenceFtn + 4) = 3F(n + 2) — F(n)foralln z 1Because we want to prove something true for all n 2 l, it is natural to think ofa proof by induction. And because the value of F(n) depends on both F(n — 1) and F (ri — 2),the second principle of induction should be used. For the basis step ofthe inductive proof, we'llprove two cases, n = land n = 2. For n = l, we getF(5) = 3F(3) - F(1)or (using values computed in Practice 12)5 = 3(2) — 1which ia cruel For n = 2,F(6) = 3F(4) - F(2)or8 = 3(B) - 1which is also true. Assume that for all r, 1 S r s k,, F(r + 4) = 3F(r + 2)- F(r)

Page 149: Mathematical Structures for Computer Science

SECTION 214 Recursive Definitions 131Now show the case for k + 1, where lr + 1 2 3. (We need k + 1 2 3 so that the value at ‘2 positions back, k - 1, is greater t.han or equal to 1. That’s why we had to prove the case for land the case for 2 separately.) Thus we want to showF(k+ 1 +4)é3F(k+ 1 +2)7F(k+ 1)

F(k + 5) é 3F(k + 3) — F(k +1)From the recurrence relation for the Fibonacci sequence, we haveF(k + 5) = F(k + 3) + F(k + 4) (F at any value is the sum ofFat thetwo previous values.)and by the inductive hypothesis. with r = k - 1 and r = lr, respectively,F(k+ 3) = 3F(k+ 1) — F(k— 1)andF(k + 4) = 3F(k + 2) · F(k)ThereforeF(k+5)=F(k+3)+F(k+4)= [3F(k + 1) —· F(k -1)]+ [3F(k + 2) 4 F(k)]= 3[F(k +1)+ F(k + 2)] - [F(k -1)+ F(k)]= 3F(k + 3) - F(k + l) (using the recurrence relation again)This completes the inductive proof. •EXAMPLE 32 T`he formulaF(n + 4) = 3F(n + 2) - F(n) for alln 21of Example 31 can also be proved without induction, using just the recurrence relation from thedefinition of Fibonacci numbers. The recurrence relationF(n + 2) = F(n) + F(n +1)can be rewritten asF(n+1)=F(n+ 2)~F(n) (1)ThenF(n+4)=F(rL+3)+F(n+2)=F(n+2)+F(n+1)+I·`(n+2) rewriLingF(n+3)= F(n + 2)+ [F(n + 2) - I·`(n)] + F(n + 2) rewriting F(n + 1) using (1)= 3F(n + 2)- F(n) •

Page 150: Mathematical Structures for Computer Science

132 cHAPTER 2 rmpis, recursion, and Analysis 0fAIg0rithmsln the inductive proof of Example 31, why is it necessary to prove n = 2 as aspecial case? •Recursively Defined SetsThe objects in a sequence are ordered—tbere is a first object. a second object, and so on. A setof objects is a collection of objects on which no ordering is imposed. Some sets can be definedrecursively.EXAMPLE 33 In Section l.l we noted that certain swings of statement letters. logical con~necuves, and parentheses, such as (A A B)' V C, are considered legitimate. while other strings,such as A A A' 'B, are not legitimate. The syntax for arranging such symbols constitutes the def-inidon of the set of propositional well-formed formulas, and it is a recursive definition.l. Any statement letter is a wff.2. lfP and Q are wffs, so are (P A Q), (P V Q), (P —> Q), (P'), and (P ·-» Q).We often omit parentheses when doing so causes no confusion; thus we write (P V Q) asP V Q, or (P') as P'. By beginning with statement letters and repeatedly using rule 2. anypropositional wff can be built. For example, A, B, and C are all wffs by rule l. By rule 2.(A A B) and (C ')are both wffs. By rule 2 again.((A A B) ·> (CU)is a wff. Applying rule 2 yet again, we get the wff(((A A B) e <C'>>'J` Eliminating some parentheses, we can write this wff as((A A B) —> C’)’ •Show how to build the wff ((A V (B')) —> C) from the definition inExample 33, •A recursive definition for the set of people who are ancestors of James couldhave the following basis:James's parents are ancestors of James.Give the inductive step. •Strings of symbols drawn from a finite "alphabet" set are objects that are commonly encoun-tered in computer science. Computers store data as binary strings, strings from the alphabetconsisting of Os and ls; compilers view program statements as swi.ngs of tokens. such as keywords and identifiers, The collection of all finite-length strings of symbols from an alphabet,

Page 151: Mathematical Structures for Computer Science

SECTION 2.4 Recursive Definitions 133usually called swings over an alphabet. can be defined recursively (see Example 34). Many sets —of strings with special properties also have recursive definitions.EXAMPLE 34 The set of all (finite·length) strings of symbols over a finite alphabet A isdenoted by A*. The recursive definition of A* is1. The empty string A (the string with no symbols) belongs to A*.2. Any single member of A belongs to A*,3. Ifx and y are swings in A*, so is xy, the concatenation of swings x and y,Parts l and 2 constitute the basis and part 3 is the recursive step of this definition. Note that forany string x, xA = Ax = x. •its = 1011 and y = 001, wl-ne me strings xy, yx, and yxlx. •Give a recursive definition for the set of all binary strings that are palindromes,swings that read the same forwards and backwards. •EXAMPLE 35 Suppose that in a certain programming language, identifiers can be alphanu-meric strings of arbitrary length but must begin with a letter. A recursive defuiidon for the set ofsuch strings isl. A single letter is an identiHer.2. If A is an identifier, so is the concatenation of A and any letter or digit.A more symbolic notation for describi.ng sets of strings that are recursively denned is calledBackus-Naur form, or BNF, originally developed to define the pr0g·a.mn1ing languageALGOL. In BNF notation, items that are defined in terms of other items are enclosed in anglebrackets, while specific items that are not further broken down do not appear in brackets. Thevertical line i denotes a choice, with the same meaning as the English word or. The BNF defi-nition of an identifier is<idendiier> ze = <1etter> | <identifier> <1etter > l <identif*ier> <digit><letter> ::= rz \ b \ c | iz<¤1git> ;;= 1 |2 | | 9Thus the identifier me2 is built from the definition by a sequence of choices such as<identifier> can be <identifier> <digit> { Awhich can be <identifier>2 ` fswhich can be <identiF1er> <letter>2 ` Vwhich can be <identifier>e2which can be <letter>e2 [ A 1which can be me2 ’? •

Page 152: Mathematical Structures for Computer Science

1 34 cuawek 2 rmais, Rem-sia". and Analysis ol/QlgcrilhmsRecursively Defined OperationsCertain operations performed on objects can be deiined recursively. as in Examples 36 and 37.EXAMPLE 36 A recursive definition of the exponentiation operation a" on a nonzero realnumber zz, where n is a nonnegative integer, is1. a¤ = 12. a" = (a"")a forn 21 •EXAMPLE 37 A recursive definition for multiplication of two positive integers m and n is1. m(1) = m2. m(n)=m(n-1)+mforn22 •Letx be a string over some alphabet. Give a recursive deinition for the opera-tion if (concatenation of x with itself n times) for n 2 1. •In Section l.l, we defined the operation of logical disjunction on two statement letters. Thiscan serve as the basis step for a recursive definition of the disjunction of n statement letters,vt 2 2:1. A1 VA: defined as in Section l.l2. AIV ···VAn= (Al V···vAn_l)VAnforn>2 (2)Using this definition, we can generalize the associative property of disjunction (tauto-logical equivalence 2a) to say that in a disjunction of n statement letters. grouping by parenthe-ses is unnecessary because all such groupings are equivalent to the general expression forthe disjunction of n statement letters. In symbolic fomi, for any n with n 2 3 and any p with1 s p s n — l.(A1 V VAP) V (APH V VA") ¢A‘ V V AnThis equivalence can be proved by induction on n. For n = 3,A1 V (A2 V A3) ·:= (A1 V A2) V A3 (by equivalence 2a)= Al V A2 V A3 (by equation (2))Assume thatforn = kand 1 Sp 5 k -1,(Al V V AP) V (AIM V VAk) QA, V v Ak

Page 153: Mathematical Structures for Computer Science

SM IIO\ :,4 k¤rnr~wu Ih~l7ninom 135Thenforn;k+land1spsk, t(Al V VAP) V (AVI V VAHI)Z (A, V VAP) V [(ApH V \/Ak) VAN,] (by equation (2))e [(A‘ V V Ap) V (AV, V V A,)] V Am (by equivalence 2a)c (Al V V Ak) VAN, (by inductive hypothesis)= A, V v AMX (by equation (2))Rerursively Defined AlgorithmsExample 29 gives a recursive definition for a sequence S. Suppose we want to write a computerprogram to evaluate $(n) for some positive integer n. We can use either of two approaches. lf wewant to find $(12), for example, we can begin with $(1) = 2 and then compute $(2), $(3), and soon, rnuch as we did in Example 29, until we finally get to $(12). This approach no doubt involvesiterating through some sort of loop. A pseudocode function S that uses this iterative algorithmfollows. The basis, where n = 1, is handled in the first clause of the if statement; the value 2 isretumed. The else clause, for n > 1, does some initializing and then goes into the while loop thatcomputes larger values of the sequence until the correct upper li.tnit is reached. You can trace theexecution of this algorithm for a few values of n to convince yourself that it works.A LGORITH MS(integer n)llfunction that iteratively computes the value S(n)//for the sequence S of Example 29Local variables:integer i //loop indexCuz-rentValue //current value of function Sif' n = 1 thenreturn 2elsei = 2CunentValue = 2while i <= yi doCurrentvalue = 2*CurrentValuei = i + 1end while//CurrentValue now has the value S(n)retum CurrentValueend ifend function S

Page 154: Mathematical Structures for Computer Science

iss cnamzn 2 rr-oois, recursion, and Analysts ofAIgorithmsThe second approach to computing $(rt) uses the recursive definition of $ directly. Followingis a version df the recursive algorithm, written again as a pseudocode function.A L c 0 RIT H M» r? ¤·——

‘‘‘* ···- ·‘.

‘· = a ~ -r ...~,;. r. e r- ’* * ,‘ ·. .- a .

The body of this function consists of a single if-then-else statement. To understand how thefunction works, let‘s trace the execution to compute the value of $(3). The function is iirstinvoked with an input value of rt = 3. Because rr is not l, execution is directed to the elseclause. At this point, activity on computing $(3) must be suspended until the value of $(2) isknown. Any known information relevant to the computation of $(3) is stored within computermemory on a stack, to be retrieved when the computation can be completed. (A stack is a col-lection of data where any new item goes on top of the stack, and only the item on top of thestack at any given time can be accessed or removed from the stack. A stack is thus a LIFO—lastin, first our—st.ructure.) The function is invoked again with an input value ofrr = 2. Again, theelse clause is executed and computation of $(2) is suspended, with relevant information storedon the stack, while t.l1e function is invoked again with rr = l as input.This time the first clause of the if statement applies, and the functional value, 2, can be conr-puted directly. This final invocation of the function is now complete, and its value of 2 isreturned to the second-to-last invocation, which can now remove any infomation relevant tothe n = 2 case from the stack, compute $(2), and retum the result to the previous (initial) invo-cation, Finally, this original invocation of $ is able to empty the stack and complete its calcula-tion, retumiug the value of $(3).What are the relative advantages of iterative and recursive algorithms for doing the sametask'! In this example, the recursive version is certainly shorter because it does not have to man-age a loop computation. Describing the execution of the recursive version makes it sound morecomplex than the iterative version, but all steps are carried out automatically. One need uct beaware of what is happening intemally except to note that a long series of recursive invocationscan use a lot of memory by storing information relevant to previous invocations on the stack. Iftoo much memory is consumed, a "stack overflow" can result. Besides using more memory,recursive algorithms can require many more computations and can run more slowly than non-recursive ones (see Exercise 7 in On the Computer at the end of this chapter).Nonetheless. recursion provides a natural way to think about many problems, some of whichwould have very complex nonrecursive solutions. The problem of computing values for asequence that has itself been defined recursively is well suited to a recursive solution. Manyprogramming languages support recursion.

Page 155: Mathematical Structures for Computer Science

sscuorv 2.4 Recursive Darwinian; 137— Write the body of a recursive function to compute T(n) for the sequence T ’defined in Practice ll. •EXAMPLE 38 In Example 37, a recursive deiinition was given for multiplying two positiveintegers m and n. A recursive pseudocode function for multiplication based on this definitionfollows. •_ A t c 0 Rl T H M

.

NQ A recursive algorithm invokes itself with "smaller" input values. Suppose a problem can be°` solved by solving smaller versions of the same problem, and the smaller versions eventuallybecome trivial cases that are easily handled. Then a recursive algorithm can be useful, even ifthe original problem was not stated recursively.¤">Y`Y To convince ourselves that a given recursive algorithm works, we don't have to start with a2; particular input and go down through smaller and smaller cases to tl·re trivial case and then backW up again. We did this when discussing the computation of $(3), but that was just to illustrate theW mechanics of a recursive computation. Instead, we can verify the trivial case (like proving therm base case in an induction proof) and verify that if the algorithm works correctly when invoicedMa on smaller input values, then it indeed solves the problem for the original input values (this issimilar to proving P(k + 1) from the assumption P(k) in an inductive proof).EXAMPLE 39 One of the most common tasks in data processing is to sort a list L of rr itemsi.nto increasing or decreasing numerical or alphabetical order. (The list might consist of cus-tomer names, for example, and in sorted order "Valdez, Juanita” should come after "Tucker,Joseph.") The selection sort algorithm—a simple but not particularly efficient sorting algo-rithm—is described in pseudocode in the accompanying box.This frmction sorts the first j items in L into increasing order; when the function is initiallyinvoked, j has the value n (thus, the first invocation ultimately sorts the entire list). The recur-sive part of the algorithm lies within the else clause; the algorithm examines the section of thelist under consideration and iinds the location i such that LM is the maximum value. It thenexchanges L[i] and L[j], after which the maximum value occurs at position j, the last positionin the part of the list being considered, L[ j] is now correct and should never change again, sothis process is repeated on the list L[l] through L[ j - 1]. If this part of the list is sorted cor-rectly, then the entire list will be sorted correctly. Whenever j has the value 1, the part of the listbeing considered consists of only one entry, which must be in the right place. The entire list issorted at that point. •

Page 156: Mathematical Structures for Computer Science

1ss cwwrrtz 2 rmrrtt,Rn.moon,.»m1m».r1·to at trqrnwmtvALGORITHM SelectionsortSelecri4mSarr(list L; integer j)//recursively sons the items from 1 to j in list L into increasing orderif j = 1 thensort is complete, write out the sorted listelsefind the index i of the maximum item in L between 1 and jexchange L[i] and L[ j]SelecticnSon(L, j — 1)end ifend function SelectionSortEXAMPLE 40 Now that we have sorted our list, another common task is to search the listfor a particular item. (Is Juanita Valdez already a customer?) An efficient search technique for asorted list is the recursive binary search algorithm, which is described in pseudocode in theaccompanying box.Atcontmm BinarysearchBinarySearch(list L; integer i; integer j; itemtype x)//searches sorted list L from LM to L[ j ] for item xifi > j thenwrite("not found”)elsefind the index k of the middle item in the list L[i]-L[ j]if x Z middle item thenwrite(“found")elseif x < middle item thenBinarySearch(L, i, k — 1,1)elseBinarySeareh(L, k + 1, j, r)end ifend ifend ifend function BinaiySearchThis algorithm searches the section of list L between L[i] and L[ j] for item at; initially i and jhave the values l and n, respectively. The first clause of the major if statement is the basis stepthat says x cannot be found in an empty list, one where the Erst index exceeds the last index. Inthe major else clause, the middle item in a section of the list must be found, (If the section con-tains an odd number of items, there is indeed a middle item; if the section contains an evennumber of items, it is sufficient to take as the "middle" item the one ar the end of the first half

Page 157: Mathematical Structures for Computer Science

SECTION 2.4 Recursive Denniriims 139of the list section.) Comparing x with the middle item either locates x or indicates which half ofthe list to search next. •EXAMPLE 41 I..et’s apply the binary search algorithm to the list3, 7, 8, 10, 14, 18, 22. 34where the target item x is the number 25. The initial list is not empty, so the middle item islocated and detennined to have the value 10. Then x is compared with the middle item. Becausex > 10, the search is invoked on the second half of the list, namely, the items14, 18, 22, 34Again, this list is nonempty, and the middle item is 18. Because x > 18, the second half of thislist is searched. namely, the items22. 34In this nonempty list, the middle item is 22. Because x > 22, the search continues on the sec-ond half of the list, namely,34This is a one·element list, with the middle item being the only item. Because x < 34, a searchis begun on the "E1rst half" of the list; but the first half is empty. The algorithm terminates at thispoint with the information thatx is not in the list.This execution requires four comparisons in all; x is compared, in turn, to 10, 18, 22,and 34. •In a binary search ofthe list in Example 41, name the elements against which1 is compared if x has the value 8. •We have now seen a number of recursive definitions. Table 2.5 summarizes their features.TABLE 2.5

Recursive DefinitionsWhat Is Being Defined CharacteristicsRecursive sequence The first one or two values in the sequence are known; lateritems in the sequence are defined in terms of earlier items.Recursive set A few specific items are known to be in the set; other items inthe set are built from combinations of items already in the set.Recursive operation A "sma11" case of the operation gives a specific value; othercases of the operation are defmed in terms of smaller cases.Recursive algorithm For the smallest values of the arguments, the algorithmbehavior is known; for larger values of the arguments, thealgorithm invokes itself with smaller argument values.

Page 158: Mathematical Structures for Computer Science

140 CHAPTER 2 Proofs, Recursion, and Analysis of AQ;urithmsSECTION 2.4 RENEW

TECHNIQUES• Generate values in a sequence defined recursively.• Prove properties of the Fibonacci sequence.W • Recognize objects in a recursively defined collection of objects.• Give recursive definitions for particular sets of objects.• Give recursive definitions for certain operations on objects.• Write recursive algorithms to generate sequences defined recursively.MAIN IDEAS• Recursive definitions can be given for sequences of objects, sets of objects. andoperations on objects where basis information is known and new informationdepends on already known information.• Recursive algorithrns provide a natural way to solve certain problems byinvoking the same task on a smaller version of the problem,EXERCISES 2.4For Exercises 1-10, write the first five values in the sequence.k 1. $(1) = IO$(21): S(n - 1)+ 10forn2 22. A(1) = 21A(n) - Am _1)forn2 23. B(l) = 1B(n) =B(n -1)+ nZforn22ir 4, $(1) = 1S(n) = S(n #1)-t-iforn 2 2S, T(1) = 1T(n) = nT(n - 1) forn 2 26. P(1) = 1P(n) = n’P(n — 1) + (ri #1)forn 2 2t 7. M(1) = 2M(2) = 2M(n) = 2M(n — 1) + M(n - 2) forn > 28, D(1) = 3D(2) = 5D(n) = (n —1)D(n r1)+(n — 2)D(n — 2) forn > 29. W(1) = 2W(2) = 3W(n) = W(n - l)W(n 7 2) forn > 2

Page 159: Mathematical Structures for Computer Science

sscnorv 2,4 Recursive ueiinimms 14110. T(1) = 1 —T(2) = 2T<3> = 3T(n) = T(n -1)+ 2T(n — 2)+ 3T(n — 3)forn > 3ln Exercises 11-15, prove the given property of the Fibonacci numbers directly from the deiinition.1: 11. F(n +1)+ F(n - 2) = 2F(n)foi-n 2 312. F(n) = 5F(n - 4) + 3F(n 4 5) forn 2 613. [F(n + l)]2 = [F(n)]* + F(n — l)F(n + 2) for ri 2 214, F(n + 3) = 2F(n +1)+ F(n) forn 2115. F(n + 6) = 4F(n + 3)+ F(n) forn 21Ln Exercises 16-19. prove the given property of the Fibonacci numbers for all n 2 1. (Him: Thefirst principle of induction will work.)1: 16. F(1) + I-`(2) + + F(n) = F(n + 2) - 117, F(2) + F(4) + + F(2n) = F(2n + 1)- 118. F(1) + F(3) + + F(2n - 1) = I-'(Zn)19. [F(1)]’ + [F(2)]2 + + [F(n)]Z = F(n)F(n + 1)In Exercises 20-23, prove the given property of the Fibonacci numbers using the second prin-ciple of induction.1 20. Exercise 1421. Exercise 1522. F(n) < Z" for n 21

23. F(ri) > for ri 2 624. The values p and q are defined as follows:p:1+2\/3 and ,1:1-Zr/Ea.Provet.hat1+p =p’a11dl+ q = qi.b. Prove thatH,.) = liP ‘ 4c. Use part (ln) to prove thatF(”)=_\/_§1+\B)~rE(12\/§>»5 2 5 2is a closed-form solution for the Fibonacci sequence.

Page 160: Mathematical Structures for Computer Science

142 CHAPTER 2 Proofs, neeursimi, and Analysis ofAlgurithmsir 25. A sequence is recursively defined by$(1) = 2S(2) = 2$(3) = 6S(n) = 3S(n - 3) forn 2 3Prove that S(n) is an even number for n 2 1.26. A sequence is recursively defined byT(5) = 6T (6) = 10T(n) = 2T(n - 2) + 2foi-n2 7Prove that T(n) 2 2n for n 2 7.27. A sequence is recursively defined by$(0) = 1$(1) = lS(n) = 2S(n -1)+ S(n · 2) forn 2 2a. Prove that S(n) is an odd number for rz 2 0b. Prove that S(n) < 6S(n - 2) forn 2 428. A sequence is recursively defined byT(0) = lT(l) = 2T(n) =2T(n- 1) + T(n-2)forn22nProve that T(n) S for rr 2 0.29. The Lucas sequence is defined byL(1) = 1L(2) = 3L(n) = L(n v 1) +L(n - 2) forn2 2a. Write the first five terms ofthe sequence.b. Prove that L(n) = F(n + 1) + F (n - 1) for n 2 2, where F is the Fibonacci sequence.For Exercises 30-33, decide whether the sequences described are subsequences of theFibonacci sequence, that is. their members are some or all of the members, in the right order, ofthe Fibonacci sequence}30. The sequence A(n), where A(n) = (n - 1)2”'i + 1, n 2 1. The first four values are 1, 2,5, 13. wl1ich—s0 far—for·m a subsequence of the Fibonacci sequence.ZEx=rcises 20-33 are taken from ·*Mamem¤nes1 Rr¤crearions" by lan smwm. smmwe American, May 1995.

Page 161: Mathematical Structures for Computer Science

sscnorv 2.4 kmusive Definitions 143ir 31. The sequence B(n), where B(n) = 1 + (the sum of the first n terms of the Fibonacci .sequence), ri 2 1. The lirst four values are 2, 3, 5, 8, which—so far—form a subsequenceof the Fibonacci sequence.32. The sequence C(n), where C(n) is t.he number of ways in which n coins can be arrangedin horizontal rows with all the coins in each row touching and every coin above thebottom row touching two coins in the row below it. ri 2 1. The {rst five values are1, 1, 2, 3, 5, which—so far—form a subsequence of the Fibonacci sequence.0 00 000 & 0000 *0 0*pi 2 1 it 2 2 it 2 3 it = 4••••• &•• •&• ••& &it = 533. The sequence D(n), where D(n) describes the number of ways to paint the floors on ann-story building where each floor is painted yellow or blue and no two adjacent floorscan be blue (although adjacent floors can be yellow). n 2 l. The first four values are2, 3, 5, 8, which—so far—form a subsequence ofthe Fibonacci sequence. For example,D(3) = 5 because a 3-story building can be paintedY Y Y B BY Y B Y YY B Y B Y(Hinr: Think about a recursive expression for D(n + 1).)34. The original problem posed by Fibonacci concemed pairs of rabbits. Two rabbits donot breed until they are two months old. After that. each pair of rabbits produces a newpair each month. No rabbits ever die. Let R(n) denote the number of rabbit pairs at theend of n months if you start with a single rabbit pair. Show that R(n) is the Fibonaccisequence.35. The sequence of Catalan numbcrr is delined recursively byC(0) = 1 _C(l) = 1nC(n) = E C(k — l)C(n — k) fern 2 2Compute the values of C(2). C(3). and C(4) using this recurrence relation.1 36. Write a recursive definition for a geometric progression with initial term a and commonratio r (see Exercise 23. Section 2.2).37. Write a recursive definition for an arithmetic progression with inidal term a and commondifference d (see Exercise 24. Section 2.2).

Page 162: Mathematical Structures for Computer Science

144 CHAPTER 2 Proofs, Recursion, and Analysis uf Algorithms* 38. ln an experiment, a certain colony of bacteria initially has a population of 50,000. Areading is taken every 2 hours, and at the end of every 2-hour interval, there are 3 timesas many bacteria as before.a. Write a recursive definition for A(n), the number of bacteria present at the beginningof the nth time period.b. At the beginning of which interval are there 1,350,000 bacteria present?39. An amount of $500 is invested in an account paying 10% interest compounded annually.a. Write a recursive definition for P(n). the amount in the account at the beginning ofthe nth year.b. After how many years will the account balance exceed $700740. A collection Tof numbers is defined recursively by1. 2 belongs to T.2. IfXbelongs to T, so doesX + 3 and 2 4= X.Which of the following belong to T?a.6 b.7 c.l9 d.l241. A collection M of numbers is dehned recursively byl. 2 and 3 belong to M.2. IfX and Ybelong to M, so does X * KWhich of the following belong to M'!a. 6 b. 9 c. 16 d. 21 e. 26 f. 54 g. 72 h. 218ik 42. A collection S of strings of characters is defined recursively byl. a and b belong to S.2. IfXbelongs to S, so does Xb.Which of the following belong to S7a. a b. ab c. aba d. uaab e. bbbblr43. A collection Wof strings of symbols is defined recursively by1. u, b, and c belong to W.2. lf X belongs to W, so does a(X)c.Which of the following belong to VWa. a(b)c b. a(a(b)c)c c. a(ubc)c d. a(a(a(a)c)c)c e. a(aacc)cik 44. Give a recursive definition for the set of all unary predicate wffs in x.45. Give a recursive definition for the set of all well-formed formulas of integer arithmetic.involving integers together with the arithmetic operations of +, -, *, and /.46. Give a recursive definition for the set of all strings of well-balanced parentheses.47. Give a recursive dehnition for the set of all binary strings containing an odd numberof Os.* 48. Give a recursive definition for 1:*. the reverse of the string x.49. Give a recursive definition for Ixl. the length of the string x.ik 50. Use BNF notation to define the set of positive integers.

Page 163: Mathematical Structures for Computer Science

sscno/v 2.4 Recursive Derinimms 14551. Use BNF notation to define the set of decimal numbers, which consist of an optional sign(+ or —), followed by one or more digits, followed by a decimal point, followed by zeroor more digits.·k 52. Give a recursive defmition for the factorial operation nl for n 2 1.53. Give a recursive defmition for the addition of two nonnegative integers m and n.54. a. Write a recursive definition for the operation of taking the maximum of n integersu,...,a,n22.b. Write a Peemsive definition for the operation of taking the minimum of n integersai, ,a”, n 2 2.55. a. Give a recursive definition for the conjunction of n statement letters in propositionallogic, rr 2 2.b, Write a generalization of the associative property of conjunction (tautological equiv-alence 2b of Section 1.1) and use induction to prove it,* 56. LetA and Bl, B2, , Bu be statement letters. Prove the finite extension of the distributiveequivalences of propositional logic:Av (B, ABZA AB”)·:>(A VB,)/x(A vB,)A A(AvB”)andAA(B,VBZV···vBn)=·(AAB,)v(AAB,)v···v(AABn)for rr 2 2.57. Let B1, B2, , Bn be statement letters. Prove the finite extension of De Morgun’s laws(B, v B, v v Bn)’ es; AB; A AB;and(B, AB, A ABn)' ¤B{ v Bl v v BQ,for n 2 2.In Exercises 58-63, write the body of a recursive function to compute S(n) for the givensequence S.58. 1, 3, 9, 27, S1,59. 2, 1, 1/2, 1/4, 1/8,* 60, 1, 2, 4, 7, 11, 16, 22, ,...61. 2, 4, 16, 256,62. a, b, a + 1:,/1 + 2b, 2n + 3/1,3u + Sb,* 63. up · 4.1: + q.1¤ r 2q.p + 2q.p 7 3q.64. What value is retumed by the following recursive function Mystery for an input value of n7Mystery (integer n)lf n = l thenretum lelsereturn Mystery(n — 1) + 1end ifend function Mystery

Page 164: Mathematical Structures for Computer Science

146 cnarrsk 2 moors, Recursion, and Analysis amlgnriehms65. The following recursive function is initially invoked with an i-value of 1. L is a list (array)of 10 integers. What does the function do?g(list L; integer i; integer x)if i > 10 thenretm·n 0elseif LM = x thenretum 10elseretum g(L, i + 1,x)end ifend ifend fmrction g* 66. Informally describe a recursive algorithm to reverse the entries in a list of items.67. Informally describe a recursive algorithm to compute the sum of the digits of a positiveinteger.68. Inforrnally describe a recursive algorithm to compute the greatest common divisor oftwo positive integers u and b where a 2 b. (Him: The solution is based‘on the Euclideanalgorithm, discussed in Section 2.3. In particular. make use of expression (5) on page 122.)69. The famous Towers of Hanoi puzzle involves three pegs with n disks of varying sizesstacked in order from the largest (on the bottom) to the smallest (on the top) on one of thepegs. The puzzle requires that the disks end up stacked the same way on a different peg;only one disk at a time can be moved to another peg. and no disk can ever be stacked ontop of a smaller disk. informally describe a recursive algorithm to solve the Towers ofHanoi puzzle.I‘Msti* 70. Simulate the execution of algorithm SelectionSort on the following list L; write the Listafter every exchange that changes the list.4, 10. -6, 2. 571. Simulate the execution of algorithm SelecrivnSarr on the following list L; write the listafter every exchange that changes the list.9, 0, 2. 6. 4

Page 165: Mathematical Structures for Computer Science

szcnolv 2.5 Recurrence Relations 14772. The binary search algorithm is used with the following list; x has the value "Chicago." _Name the elements against which x is compared.Boston, Charlotte, Indianapolis, New Orleans, Philadelphia, San Antonio, Yakima73. The binary search algorithm is used with the following list; x has the value "flour." Namethe elements against whichx is compared.butter, chocolate, eggs, flour, shortening, sugar74. Do a proof of correctness for the iterative fimction given in this section to compute $(n)of Example 29, where $(n) = 2**.ECTION 2 .5 Recurrence Relations

We developed two algorithms, one iterative and one recursive, to compute a value $(n) for thesequence S of Example 29. However, there is a still easier way to compute $(n). Recall that$(1) = 2 (1)$(n) = 2$(n e 1) for n 2 2 (2)Because$(1) = 2 = 2*$(2) = 4 = 22$(3) = 8 = 23$(4) = 16 = 2*and so on, we can see that$(n) = Z" (3)Using equation (3), we can plug in a value for n and compute S(n) without having to compute-either explicitly, or, through recursion, implicit.ly—all the lower values of S first. An equationsuch as (3), where we can substitute a value and get the output value back directly, is called aclosed-form solution to the recurrence relation (2) subject to the basis step (1). Finding aclosed-form soludon is called solving the recurrence relation.Recurrence relaclons can be used to describe a variety of things, from chemical degradation(see the opening problem for this chapter) to the amount in an interest-bearing account, fromthe growth of species to the spread of a computer virus. Clearly. it is nice to End a closed-formsolution to a recurrence relation whenever possible.Linear First-Order Recurrence RelationsExpand, Guess, and VerifyOne technique for solving recurrence relations is an "expand, guess, and verify" approach thatrepeatedly uses the recurrence relation to expand the expression for the nth term until the gen-eral pattem can be guessed. Finally the guess is verified by mathematical induction.

Page 166: Mathematical Structures for Computer Science

148 CHAPTER 2 Proofs, Recursion, and Analysis ofAIguri1hmsEXAMPLE 42 Consider again the basis step and recurrence relation for the sequence S ofExample 29:S(1) = 2 (4)S(n) = 2S(n - 1) forn 2 2 (5)Let's pretend we don‘t already know the closed-form solution and use the expand, guess, andverify approach to iind it. Beginning with S(n), we expand by using the recurrence relationrepeatedly. Keep in mind that the recurrence relation is a recipe that says S at any value can bereplaced by two times S at the previous value. We apply this recipe to S at the values n. n - l.n - 2, and so on;S(n) = 2S(n - 1)= 2[2S(n - 2)] = 22S{n — 2)= 2* [2S(n - 3)] = 2’S(n - 3)By looking at the developing pattem, we guess that after k such expansions, the equation hasthe formS(n) = 2"S(n - k)This expansion of S-values in terms of lower S-values must stop when n — k = 1, that is, whenk = n - 1. At that point,S(n) = 2""S[n — (n — l)]= 2""S(l) = 2¤·'(2) = 2*which expresses the closed—fon·n solution.We are not yet done, however, because we guessed at the general pattem. We now continnour closed-form solution by induction on the value of n. The statement we want to prove istherefore S(n) = 2” for n 2 1.For the basis step, S(l) = 2‘. This is true by equation (4). We assume that S(k) = 2*.ThenS(k + 1) = 2S(k) (by equation (5))= 2(2‘) (by the inductive hypothesis)This proves that our closed-form solution is correct. •__ Lf.; A}. Find a closed-form solution for the recurrence relation, subject to the basisstep, for sequence IZ .l. T(l) = l2. T(n) = T(rr - 1) +3forn22(Hint: Expand, guess. and verify,) •

Page 167: Mathematical Structures for Computer Science

szcnorv 2.5 Recurrence Relations 149x Solution Formula _Some types of recurrence relations have known solution formulas. A recurrence relation for aequence S(n) is linear if the earlier values of S appearing in the definition occur only to theist power. The most general linear recurrence relation has the formS(n) = j](n)S(n - 1) + )g(n)S(n f 2) + + fk(n)S(n — k) + g(n)ahere the jc 's and g can be expressions involving n. The recurrence relation has constant coef-Ecients if the jj ‘s are all constants. It is first-order if the nth term depends only on term rt - 1.Linear first~order recurrence relations with constant coefficients therefore have the formS(n) = cS(n - 1) + g(n) (6)inally, a recurrence relation is homogeneous if g(n) = 0 for all n.\\`e will ftnd the solution formula for equation (6), the general linear first-order recurrencerelation with constant coefficients, subject to the basis that $(1) is known. We will use the expand,guess. and verify approach. The work here is a generalization of what was done in Example 42.Repeatedly applying equation (6) and simplifying, we getS(n) = cS(n — 1) + g(n)= c[cS(n — 2) + g<¤ - 1)] + gw)= cJS(n — 2) + cg(n — 1) + g(n)= cz [cS(n — 3)+ g(n — 2)] + cg(n — 1)+ g(n)= c¤s(n — 3)+ czgot — 2) + ego] — 1)+ got)=.jtet It expansions, the general form appears to beS(n) = r*S(n - k) + e""g(n - (k — 1)) + + cg(n - 1)+ g(n)Q ie sequence has a base value at l. then the expansion terminates when n — k = 1 or. = n — l, at which pointS(n) = c""S(1) + c""g(2) + + eg(n — 1) + g(n)= r*"*S(1) + d"Zg(2) + + c‘g(n - 1) + c°g(n) (7)We san use stmtmation notation to write part of this expression more compactly. The upper-;;se Greek letter sigma, E. stands for summation. The notationqE (expression)r=p—e;- > to substitute into the expression successive values of i, the index of summation, from thenu er litnit p to the upper limit q. and tl1en sum the results. (See Appendix B for further discus--.·:¤ of summation notation.) Thus. for example,nE(2i-1)=l+3+5+···+(2n»l)1 Example 14, Section 2.2, we proved by induction that the value of this summation is nz.

Page 168: Mathematical Structures for Computer Science

150 CHAPTER 2 Proofs, Reeursirm, .s»d Analysis nrahyorirhmsIn summation notation, equation (7) becomesS(n) = c""S(1) + E c"'ig(i)Induction can be used, much as was done in Example 42, to verify that this formula is the solu-tion to recurrence relation (6) (see Exercise 20).Therefore, the solution to the recurrence relation (6) is” .S(n) = c"'1S(l) + 2 c""g(i) (8)This is not yet a closed-form solution, however, because we must find an expression for thesummation. Usually it is either trivial to find the sum or we found its value in Section 2.2 usingmathematical induction.The work we’ve done here gives a general solution once and for all for any recurrence rela-tion of the form shown in (6); this work need not be repeated, All that is necessary is to matchyour problem to equation (6) to find the value for c and the formula for g(n) and then plug theseresults into the expression in (8).The g(n) in Equation (6) is the usual notation for a function ofrr; although we will study functions formally in Chapter 4, you can think of g(vr) as giving a"recipe" for what to do wit.l1 its argument rr. If, for example,g(n) = 2nthen g doubles whatever its argument value is:g(3) = 2(3) = 6 g(27) = 2(27) = 54 and g(i) = 2iThis last value, 2i, would be used in Equation (8) if g(n) = Zn.EXAMPLE 43 The sequence S(n) of Example 42,S(l) = 2S(n) = 2S(n - l) forn 2 2is a linear, Hrst-order, homogeneous recurrence relation with constant coefficients. In otherwords, it matches equation (6) with c = 2 and g(rr) = 0. Because g(n) = 0, the g function eval-uates to 0 no matter what the argument is, From formula (8), the closed—form solution isnS(n) = 2"" (2) + E 0 = 2"which agrees with our previous result. •You now have a choice of two alternative ways to solve a linear, tirst-order recurrence rela-tion with constant coefncients. 'I`able 2.6 summarizes these approaches.

Page 169: Mathematical Structures for Computer Science

· sscrrorv 2.5 Recurrence Relations 151TABLE 2.6_To Solve Recurrence Relations of the Form $(n) = c$(n - 1) + g(n)Subject to Basis $(1)Method StepsExpand, guess. verify l. Repeatedly use t.he recurrence relation until youcan guess a pattem.2. Decide what that pattem will be when n ~ k = 1.3. Verify the resulting formula by induction.Solution formula 1. Match your recurrence relation to the formS(n) = cS(n - 1) + g(n) to find c and g(n).2. Use c, g(n), and $(1) in the formulaHS(n) = d"‘$(1) + E 4’*'*g(i)3. Evaluate the resulting summation to get the finalexpression.EXAMPLE 44 Find a closed-form solution to the recurrence relation$(n) = 2$(n -1)+ 3forn22subject to the basis step$(1) = 4We'll use the solution formula method. Comparing our recurrence relationS(n) = 2S(n -1)+ 3with the genera] form S(n) = c$(n - 1) + g(n), we see thatc = 2 g(n) = 3The fact that g(n) = 3 says that g has a constant value of 3 no matter what the value of its argu-rnent; in particular, g(i) = 3. Substituting into the general solution form S(n) = ¢""$(1) +E d"'g(i), we getn$(n) = 2"" (4) + E 2"" (3)n= Za-1(Z2) + 3 2 2»¤»x= 2*·** + $[2*** + 2*** + + 2* + 2°]= 2"** + 3[2’*" -1] (from Example 15)So the value of$(5), for example, is 2“ + 3(2’* - 1) = 64 + 3(15) = 109.

Page 170: Mathematical Structures for Computer Science

152 cuamzn 2 Proofs, nmmign, anu Analysis omlgamhmsAltematively, by expand-guess·verify. we expandS(n) = 2S(n - 1) + 3=2[2S(n—2)+3]+3=22S(n—2)+2·3+3»ut¥{.,¤.z.QWzV =22[2S(n—3)-l-3]+2-3+3=2’S(n-3)+22·3+2·3+3asmma ;..¤.¤¤¤ ads., ',,, ,,,,,,,,,,,,,,,> The general pattem seems to bes(»)=2*s(»—k)+2*·*·3+2*r*·3+»·-+2*-3+20+3which, when rz — k = l or lc = n - 1, becomesS(n)=2""S(1)+2"'2·3+2"'*·3+---+22-3+2-3+3= 2*··*(4) + 3[2"'2 + 2**** + + 22 + 2 +1]= 2*** + 3[2*r* — 1] (nom Example 15)Finally, we must prove by induction that S(n) = 2**** + 3[2"" - 1].Base case: n = l: S(l) = 4 = 2z + 3[2" - 1], trueAssume S(k) = 2"" + 3[2’”" — 1]Show S(k + 1) = 2**2 + 3[2" — 1]SU: + 1) = 2S(k) + 3 by the recurrence reladon= 2(2"*‘ + 3[2"'l - 1]) + 3 by the inductive hypothesis= 2**2 + 3 - 2* — 6 + 3 multiplying out= 2*** + 3[2" -1] •Rework Practice 21 using equation (8). •EXAMPLE 45 Find a closed-form solution to the recurrence relationT(n) = T(n — 1) + (n +1)forn2 2subject to the basis stepT(1) = 2Using the solution formula method and comparing the recurrence relation with the generalform from Equation (6), S(n) = cS(n ~ 1) + g(n), we find that c = 1 and g(n) = n + 1.nWe’l.l substitute intc the solution equation (8), S(n) = c""S(l) + E c"" g(i), where g(i) willbe z + 1. **2

Page 171: Mathematical Structures for Computer Science

srcnorv 2.: nmmshre nemiuhs isa¤ .Tw) = (D"' (2) + E<1>""<i+1JH= 2 + E tz + 1)=2+(3+4+...+(n+1))+ 1 + 2= (rt > tn > _ 1 OVEXAMPLE 46 Consider the problem of reading data from a computer disk drive? Thecircular drive is organized as a series of concentric tracks. divided into sectors. Each sectorcontains a block of data (Figure 2.5).·’..mecha z.sThe time to read a particular block of data into memory is composed of three components:l. Seek rime—the time to position the read head over the proper track. This time variesdepending on the relative position of the read head and the proper track when the readrequest is generated. In the best case, the read head is already over the proper track and theseek time is O. In the worst case, assuming there are rt tracks, the read head might be overtrack l and have to move to track n, which would be n — 1 units, where a unit is the dis-tance between adjacent tracks. We can assume that the seek time would be some multipleof the number of units.2. Latency time—the time for the proper sector to rotate underneath the read head. Thistime also varies, depending on whether the correct/sector is just coming under the readhead (minimum latency time) or whether it has just gone by and a full rotation must occur(maximum latency time).3This mrhpie is asm hh work mana ih "Research maiem ros uhasrgrhrum Students which Spans HardwareIssues, Msrhei-hsacsi Methods and nagsmmihg; Average seek ram; of s cmhpurer Disk,“ by Jan vim,harp;urashiry,pihttshurghaauxjsh.pim/rsashihgzpspers/ssskam».hrm1

Page 172: Mathematical Structures for Computer Science

asa cnavrrn 2 rms, ummm., and Analysis orarganmms3. Transfer rime—the time to read a block once it is positioned under the read head,usually a constant amount of time for all blocks.The problem is to find the average seek Lime, actually the average number A(n) of units. Theassumptions are that there are n tracks, the read head is positioned over some track i, and isequally likely to have to move to any track j.Table 2,7 shows the number of unirs in going from one track to another. where a row is thesource track and a column is the destination track. For example. if the read head is currentlyover track 3 and must end up over track n, then n - 3 units are required, as shown by the entryin row 3, column n. The entry in row n. column 3 is the same because it takes the same numberof units to move in the opposite direction.TABLE 2.7DestinationTrack1 2 3 n - 1 nSourceTrack1 IH 1 1 H *1 · 1 1 -11 1 I- 1 1 1-3 »-23 1 1 HB1 1-1 1-3*1-1 *1-2 *1-3 *1-1 _" 1n n — 1 n — 2 n — 2 - 1 0Table 2.7 illustrates the nz different possible track moves. We ind the average number A(n)of units for these ni cases by computing the total number of units shown in the table, T(n), andthen dividing by nz. To compute T(n), note thatT(n) = T(n - 1) + (the total of the last row plus the last column)and that the last row plus the last column contribute2[1 + 2 + 3 + + (n - 1)] = (using Practice 7)= (n - l)nso thatT(n) = T(n - 1) + (rr — l)n

Page 173: Mathematical Structures for Computer Science

SECTION 2.5 kmmenre nelarinns 155The base case is T(1) = 0 (no seek time for a 1-track disk). This is a linear, f1rst»order recur- .rence relation with constant coefiicients. We can solve it using equation (8), where c = 1 andg(n) = (n - l)n. The solution isnT(n)= 0+24+ 1)i=1·2+2·3+3-4+···+(n- 1)n- 1= (from Exercise 19, Section 2.2)Therefore the average number of units is- x _ z z _ a _ 2 ,A(n)=(n l)n(n+1)/n2=n ri -t-Zn n:n 2n:n 1:2_L3 3n 3n 3n 3 3nBecause the best case is 0 and the worst case is n - l, we might have expecned the average casezo be close to n/2. but in fact it is slightly less than n/3. •EXAMPLE 47 Not every recurrence relation fits the pattern of Equation (6). Consider therecurrence relationTU) = 1T(n)=2nT(n-1) forn22Although this is a linear, first-order recurrence relation. it does not have constant coefficients.Equation (8) does not apply. To find a closed-fon·n solution, we have to go back to the expand,guess. verify technique.T(n) = 2nT(n A 1)= 2n[2(n — l)T(n — 2)] = 2Zri(n - 1)T(n - 2)= 22n(n - 1)[2(n - 2)T(n — 3)] = 2’n(n- l)(n — 2)T(n — 3)Ln general, it seems thatT(n) = 2"n(n — 1)(n - 2) (n— (k - 1))T(n · k)\\1:enn-k= 1,thenk=n - landT(n) = 2""n(n - 1)(n — 2) (2)T(1) = 2""n(n - l)(n -— 2) (2)(1) = 2""n!This is our guess at a closed-form solution. which we verify by induction on rt.

Page 174: Mathematical Structures for Computer Science

156 ctmrrzk 2 moors, recursion, and Analysis nf AlgorithmsBase case, my Tu) = 2***11 = 2°(l) = l,trueAssume T(k): T(k) = 2""k!Show T(k + 1): T(k + 1) = 2’*(k+1)lT(k + 1) = 2(k + 1)T(k) bythe recurrence relation= 2(k + l)2""k! by the inductive hypothesis= 2*(k + 1)!This proves that our closed-form solution guess was correct, •Linear Second-order Recurrence RelationsIn a tirst-order recurrence relation. the nth term depends only on the previous term, In a second-order recurrence relation, the nth temr depends on the two previous terms. Linear second-order homogeneous recurrence relations with constant coefiicients therefore have the formS(n) = c,S(n — 1) + c,S(n - 2) (9)The Fibonacci sequence is an example (Exercise 31 asks for a solution):F(l) = 1F(2) = 1F(n)=F(n— l)+F(n-2) forn>2_ In such a sequence, we must have two "base case" values, that is, two known values of thesequence in order to generate subsequent values.We’d like to iind a general solution formula for recurrence relations like (9). If we drop thesecond term, we of course have a linear i·irst·order homogeneous recurrence relation with con-stant coefficients:S(n) = c,S(n -1)From Equation (B), we know that the solution to this recurrence relation has the formsm) = ¤;~*s(1)Let’s express this solution asSw) = pr”" (10)where r (that is, cl) is the solution (root) of the Linear equationr - cl = 0 (ll)and p (that is, $(1)) satisfies equation (10) for the ininal condition n = l:$(1) = pr‘“‘ = pr" = 1¤This viewpoint suggests a way in which we might conjecture a solution to equation (9).

Page 175: Mathematical Structures for Computer Science

sscncrv 2,5 Recurrence Relations 157Since we now have two terms in the equation itself, let‘s add a second term to (10) and repre-sent a potential solution asS(n) = prf" -1- qr;"‘ (12)ahere r, and ri are two distinct roots of (extending (1 1) to a quadratic equation)ri — pp — cz = 0 (13)The p and q will have to be chosen to satisfy the two initial conditions$(1) = prQ" -1- qrirl =p + q$(2) = prf" + qrf = pr, + qrlor. simplifying,1¤ + q = $(1)pr, + qrl = $(2) (14)Of course, this is just a wild leap of speculation on our part, so we must now verify that Equa-unn (12) is a closed-form solution to recurrence relation (9).We are trying to prove thatSm) = prim -1- qrg·*—s. here r,, rz, p. and q are as described) is a solution toS(n) = c,S(n - 1) -1- c1S(n - 2)for all n 2 1. The "for all n 2 1" phrase suggests a proof by mathematical induction. BecauseS 111 has to "reach back" two values to compute the current value, we should use the secondgnnciple of induction.Base Cases: When n = 1, the proposed solution givesS(n) =prQ" -1- qr;" =p + qWhen rx = 2, the proposed solution givesS(n) = prf" -1- qr;" = pr, + qrlBoth are trivially true because we chose p and q to meet these very conditions,Assume that for all 1. 1 s r s k, $(1) = pr;"' 1- qrfl. Show that s(k + 1) = prf-1- qrf,Before we proceed here, note that because 1, and rz are solutions of the equationr: - cl: - 1:2 = 0, it is true thatrf - clrl - cz = 0 or rf = clrl-1- cz (15)rg-clrz-c2=O or r§=c,r2+cz

Page 176: Mathematical Structures for Computer Science

15E cuAPTER 2 Pmols, rzecmion, and Analysis of AlgorirhmsNowS(k + 1) = clS(k) + cZS(k - 1) by the recurrence relation= cl (prf" + qr§") + cZ(prf'* + qrfi) by the inductive hypothesis,= prf"(c, + c,r,) + qr’;'*(:2 + girl) “PPh°d twme= prfz rf + qrfz rg by Equations (15)= prf + ar?which is the desired result. This confirms that equation (12) is a solution to equation (9).The key to the solution is the quadratic equationrz - cit - cz = 0which is called the characteristic equation of t.he recurrence relationS(n) = c,S(n - l) + cZS(n — 2)EXAMPLE 48 Solve the recurrence relationS(n) = 2S(n - 1) + 3S(n - 2) forn 2 3subject to the initial conditionsS(l) = 3$(2) = lUsing t.he recurrence relation, we can compute the first few terms of the sequence:3, 1, ll, 25. 83,ln this recurrence relation, c, = 2 and ci = 3. To find the closed-form solution. we form thecharacteristic equationri - 2: - 3 = 0which has roots rl = 3, rz = -1. Equation (12) gives the solution formS<n> = p3”" + q<·1>”"where from Equations (14) p and q satisfyp+q=31¤(3)+ ¤1<·1> =1Solving this system of equations results in p = l, q = 2. Therefore the closed-form solution isS(n) = 3"" +2(-l)"" •Check that the closed-form solution formula in Example 48 produces thecorrect first 5 terms of the sequence. •

Page 177: Mathematical Structures for Computer Science

sscnorv 2.5 Recurrence Relations 159" Q? .‘`` sewe the recurrence retsuen ·T(n) = 6T(n · 1) ·5T(n — 2) forn 2 3subject to the initial conditionsT(1) = 5T(2) = 13 •While it would seem at this point that we have the solution method in hand for any linearsecond-order homogeneous recurrence relations with constant coeiiicients, such is not the case.Consider the system of equations (14):1> + 4 = $(1)pri + qrz = $(2)We can solve the Hrst equation for p.1¤ = $(1) - 4and then substitute in the second equation to solve for q;[$(1) · ql r, + qrz = $(2)q(r2 - rl) = $(2) - S(1)r1_ $(2) · $(l)fiWhat happens if the characteristic equation 1* - cl: — cz = 0 happens to have one repeatedroot. rl = rz'! Oops—we can't solve this system of equations. The solution form when the char-acteristic equation has a repeated root r looks likesor) = pr"' + q(n — 1)w··1 rar all n 21where p and q satisfy the equationsp = $(1)pr + qr = $(2)This can be proved by induction in a manner similar to the distinct roots case (see Exercise 37).EXAMPLE 49 Solve the recurrence relationS(n) = BS(n - 1) — 16$(n - 2) forn 2 3subject to the initial conditions$(1) = 1$(2) = 12

Page 178: Mathematical Structures for Computer Science

160 cuarmz 2 Proofs, neemiarr, andxlnalysis nfAIgurithmsIn this recurrence relation. cl = 8 and cz = -16. To find the closed-form solution, we form thecharacteristic equation:2 — St + 16 = 0(i — 4)* = 0which has a repeated root r = 4. The solution issm) = p4¤·¤ + qty, — 1)4~·*wherep = 1p<4> + q<4> = 12Solving this system of equations. p = l and q = 2, so the solution isst") = 4~·1 + 2(»r — 1)4~r* = (2n — 1)4¤r* •Table 2.8 summarizes the solution steps for linear second-order homogeneous recurrence rela-tions with constant coefticients.TABLE 2.8To Solve Recurrence Relations ofthe Fonn $(n) = cl$(n -1) + cZS(rr 4 2)Subject to Initial Conditions $(1) and $(2)1. Solve the characteristic equation :1 · cit - cz = 0.2. lf the characteristic equation has distinct roots rl and rz, the solution issw) = prl"" + qrgrlwherep + q = $(1)pr] + qrz = $(2)3. lf the characteristic equation has a repeated root r, the solution isS(n) = pr"" + q(n - l)r’*"where1¤ = $(1)pr + qr = $(2)lf the roots ofthe characteristic equation tum out to be complex numbers. the solution formulasstill work.

Page 179: Mathematical Structures for Computer Science

sscncrv 2.s nemrmrm Relations 151Divide-and-Conquer Recurrence RelationsStill another recurrence relation form occurs when the value of S(n) depends not on the previ-ous term or on the two previous terms but on the value halfway back in the sequence,EXAMPLE 50 Consider the sequence with the following values:$(1) = l,S(2) = 4, $(4) = 8, $(8) = 16, $(16) = 32,We are looking at only selected terms of the sequence, namely, S(n) where n is a power of 2,For these terms, we can see that S(n) = 2S(%>. •Such recurrence relations will occur in the analysis of certain "divide-and-conquer" algo-rithms. algorithms tl1at solve a problem by breaking it into smaller versions, each half the sizeof the original (see the next section). Hence such recurrence relations are called divirle-and-conquer recurrence relations. The general form isS(n) = dg) + g(n) forn 2 2, n = 2”* (16)where c is a constant and g can be an expression involving rr. lt’s convenient to look only atvalues of n that are powers of 2 because then cutting n in half always results in an integer. Asxx a will see in the next section, this is not a significant restriction.To solve a divide-and-conquer recurrence relation, we go back to the expand, guess, andverify approach. Also, the solution will involve the logarithm function; for a review of the log-arithm function and its properties, see Appendix C.EXAMPLE 51 Solve the recurrence relationC(n)= 1 +C(;) forn22,n=2’"subject to the basis stepC(1) = 1Expanding. we getctn) = 1 +

— 1 + (1 + C(4))=l+l+(l+C(g))

Page 180: Mathematical Structures for Computer Science

1E cnavmz 2 Proofs, Recursion, and Analysis or/agorirhmsand the general term seems to beco.) = k + 0%)2The process stops when 2* = rt, or k = log, n. (We'll omit the base 2 notation from now on-log n will mean log, n. See Appendix C for a brief discussion of logarithms.) ThenC(n) =1ogri + C(1)= 1 +1ognNow we will use induction to show that C(n) = 1 + log n for all n 2 1, n = Z"'. This is asomewhat different form of induction, because the only values of interest are powers of 2. Westill take 1 as the basis step for the induction, but then we prove that if our statement is true fora value k, it is true for 2k. The statement will then be true for 1. 2, 4. 8. . that is. for all non-negative integer powers of 2. which is just what we want,C(l)=l+logl=l+0=1,trueAssume that C(k) = 1 + log k. ThenC(2k) = 1+ C(k) (by the recurrence relation)= 1 + 1 + log k (by the inductive hypothesis)=l+log2-l-logk (1og2=1)= 1 + log 2k (property of logaritlims)This completes the inductive proof. •We’d like to find a closed-form solution for (16) subject to the basis that S(1) is known. Wecould use the expand, guess, and verify approach to find the general solution, but instead wewill do a transformation on (16) to convert it to a first-order recurrence relation with constantcoefficients, use the solution formula we already have for such a recurrence relation, and thenreverse the transformation.Equation (16) assumes thatn = 2”' with n 2 2. From this it follows that m = log ri and m 2 1.Substituting 2'” for n in equation (16) results inS(2”') = cS(2""‘) + g(2"‘) (17)Now, letting T(m) represent S(2"‘) in equation (17), we getT(m) = cT(m - 1) + g(2’”) for m 2 1 (18)Equation (18) is a linear, first-order equation with constant coefficients; from equation (8),we obtain the solution

T(m) = c'”i‘T(I) + E c""’g(2') (19)

Page 181: Mathematical Structures for Computer Science

sscnorv 2.: Recurrence aeration 1s:subject to the basis condition that T(l) is known. Because equation (18) holds for m = 'l, weknow thatT(1) = cT(0) + g(2)Making this substitution in (19) results in· —·l.; _ “ . -_ i T(m) ~ c’”T<0) + E ¤’” ‘g(2') (20)mm Now reversing the substitution T(m) = S(2"‘), (20) becomes

sm) = ws(2°> + E w"·g(2·>me net "‘°*”’ ` " Finally, ming 2~· = rt or m = log yr, we getS(n) = c’°¤”S(1) + E c“°“""’g(2‘) (21)Equation (21) thus represents the solution for the recurrence relation (16). As before, to usethis general solution you need only match your recurrence relation to (I6) to determine c andg(n), then substitute into equation (21). Again as before, g(n) gives a recipe for what to do withan argument n; in Equation (21), the argument is 2‘. If you can evaluate the resulting summa-tion. you will then have a closed-form solution.EXAMPLE 52 The recurrence relationC(1) = 1C(n) = 1 + for-n 2 2,n = 2**matches Equation (16) above, with c = l and g(n) = l. Because g(rr) = 1, the g function eval-uates to l no matter what the argument is. The solution, according to formula (21). isC(n) = 1‘°¤"C(1) + E 1(‘°¤""‘(1)= 1 + (logn)(l)= l +1ognwhich agrees with our previous result, •EXAMPLE 53 Solve the recurrence relationT(l) = 3mt) = + Zn

Page 182: Mathematical Structures for Computer Science

164 CHAPTER 2 Proofs, Recursion, and Analysis of AlgorithmsThis is a match for Equation (16). where c = 2 and g(n) = Zn. Therefore g(25 = 2(2'). Substitut-ing into Equation (2l)—the solution of Equation (l6)—gives the following, where we use the factthat 2*¤¤~ = n.T(n) = 2‘°¤” T(1) + i 2‘°g”"2(2*), Zag. (Z) r '§ Z.....+r= nu) + (2*¤¤~*l)1¤g n= an + (2*¤·=~ · 2)log rz= 3n + 2n log n •fj Show that the solution to the recurrence relation$(1) = 1S(n) = zig) + 1 forn 2 2, n = 2**1is 2n — 1. (Hint: See Example 15 and note that 2‘°¤’* = n.) •SECTION 2.5 ReviewTECHNIQUESW • Solve recurrence relations by the expand. guess, and verify technique.W • Solve linear, first-order recurrence relations with constant coefficients by usinga solution formula.• Solve linear. second-order homogeneous recurrence relations with constantcoefficients by using the characteristic equation,W • Solve divide-and-conquer recurrence relations by using a solution formula.MAIN IDEA• Certain recurrence relations have closed-form solutions.EXERCISES 2.5In Exercises 1-9, solve the recurrence relation subject to the basis step.* 1. $(1) = 5S(n) = S(n -1)+ Sforn 222. F(1) = 2F(n) = 2F(n — 1) + 2** forn 2 2

Page 183: Mathematical Structures for Computer Science

srcnotv 2.s Recurrence Relations 1 653. T(1) = 1 `T(n)=2T(n— 1)+ 1forn22(Hint: See Example 15.)t 4. A(1) = 1A(n) =A(n — 1) + nforn 22(Hint: See Practice 7.)5. S(1) = 1S(n)=S(n — 1)+2n- 1forn22(Hint: See Example 14.)6. P(1) = 2P(n) = 2P(n — 1)+ n2" forn 2 2(Hint: See Practice 7.)* 7. F(l) = 1F(n) = nF(n — l)forn 2 28. S( 1) = 1S(n) = nS(n - 1) + nl forn 2 29. P(1) = 2P(n) = 3(n + l)P(n - 1) forn 2 2t 10. At the beginning of this chapter the contractor claimed:The material to be stored at the chemical disposal site degrades to inert matter at the rateof 5% per year. Therefore only about one-third of the original active material will remainat the end of 20 years.Write and solve a recurrence relation to check the contractor‘s claim; note that the end of20 years is the beginning of the 21st year.11. A colony of bats is counted every two months. The first four counts are 1200, 1800, 2700,and 4050. If this growth rate continues, what will the 12th count be? (Hint: Write andsolve a recurrence relation.)12. Spam e-mail containing a virus is sent to 1000 e-mail addresses. After 1 second, a recipi-ent machine broadcasts 10 new spam e-mails containing the virus, after which the virusdisables itself on that machine. How many e-mails are sent at the end of 20 seconds?13. In an account that pays 8% annually, $1000 is deposited. At the end of each year, an addi-tional $100 is deposited into the account. What is the account worth at the end of 7 years(that is, at the beginning of the 8th year)? (Hint: Write and solve a recurrence relation. Alsoconsult Exercise 23 of Section 2.2 for the formula for the sum of a geometric sequence.)* 14. A loan of $5000 is charged a 12% annual interest rate. An $80 payment is made eachmonth. How much is left of the loan balance at the end of 18 months (that is, at the begin-ning of the 19th month)? (Hint: Write and solve a recurrence relation. Also consult Exer-cise 23 of Section 2.2 for the formula for the sum of a geometric sequence).15. The shellfish population in a bay is estimated to have a count of about 1,000,000. Studiesshow that pollution reduces this population by about 2% per year, while other hazards arejudged to reduce the population by about 10,000 per year. After 9 years, that is, at thebeginning of year 10, what is the approximate shellfish population? (Hint: Write andsolve a recurrence relation. Also consult Exercise 23 of Section 2.2 for the formula forthe sum of a geometric sequence.)

Page 184: Mathematical Structures for Computer Science

156 CHAPTER 2 Proofs, Recursion, and Analysis of Algorithms16. A computer virus that spreads by way of e·mail messages is planted in 3 machines thefirst day. Each day. each infected machine from the day before infects 5 new machines.By the end of the second day, a software solution has been found to counteract the virus.and 1 machine is clean at that point. Each day thereafter, 6 times as many machines areclean as were clean the day before. How many days will it be before tl1e effects of thevirus are completely gone?17, This problem concems the Towers of Hanoi puzzle (see Exercise 69 in Section 2.4).a. Based on the recursive algorithm of Exercise 69 in Section 2.4. find a recurrencerelation M(n) for the number of disk moves required to solve the Towers of Hanoipuzzle rar n disks.b. Solve this recurrence relation. Consult Exercise 23 of Section 2.2 for the formula forthe sum of a geometric sequence.c. The mythical origin of the Towers of Hanoi puzzle concems 64 golden disks that agroup of monks are moving from one tower to another. When their task is complete.the world will end. Assuming that the monks can move 1 disk per second, calculatethe number of years to complete the task.at 18. Early members of the Pythagorean Society defined yigurate numbers to be the number ofdots in certain geometrical configurations. The first few triangular numbers are 1, 3, 6,and 10:. A 4; &1 3 6 l0Find and solve a recurrence relation for the nth triangular number. (Hint: See Practice 7.)19. The first few pzntagorml numbers (see the previous exercise) are 1, 5, 12, and 22:. O Q @1 s l2 zzFind and solve a recurrence relation for the nth pentagonal number. (Hint: See Exercise 24of Section 2.2 for the formula for the sum of an arithmetic sequence.)

Page 185: Mathematical Structures for Computer Science

SECTION 2.5 Recurrence Relations 16720. Use induction to verify that equation (8) of this section is the solution to the recurrence `relation (6) subject to the basis condition that $(1) is known.In Exercises 21-28, solve the recurrence relation subject to the initial conditions.21, T(1) = 5T(2) = 11T(n)=5T(n-1)-6T(n-2) forn2322. A(1) = 7A(2) = 18A(n)=6A(n- l)—8A(n-2) forn23ik 23. $(1) = 4$(2) = -2$(n)= —$(n- 1)+2S(n-2) forn2324. P(l) = 5P(2) = 17P(n) = 7P(n - 1)- 12P(n — 2) forn 2 325. F(1) = BF(2) = 16F(n) = 6F(n - 1)- 5F(n — 2) forn 2 326. T(l) = -1T(2) = 7T(n) = —4T(n -1)- 3T(n - 2) forn 2 3* 27. B(1) = 3B(2) = 14B(n) = 4B(ri — 1) - 4B(n - 2) for rt 2 328. F(1) = -10F(2) = 40F(n)= -10F(n- 1)- 25F(n-2) forn23In Exercises 29-30, solve the recurrence relation subject to the initial conditions; the solutionsinvolve complex numbers.ir 29. A(1) = 8A(2) = 8A(n)=2A(n-1)-2A(n-2) forn2330. $(1) = 4$(2) = -8$(n) = —4$(n - 1)- 5$(ri - 2) forn 2 3ir 31. Solve the Fibonnaci recurrence relationF(1) = 1F(2) = lF(n)=F(n-1)+F(n—2) forn>2Compare your answer with Exercise 24 of Section 2.4

Page 186: Mathematical Structures for Computer Science

153 CHAPTER 2 Proofs, kecursiun, and Analysis of Algorithms32. Find a closed-form solution for the Lucas sequenceL(1) = 1L(2) = 3L(n)=L(n-l)+L(n-2) forn2333. Houses in a new development go on sale initially for an average price of $200.000.At the beginning of month 2, the average sale price has risen to $250,000. At thebeginning of each succeeding month, the average price increase is half what it wasthe previous month.a. Write and solve a recurrence relation for M(n), the average sale price at the begin-ning of month n.b. At the beginning of which month is the average price within $2,000 of $300,000734. A contaminated soil site is tested monthly for the presence of a particular microorganism.Initially, 950 microorganisms per cubic foot of soil are found; at the beginning of month2, there are 1000 organisms per cubic foot. Left untreated, the growth rate of thismicroorganism increases by 25% per month.a. Write and solve a recurrence relation for O(n), the number of organisms present percubic foot at the beginning of month n.b. At the end of what month does the number of organisms first exceed 5000 per cubicfoot?35. Prove that the number of binary strings of length n with no two consecutive Os is given bythe Fibonacci sequence term F(n + 2). (Hint.- Write a recurrence relation; considerstrings of length n that end in 1 and those that end in 0.)* 36. Consider the recurrence relation S(n) = c,S(n — 1) as a linear second-order homogeneousrecurrence relation with constant coefficients where cz = 0. Solve this recurrence relationusing its characteristic equation, and prove that the solution is the same as that of Equa-tion (8).37. Prove thatS(n) = pr"' + q(n - 1)r""wherep = $(1)pr + qr = $(2)is a solution to the recurrence relation S(n) = c1S(n — 1) + c2S(n s 2) for all n 2 1 ifr is a repeated root of the characteristic equation.In Exercises 38-41, solve the recurrence relation subject to the basis step. (Hint: See Example 15,and note that 2‘°¤" = ri.)38. T(1) = 3mi) = + n fora 22,n = z~* 39. Pu) = 1P(n) = ZPG) + 3 forn 2 2,n = 2"‘

Page 187: Mathematical Structures for Computer Science

SECTION 2.a Analysis an/gmirhms 16940. $(1) = 1 ·S(rr) = ZSG) + rr forn 2 2,n = 2"*41. P(1) = 1P(n) = ZPG) + nz forn 2 2, n = 2***SECTION 2.6 Analysis of AlgorithmsThe General IdeaOften more than one algorithm can perform the same task. Because we assume that all thesealgorithms perform correctly, we need some other basis of comparison to decide which algo-rithm to use in a given situation. Several criteria could be used to judge which is the "best"algorithm. We might ask, for example, which is easiest to understand, which makes the mostefficient use of machine memory when implemented as computer code, or which runs mostefficiently. One might expect that "runrring efticiently” means standing by with a stopwatchwhile the computer code executes. But the stopwatch approach may tell us more about thespeed of the processor than it does the inherent efficiency of the algorithm. Even timing thecode for competing algorithms on the same processor and using the same input data can give amisleading picture of what might happen when the input data set is larger.Instead, we evaluate the time efficiency of an algorithm by estimating the number of opera-tions it must perform. We count only the operations that are basic to the task at hand, nut"housekeeping” operations that make just a small contribution to the total work required.The study of the efficiency of algorithms, that is, the number of operations they perform, iscalled analysis of algorithms. Various techniques for algorithm analysis have been developed.Sometimes a rather straightforward analysis can be done simply by inspecting the algorithm.EXAMPLE 54 Following is an algorithm to write out, for each student on a grade roster.the sum of m quiz grades minus the lowest grade. The outer loop goes through each of the n stu-dents; the inner loop goes through the quizzes for the current student. For each student. the suc-cessive quiz grades are added and the lowest grade is ultimately subtracted from the sum of allthe grades. These additions and subtractions seem fundamental to how the algorithm works, sowe will count the work contributed by these arithmetic operations.fori = l to n dolow = roster[i].quiz[l]sum = roster[/].quiz[l]for j = 2 to m dosum = sum + roster[i].quiz[j] //Aif roster[i].quiz [j] < low thenlow = roster[i].quiz[ j]end ifend forsum = sum - low //5write("Total for student i, " is ", sum)end for

Page 188: Mathematical Structures for Computer Science

170 r;>·mrr£sz r·m.,»x, 12.-. mntm ami \n.»/tsit on tlgt»mm.t~Subtraction occurs at the line marked //S. which is executed once for each pass through theouter loop (once for each student). a total of rt times. Addition, however, occurs at the linemarked //A. This is done within the inner loop, which executes m — 1 times for each student,that is, for each of the n passes of the outer loop. The total number of additions is thereforen(m — 1). The total number of arithmetic operations is n + n(m - 1). Of course the value ofthis expression depends on n (the number of students) and m (the number of quizzes). Thequantities n and m measure the amount of input data; virtually any algorithm’s work willchange with the input size.The algorithm also does some additional “housekeeping" work. Values are assigned to vari-ables, comparisons are done (to find the lowest quiz grade for each student), and the for loopindices i and j have to be incremented. But the number of times these operations are done alsodepends on the number of times through the loops, so their effect might be to multiply then + n(m — 1) result by some constant factor. In comparing algorithms A and B, we’re usuallylooking for bigger differences than just a constant multiple. which is why we ignore the house-keeping details. •Now suppose the task is to search a sorted list of n words or numbers for a particulartarget value x. We already have one algorithm to perform this task, the binary search algorithmfrom Section 2.4. Another algorithm for the same task, the sequential search algorithm, sim-ply compares x with each entry in the list in tum until either it is found or the list is exhausted.(This algorithm actually works on any list, sorted or not.) A pseudocode description of thesequential search algorithm is given in the following box.ALGORITHM SequentiaISearch .SequentialSearch(l.ist L; integer n; itemtype x)llsearches a list L of n items for item xLocal variable:integer i //marks position in the listi = 1while LM ae x and i < n doi = i + 1end whileif L[i] = x thenwi-ite("Found")elsewrite(“N ot found")end ifend function SequentialsearchBoth binary search and sequential search work by comparing elements from the list with thetarget value x until a match is found. In fact it is difficult to imagine how any possible searchalgorithm could avoid such comparisons, so these comparisons will be the basic operation tocount in analyzing these two algorithms.The sequential search algorithm does the maximum amount of work (number of compar-isons) when x is the last item in the list or when r does not appear in the list at all. ln either case,all elements are compared to x, so n comparisons are done. This is the "worst case" for this

Page 189: Mathematical Structures for Computer Science

srcnotv 2.6 Analysis of Algorithms 171algorithm. and the work depends on the size n of the input (the length of the list). The minimumamount of work is done when x is the very first item in the list; only one comparison is made.This is the "best case" for this algorithm. (In the algorithm of Example 54. the same number ofarithmetic operations is always done; there is no best or worst case.)There are many possibilities between the best case and the worst case. If x falls in the exactmiddle of the list, the search would require roughly n/2 comparisons. It would be helpful toobtain some measure of the "average" amount of work done. This measure would require someway to describe the average list being searched and the average relationship of a target item 1 tothat list. Exercises 23-24 in this section explore some aspects of average-case analysis for thesequential search algorithm. For most algorithms, however, average behavior is very difficult todetermine. To compare the efficiency of algorithms, therefore. we will often content ourselveswith the worst-case count of the number of operations requiredEXAMPLE 55 Given a long string of text characters, can we find the first instance of a par-ticular substxing or "pattem" within the text'! This problem has a number of important applica-tions, such as• looking for specific gene or protein sequences within a strand of DNA or aminoacids. respectively.• looking for specific strings in an HTML document that might be governed by astyle rule of' a cascading style sheet.• the UNIX grep command to search a file for a specified string, or the "Find"command in any text editor or word processor.The most intuitive (although not the most efficient) string pattem-matching algorithm com-pares the pattem (a string of length m) against the text (a string of length n with vi 2 m), start-ing with the first character of the text and marching through the pattem.T. Ti Tr ···- Tm Tm. --4- T.Pl P2 P3 .... PmIf all m characters match, the pattem has been found. If there is a mismatch at some pointbetween the text and the pattern, the pattem slides over one character in the text, and the match-ing process begins again.Tr Ti T; ··-· Tm Tm ---- TiP1 P2 P3 ..., PmThe last segment of text where the pattern could possibly occur is the last m elements of thetext. This segment begins at Tn_m_,, as shown below.Tr Tr Ti -·v· Two Ti-M ---~ Ti-M = TrPl P2 .... Pm

Page 190: Mathematical Structures for Computer Science

172 cuavmz 2 Proofs, Recursion, ammnalysis chilgurilhmsFor example, if the text is 23 characters long and the pattem is 5 characters long, then the lastpiece of text that can possibly hold the pattem is TNTZBTHTHTH.The unit of work in this algorithm is comparisons between a text character and a patterncharacter. The best case occurs when the pattem is found as the first m characters of the text,which requires m comparisons. The worst case is when the pattem does not occur in the text atall. and the pattern "window" slides all the way over to TVN!. From each of those n - m + 1starting points, the pattern fails to be found. but the worst case is if the pattem almost matchesand only the last character fails. For example. consider the text and pattern shown:Text: TTTTTTTTTTTTTPattern: TTTTTSThis requires m comparisons (the iirst m - l are matches and only the mth comparison fails) at eachof the ri - m + l starting positions, making the total number of comparisons m(n — m + l). •Analysis Using Recurrence RelationsIn this section we will analyze algorithms that are defined recursively. Because much of theactivity of a recursive algorithm takes place "out of sight" in the many invocations that canoccur, an analysis using the direct counting technique of Example 54 won’t work. Analysis ofrecursive algorithms usually involves solving a recurrence relation.EXAMPLE 56 We can recast the sequential search algorithm from an iterative version(repeating some action over and over in a loop) to a recursive version. The base case says thatyou check whether you have run off the end of the list and if not, you check for the target x atthe first position i.n the list. Lf you find it, Fine; if not. then you invoke the algorithm again on therest of the list. Here is pseudocode for the recursive function to search the list from L[i] to L[n];the function is invoked initially with i = l:Atooanmrvt St·q11:·ntiulSeurchKucursiw‘’’·*—`·‘·‘ z·‘~· ==-=*· ¢·-·aLet C(n) represent the maximum number of comparisons required for an n-element list. This is_ a symbolic expression for an answer we are assuming we don’t already know but will leam by

Page 191: Mathematical Structures for Computer Science

srcrrcrv 2.s Analysis aiagarirhms 173solving the recurrence relation. By this notation, C(n - l) symbolically represents the maxi-mum number of comparisons required to search the rest of the list after the first position. Therecurrence relation isC(l) = 1 (l comparison to search a l-element list)C(n) = l + C(n · l) for n 2 2 (l comparison against the first element,then however many comparisons are requiredfor the rest of the list)This is a first-order, linear recurrence relation with constant coefficients. By Equation (8) inSection 2.5, the solution isnC(n) = (1)""(1) + E(1)”"(1)=1+(¤ ·1)= riThis agrees with our previous analysis for the worst case. •The next example does a worst-case analysis of the binary search algorithm, Recall that binarysearch is a recursive algorithm that first does one comparison of the target with the middlevalue of the list. If this fails, then the process is repeated on half the list. So this is a divide-and-conquer algorithm, where the problem is decomposed recursively into significantly smallersubproblems, If the original list is n elements long, then half the list is at worst n/2 elementslong. (In Example 41, for instance, when l0 is the middle value, the right "hali" of the list has4 elements, but the left "hal.t" has only 3.) If we are to keep cutting the list in half, it is conven-ient to consider only the case where we get an integer value each time we cut in half, so we willassume that n = 2*** for some m 2 0.EXAMPLE 57 Let C(n) stand for the maximum number of comparisons required to do abinary search on an n-element list. Then stands for the maximum number of comparisonsrequired to search a list half that size. The recurrence relation for C(n) isC(l) = 1 (1 comparison to search a 1-element List)C(n) = l + cg for n 2 2, n = Z"' (l comparison against the middle element,then however many comparisons arerequired for the rest of the list)This recurrence relation was solved in the previous section (Examples 51 and 52). The solutionisC(n) = l + log n •By the previous example, the maximum number of comparisons required to do a binarysearch on an n-element ordered list, with ri = 2’”, is l + log n. In Example 4l, n was 8, and fourcomparisons (l + log S) were required in the worst case (x not in the list). A sequential searchwould require eight comparisons. Becausel+1ogn<nforn =2*",n24

Page 192: Mathematical Structures for Computer Science
Page 193: Mathematical Structures for Computer Science
Page 194: Mathematical Structures for Computer Science
Page 195: Mathematical Structures for Computer Science
Page 196: Mathematical Structures for Computer Science
Page 197: Mathematical Structures for Computer Science
Page 198: Mathematical Structures for Computer Science
Page 199: Mathematical Structures for Computer Science
Page 200: Mathematical Structures for Computer Science
Page 201: Mathematical Structures for Computer Science
Page 202: Mathematical Structures for Computer Science
Page 203: Mathematical Structures for Computer Science
Page 204: Mathematical Structures for Computer Science
Page 205: Mathematical Structures for Computer Science
Page 206: Mathematical Structures for Computer Science
Page 207: Mathematical Structures for Computer Science
Page 208: Mathematical Structures for Computer Science
Page 209: Mathematical Structures for Computer Science
Page 210: Mathematical Structures for Computer Science
Page 211: Mathematical Structures for Computer Science
Page 212: Mathematical Structures for Computer Science
Page 213: Mathematical Structures for Computer Science
Page 214: Mathematical Structures for Computer Science
Page 215: Mathematical Structures for Computer Science
Page 216: Mathematical Structures for Computer Science
Page 217: Mathematical Structures for Computer Science
Page 218: Mathematical Structures for Computer Science
Page 219: Mathematical Structures for Computer Science
Page 220: Mathematical Structures for Computer Science
Page 221: Mathematical Structures for Computer Science
Page 222: Mathematical Structures for Computer Science
Page 223: Mathematical Structures for Computer Science
Page 224: Mathematical Structures for Computer Science
Page 225: Mathematical Structures for Computer Science
Page 226: Mathematical Structures for Computer Science
Page 227: Mathematical Structures for Computer Science
Page 228: Mathematical Structures for Computer Science
Page 229: Mathematical Structures for Computer Science
Page 230: Mathematical Structures for Computer Science
Page 231: Mathematical Structures for Computer Science
Page 232: Mathematical Structures for Computer Science
Page 233: Mathematical Structures for Computer Science
Page 234: Mathematical Structures for Computer Science
Page 235: Mathematical Structures for Computer Science
Page 236: Mathematical Structures for Computer Science
Page 237: Mathematical Structures for Computer Science
Page 238: Mathematical Structures for Computer Science
Page 239: Mathematical Structures for Computer Science
Page 240: Mathematical Structures for Computer Science
Page 241: Mathematical Structures for Computer Science
Page 242: Mathematical Structures for Computer Science
Page 243: Mathematical Structures for Computer Science
Page 244: Mathematical Structures for Computer Science
Page 245: Mathematical Structures for Computer Science
Page 246: Mathematical Structures for Computer Science
Page 247: Mathematical Structures for Computer Science
Page 248: Mathematical Structures for Computer Science
Page 249: Mathematical Structures for Computer Science
Page 250: Mathematical Structures for Computer Science
Page 251: Mathematical Structures for Computer Science
Page 252: Mathematical Structures for Computer Science
Page 253: Mathematical Structures for Computer Science
Page 254: Mathematical Structures for Computer Science
Page 255: Mathematical Structures for Computer Science
Page 256: Mathematical Structures for Computer Science
Page 257: Mathematical Structures for Computer Science
Page 258: Mathematical Structures for Computer Science
Page 259: Mathematical Structures for Computer Science
Page 260: Mathematical Structures for Computer Science
Page 261: Mathematical Structures for Computer Science
Page 262: Mathematical Structures for Computer Science
Page 263: Mathematical Structures for Computer Science
Page 264: Mathematical Structures for Computer Science
Page 265: Mathematical Structures for Computer Science
Page 266: Mathematical Structures for Computer Science
Page 267: Mathematical Structures for Computer Science
Page 268: Mathematical Structures for Computer Science
Page 269: Mathematical Structures for Computer Science
Page 270: Mathematical Structures for Computer Science
Page 271: Mathematical Structures for Computer Science
Page 272: Mathematical Structures for Computer Science
Page 273: Mathematical Structures for Computer Science
Page 274: Mathematical Structures for Computer Science
Page 275: Mathematical Structures for Computer Science
Page 276: Mathematical Structures for Computer Science
Page 277: Mathematical Structures for Computer Science
Page 278: Mathematical Structures for Computer Science
Page 279: Mathematical Structures for Computer Science
Page 280: Mathematical Structures for Computer Science
Page 281: Mathematical Structures for Computer Science
Page 282: Mathematical Structures for Computer Science
Page 283: Mathematical Structures for Computer Science
Page 284: Mathematical Structures for Computer Science
Page 285: Mathematical Structures for Computer Science
Page 286: Mathematical Structures for Computer Science
Page 287: Mathematical Structures for Computer Science
Page 288: Mathematical Structures for Computer Science
Page 289: Mathematical Structures for Computer Science
Page 290: Mathematical Structures for Computer Science
Page 291: Mathematical Structures for Computer Science
Page 292: Mathematical Structures for Computer Science
Page 293: Mathematical Structures for Computer Science
Page 294: Mathematical Structures for Computer Science
Page 295: Mathematical Structures for Computer Science
Page 296: Mathematical Structures for Computer Science
Page 297: Mathematical Structures for Computer Science
Page 298: Mathematical Structures for Computer Science
Page 299: Mathematical Structures for Computer Science
Page 300: Mathematical Structures for Computer Science
Page 301: Mathematical Structures for Computer Science
Page 302: Mathematical Structures for Computer Science
Page 303: Mathematical Structures for Computer Science
Page 304: Mathematical Structures for Computer Science
Page 305: Mathematical Structures for Computer Science
Page 306: Mathematical Structures for Computer Science
Page 307: Mathematical Structures for Computer Science
Page 308: Mathematical Structures for Computer Science
Page 309: Mathematical Structures for Computer Science
Page 310: Mathematical Structures for Computer Science
Page 311: Mathematical Structures for Computer Science
Page 312: Mathematical Structures for Computer Science
Page 313: Mathematical Structures for Computer Science
Page 314: Mathematical Structures for Computer Science
Page 315: Mathematical Structures for Computer Science
Page 316: Mathematical Structures for Computer Science
Page 317: Mathematical Structures for Computer Science
Page 318: Mathematical Structures for Computer Science
Page 319: Mathematical Structures for Computer Science
Page 320: Mathematical Structures for Computer Science
Page 321: Mathematical Structures for Computer Science
Page 322: Mathematical Structures for Computer Science
Page 323: Mathematical Structures for Computer Science
Page 324: Mathematical Structures for Computer Science
Page 325: Mathematical Structures for Computer Science
Page 326: Mathematical Structures for Computer Science
Page 327: Mathematical Structures for Computer Science
Page 328: Mathematical Structures for Computer Science
Page 329: Mathematical Structures for Computer Science
Page 330: Mathematical Structures for Computer Science
Page 331: Mathematical Structures for Computer Science
Page 332: Mathematical Structures for Computer Science
Page 333: Mathematical Structures for Computer Science
Page 334: Mathematical Structures for Computer Science
Page 335: Mathematical Structures for Computer Science
Page 336: Mathematical Structures for Computer Science
Page 337: Mathematical Structures for Computer Science
Page 338: Mathematical Structures for Computer Science
Page 339: Mathematical Structures for Computer Science
Page 340: Mathematical Structures for Computer Science
Page 341: Mathematical Structures for Computer Science
Page 342: Mathematical Structures for Computer Science
Page 343: Mathematical Structures for Computer Science
Page 344: Mathematical Structures for Computer Science
Page 345: Mathematical Structures for Computer Science
Page 346: Mathematical Structures for Computer Science
Page 347: Mathematical Structures for Computer Science
Page 348: Mathematical Structures for Computer Science
Page 349: Mathematical Structures for Computer Science
Page 350: Mathematical Structures for Computer Science
Page 351: Mathematical Structures for Computer Science
Page 352: Mathematical Structures for Computer Science
Page 353: Mathematical Structures for Computer Science
Page 354: Mathematical Structures for Computer Science
Page 355: Mathematical Structures for Computer Science
Page 356: Mathematical Structures for Computer Science
Page 357: Mathematical Structures for Computer Science
Page 358: Mathematical Structures for Computer Science
Page 359: Mathematical Structures for Computer Science
Page 360: Mathematical Structures for Computer Science
Page 361: Mathematical Structures for Computer Science
Page 362: Mathematical Structures for Computer Science
Page 363: Mathematical Structures for Computer Science
Page 364: Mathematical Structures for Computer Science
Page 365: Mathematical Structures for Computer Science
Page 366: Mathematical Structures for Computer Science
Page 367: Mathematical Structures for Computer Science
Page 368: Mathematical Structures for Computer Science
Page 369: Mathematical Structures for Computer Science
Page 370: Mathematical Structures for Computer Science
Page 371: Mathematical Structures for Computer Science
Page 372: Mathematical Structures for Computer Science
Page 373: Mathematical Structures for Computer Science
Page 374: Mathematical Structures for Computer Science
Page 375: Mathematical Structures for Computer Science
Page 376: Mathematical Structures for Computer Science
Page 377: Mathematical Structures for Computer Science
Page 378: Mathematical Structures for Computer Science
Page 379: Mathematical Structures for Computer Science
Page 380: Mathematical Structures for Computer Science
Page 381: Mathematical Structures for Computer Science
Page 382: Mathematical Structures for Computer Science
Page 383: Mathematical Structures for Computer Science
Page 384: Mathematical Structures for Computer Science
Page 385: Mathematical Structures for Computer Science
Page 386: Mathematical Structures for Computer Science
Page 387: Mathematical Structures for Computer Science
Page 388: Mathematical Structures for Computer Science
Page 389: Mathematical Structures for Computer Science
Page 390: Mathematical Structures for Computer Science
Page 391: Mathematical Structures for Computer Science
Page 392: Mathematical Structures for Computer Science
Page 393: Mathematical Structures for Computer Science
Page 394: Mathematical Structures for Computer Science
Page 395: Mathematical Structures for Computer Science
Page 396: Mathematical Structures for Computer Science
Page 397: Mathematical Structures for Computer Science
Page 398: Mathematical Structures for Computer Science
Page 399: Mathematical Structures for Computer Science
Page 400: Mathematical Structures for Computer Science
Page 401: Mathematical Structures for Computer Science
Page 402: Mathematical Structures for Computer Science
Page 403: Mathematical Structures for Computer Science
Page 404: Mathematical Structures for Computer Science
Page 405: Mathematical Structures for Computer Science
Page 406: Mathematical Structures for Computer Science
Page 407: Mathematical Structures for Computer Science
Page 408: Mathematical Structures for Computer Science
Page 409: Mathematical Structures for Computer Science
Page 410: Mathematical Structures for Computer Science
Page 411: Mathematical Structures for Computer Science
Page 412: Mathematical Structures for Computer Science
Page 413: Mathematical Structures for Computer Science
Page 414: Mathematical Structures for Computer Science
Page 415: Mathematical Structures for Computer Science
Page 416: Mathematical Structures for Computer Science
Page 417: Mathematical Structures for Computer Science
Page 418: Mathematical Structures for Computer Science
Page 419: Mathematical Structures for Computer Science
Page 420: Mathematical Structures for Computer Science
Page 421: Mathematical Structures for Computer Science
Page 422: Mathematical Structures for Computer Science
Page 423: Mathematical Structures for Computer Science
Page 424: Mathematical Structures for Computer Science
Page 425: Mathematical Structures for Computer Science
Page 426: Mathematical Structures for Computer Science
Page 427: Mathematical Structures for Computer Science
Page 428: Mathematical Structures for Computer Science
Page 429: Mathematical Structures for Computer Science
Page 430: Mathematical Structures for Computer Science
Page 431: Mathematical Structures for Computer Science
Page 432: Mathematical Structures for Computer Science
Page 433: Mathematical Structures for Computer Science
Page 434: Mathematical Structures for Computer Science
Page 435: Mathematical Structures for Computer Science
Page 436: Mathematical Structures for Computer Science
Page 437: Mathematical Structures for Computer Science
Page 438: Mathematical Structures for Computer Science
Page 439: Mathematical Structures for Computer Science
Page 440: Mathematical Structures for Computer Science
Page 441: Mathematical Structures for Computer Science
Page 442: Mathematical Structures for Computer Science
Page 443: Mathematical Structures for Computer Science
Page 444: Mathematical Structures for Computer Science
Page 445: Mathematical Structures for Computer Science
Page 446: Mathematical Structures for Computer Science
Page 447: Mathematical Structures for Computer Science
Page 448: Mathematical Structures for Computer Science
Page 449: Mathematical Structures for Computer Science
Page 450: Mathematical Structures for Computer Science
Page 451: Mathematical Structures for Computer Science
Page 452: Mathematical Structures for Computer Science
Page 453: Mathematical Structures for Computer Science
Page 454: Mathematical Structures for Computer Science
Page 455: Mathematical Structures for Computer Science
Page 456: Mathematical Structures for Computer Science
Page 457: Mathematical Structures for Computer Science
Page 458: Mathematical Structures for Computer Science
Page 459: Mathematical Structures for Computer Science
Page 460: Mathematical Structures for Computer Science
Page 461: Mathematical Structures for Computer Science
Page 462: Mathematical Structures for Computer Science
Page 463: Mathematical Structures for Computer Science
Page 464: Mathematical Structures for Computer Science
Page 465: Mathematical Structures for Computer Science
Page 466: Mathematical Structures for Computer Science
Page 467: Mathematical Structures for Computer Science
Page 468: Mathematical Structures for Computer Science
Page 469: Mathematical Structures for Computer Science
Page 470: Mathematical Structures for Computer Science
Page 471: Mathematical Structures for Computer Science
Page 472: Mathematical Structures for Computer Science
Page 473: Mathematical Structures for Computer Science
Page 474: Mathematical Structures for Computer Science
Page 475: Mathematical Structures for Computer Science
Page 476: Mathematical Structures for Computer Science
Page 477: Mathematical Structures for Computer Science
Page 478: Mathematical Structures for Computer Science
Page 479: Mathematical Structures for Computer Science
Page 480: Mathematical Structures for Computer Science
Page 481: Mathematical Structures for Computer Science
Page 482: Mathematical Structures for Computer Science
Page 483: Mathematical Structures for Computer Science
Page 484: Mathematical Structures for Computer Science
Page 485: Mathematical Structures for Computer Science
Page 486: Mathematical Structures for Computer Science
Page 487: Mathematical Structures for Computer Science
Page 488: Mathematical Structures for Computer Science
Page 489: Mathematical Structures for Computer Science
Page 490: Mathematical Structures for Computer Science
Page 491: Mathematical Structures for Computer Science
Page 492: Mathematical Structures for Computer Science
Page 493: Mathematical Structures for Computer Science
Page 494: Mathematical Structures for Computer Science
Page 495: Mathematical Structures for Computer Science
Page 496: Mathematical Structures for Computer Science
Page 497: Mathematical Structures for Computer Science
Page 498: Mathematical Structures for Computer Science
Page 499: Mathematical Structures for Computer Science
Page 500: Mathematical Structures for Computer Science
Page 501: Mathematical Structures for Computer Science
Page 502: Mathematical Structures for Computer Science
Page 503: Mathematical Structures for Computer Science
Page 504: Mathematical Structures for Computer Science
Page 505: Mathematical Structures for Computer Science
Page 506: Mathematical Structures for Computer Science
Page 507: Mathematical Structures for Computer Science
Page 508: Mathematical Structures for Computer Science
Page 509: Mathematical Structures for Computer Science
Page 510: Mathematical Structures for Computer Science
Page 511: Mathematical Structures for Computer Science
Page 512: Mathematical Structures for Computer Science
Page 513: Mathematical Structures for Computer Science
Page 514: Mathematical Structures for Computer Science
Page 515: Mathematical Structures for Computer Science
Page 516: Mathematical Structures for Computer Science
Page 517: Mathematical Structures for Computer Science
Page 518: Mathematical Structures for Computer Science
Page 519: Mathematical Structures for Computer Science
Page 520: Mathematical Structures for Computer Science
Page 521: Mathematical Structures for Computer Science
Page 522: Mathematical Structures for Computer Science
Page 523: Mathematical Structures for Computer Science
Page 524: Mathematical Structures for Computer Science
Page 525: Mathematical Structures for Computer Science
Page 526: Mathematical Structures for Computer Science
Page 527: Mathematical Structures for Computer Science
Page 528: Mathematical Structures for Computer Science
Page 529: Mathematical Structures for Computer Science
Page 530: Mathematical Structures for Computer Science
Page 531: Mathematical Structures for Computer Science
Page 532: Mathematical Structures for Computer Science
Page 533: Mathematical Structures for Computer Science
Page 534: Mathematical Structures for Computer Science
Page 535: Mathematical Structures for Computer Science
Page 536: Mathematical Structures for Computer Science
Page 537: Mathematical Structures for Computer Science
Page 538: Mathematical Structures for Computer Science
Page 539: Mathematical Structures for Computer Science
Page 540: Mathematical Structures for Computer Science
Page 541: Mathematical Structures for Computer Science
Page 542: Mathematical Structures for Computer Science
Page 543: Mathematical Structures for Computer Science
Page 544: Mathematical Structures for Computer Science
Page 545: Mathematical Structures for Computer Science
Page 546: Mathematical Structures for Computer Science
Page 547: Mathematical Structures for Computer Science
Page 548: Mathematical Structures for Computer Science
Page 549: Mathematical Structures for Computer Science
Page 550: Mathematical Structures for Computer Science
Page 551: Mathematical Structures for Computer Science
Page 552: Mathematical Structures for Computer Science
Page 553: Mathematical Structures for Computer Science
Page 554: Mathematical Structures for Computer Science
Page 555: Mathematical Structures for Computer Science
Page 556: Mathematical Structures for Computer Science
Page 557: Mathematical Structures for Computer Science
Page 558: Mathematical Structures for Computer Science
Page 559: Mathematical Structures for Computer Science
Page 560: Mathematical Structures for Computer Science
Page 561: Mathematical Structures for Computer Science
Page 562: Mathematical Structures for Computer Science
Page 563: Mathematical Structures for Computer Science
Page 564: Mathematical Structures for Computer Science
Page 565: Mathematical Structures for Computer Science
Page 566: Mathematical Structures for Computer Science
Page 567: Mathematical Structures for Computer Science
Page 568: Mathematical Structures for Computer Science
Page 569: Mathematical Structures for Computer Science
Page 570: Mathematical Structures for Computer Science
Page 571: Mathematical Structures for Computer Science
Page 572: Mathematical Structures for Computer Science
Page 573: Mathematical Structures for Computer Science
Page 574: Mathematical Structures for Computer Science
Page 575: Mathematical Structures for Computer Science
Page 576: Mathematical Structures for Computer Science
Page 577: Mathematical Structures for Computer Science
Page 578: Mathematical Structures for Computer Science
Page 579: Mathematical Structures for Computer Science
Page 580: Mathematical Structures for Computer Science
Page 581: Mathematical Structures for Computer Science
Page 582: Mathematical Structures for Computer Science
Page 583: Mathematical Structures for Computer Science
Page 584: Mathematical Structures for Computer Science
Page 585: Mathematical Structures for Computer Science
Page 586: Mathematical Structures for Computer Science
Page 587: Mathematical Structures for Computer Science
Page 588: Mathematical Structures for Computer Science
Page 589: Mathematical Structures for Computer Science
Page 590: Mathematical Structures for Computer Science
Page 591: Mathematical Structures for Computer Science
Page 592: Mathematical Structures for Computer Science
Page 593: Mathematical Structures for Computer Science
Page 594: Mathematical Structures for Computer Science
Page 595: Mathematical Structures for Computer Science
Page 596: Mathematical Structures for Computer Science
Page 597: Mathematical Structures for Computer Science
Page 598: Mathematical Structures for Computer Science
Page 599: Mathematical Structures for Computer Science
Page 600: Mathematical Structures for Computer Science
Page 601: Mathematical Structures for Computer Science
Page 602: Mathematical Structures for Computer Science
Page 603: Mathematical Structures for Computer Science
Page 604: Mathematical Structures for Computer Science
Page 605: Mathematical Structures for Computer Science
Page 606: Mathematical Structures for Computer Science
Page 607: Mathematical Structures for Computer Science
Page 608: Mathematical Structures for Computer Science
Page 609: Mathematical Structures for Computer Science
Page 610: Mathematical Structures for Computer Science
Page 611: Mathematical Structures for Computer Science
Page 612: Mathematical Structures for Computer Science
Page 613: Mathematical Structures for Computer Science
Page 614: Mathematical Structures for Computer Science
Page 615: Mathematical Structures for Computer Science
Page 616: Mathematical Structures for Computer Science
Page 617: Mathematical Structures for Computer Science
Page 618: Mathematical Structures for Computer Science
Page 619: Mathematical Structures for Computer Science
Page 620: Mathematical Structures for Computer Science
Page 621: Mathematical Structures for Computer Science
Page 622: Mathematical Structures for Computer Science
Page 623: Mathematical Structures for Computer Science
Page 624: Mathematical Structures for Computer Science
Page 625: Mathematical Structures for Computer Science
Page 626: Mathematical Structures for Computer Science
Page 627: Mathematical Structures for Computer Science
Page 628: Mathematical Structures for Computer Science
Page 629: Mathematical Structures for Computer Science
Page 630: Mathematical Structures for Computer Science
Page 631: Mathematical Structures for Computer Science
Page 632: Mathematical Structures for Computer Science
Page 633: Mathematical Structures for Computer Science
Page 634: Mathematical Structures for Computer Science
Page 635: Mathematical Structures for Computer Science
Page 636: Mathematical Structures for Computer Science
Page 637: Mathematical Structures for Computer Science
Page 638: Mathematical Structures for Computer Science
Page 639: Mathematical Structures for Computer Science
Page 640: Mathematical Structures for Computer Science
Page 641: Mathematical Structures for Computer Science
Page 642: Mathematical Structures for Computer Science
Page 643: Mathematical Structures for Computer Science
Page 644: Mathematical Structures for Computer Science
Page 645: Mathematical Structures for Computer Science
Page 646: Mathematical Structures for Computer Science
Page 647: Mathematical Structures for Computer Science
Page 648: Mathematical Structures for Computer Science
Page 649: Mathematical Structures for Computer Science
Page 650: Mathematical Structures for Computer Science
Page 651: Mathematical Structures for Computer Science
Page 652: Mathematical Structures for Computer Science
Page 653: Mathematical Structures for Computer Science
Page 654: Mathematical Structures for Computer Science
Page 655: Mathematical Structures for Computer Science
Page 656: Mathematical Structures for Computer Science
Page 657: Mathematical Structures for Computer Science
Page 658: Mathematical Structures for Computer Science
Page 659: Mathematical Structures for Computer Science
Page 660: Mathematical Structures for Computer Science
Page 661: Mathematical Structures for Computer Science
Page 662: Mathematical Structures for Computer Science
Page 663: Mathematical Structures for Computer Science
Page 664: Mathematical Structures for Computer Science
Page 665: Mathematical Structures for Computer Science
Page 666: Mathematical Structures for Computer Science
Page 667: Mathematical Structures for Computer Science
Page 668: Mathematical Structures for Computer Science
Page 669: Mathematical Structures for Computer Science
Page 670: Mathematical Structures for Computer Science
Page 671: Mathematical Structures for Computer Science
Page 672: Mathematical Structures for Computer Science
Page 673: Mathematical Structures for Computer Science
Page 674: Mathematical Structures for Computer Science
Page 675: Mathematical Structures for Computer Science
Page 676: Mathematical Structures for Computer Science
Page 677: Mathematical Structures for Computer Science
Page 678: Mathematical Structures for Computer Science
Page 679: Mathematical Structures for Computer Science
Page 680: Mathematical Structures for Computer Science
Page 681: Mathematical Structures for Computer Science
Page 682: Mathematical Structures for Computer Science
Page 683: Mathematical Structures for Computer Science
Page 684: Mathematical Structures for Computer Science
Page 685: Mathematical Structures for Computer Science
Page 686: Mathematical Structures for Computer Science
Page 687: Mathematical Structures for Computer Science
Page 688: Mathematical Structures for Computer Science
Page 689: Mathematical Structures for Computer Science
Page 690: Mathematical Structures for Computer Science
Page 691: Mathematical Structures for Computer Science
Page 692: Mathematical Structures for Computer Science
Page 693: Mathematical Structures for Computer Science
Page 694: Mathematical Structures for Computer Science
Page 695: Mathematical Structures for Computer Science
Page 696: Mathematical Structures for Computer Science
Page 697: Mathematical Structures for Computer Science
Page 698: Mathematical Structures for Computer Science
Page 699: Mathematical Structures for Computer Science
Page 700: Mathematical Structures for Computer Science
Page 701: Mathematical Structures for Computer Science
Page 702: Mathematical Structures for Computer Science
Page 703: Mathematical Structures for Computer Science
Page 704: Mathematical Structures for Computer Science
Page 705: Mathematical Structures for Computer Science
Page 706: Mathematical Structures for Computer Science
Page 707: Mathematical Structures for Computer Science
Page 708: Mathematical Structures for Computer Science
Page 709: Mathematical Structures for Computer Science
Page 710: Mathematical Structures for Computer Science
Page 711: Mathematical Structures for Computer Science
Page 712: Mathematical Structures for Computer Science
Page 713: Mathematical Structures for Computer Science
Page 714: Mathematical Structures for Computer Science
Page 715: Mathematical Structures for Computer Science
Page 716: Mathematical Structures for Computer Science
Page 717: Mathematical Structures for Computer Science
Page 718: Mathematical Structures for Computer Science
Page 719: Mathematical Structures for Computer Science
Page 720: Mathematical Structures for Computer Science
Page 721: Mathematical Structures for Computer Science
Page 722: Mathematical Structures for Computer Science
Page 723: Mathematical Structures for Computer Science
Page 724: Mathematical Structures for Computer Science
Page 725: Mathematical Structures for Computer Science
Page 726: Mathematical Structures for Computer Science
Page 727: Mathematical Structures for Computer Science
Page 728: Mathematical Structures for Computer Science
Page 729: Mathematical Structures for Computer Science
Page 730: Mathematical Structures for Computer Science
Page 731: Mathematical Structures for Computer Science
Page 732: Mathematical Structures for Computer Science
Page 733: Mathematical Structures for Computer Science
Page 734: Mathematical Structures for Computer Science
Page 735: Mathematical Structures for Computer Science
Page 736: Mathematical Structures for Computer Science
Page 737: Mathematical Structures for Computer Science
Page 738: Mathematical Structures for Computer Science
Page 739: Mathematical Structures for Computer Science
Page 740: Mathematical Structures for Computer Science
Page 741: Mathematical Structures for Computer Science
Page 742: Mathematical Structures for Computer Science
Page 743: Mathematical Structures for Computer Science
Page 744: Mathematical Structures for Computer Science
Page 745: Mathematical Structures for Computer Science
Page 746: Mathematical Structures for Computer Science
Page 747: Mathematical Structures for Computer Science
Page 748: Mathematical Structures for Computer Science
Page 749: Mathematical Structures for Computer Science
Page 750: Mathematical Structures for Computer Science
Page 751: Mathematical Structures for Computer Science
Page 752: Mathematical Structures for Computer Science
Page 753: Mathematical Structures for Computer Science
Page 754: Mathematical Structures for Computer Science
Page 755: Mathematical Structures for Computer Science
Page 756: Mathematical Structures for Computer Science
Page 757: Mathematical Structures for Computer Science
Page 758: Mathematical Structures for Computer Science
Page 759: Mathematical Structures for Computer Science
Page 760: Mathematical Structures for Computer Science
Page 761: Mathematical Structures for Computer Science
Page 762: Mathematical Structures for Computer Science
Page 763: Mathematical Structures for Computer Science
Page 764: Mathematical Structures for Computer Science
Page 765: Mathematical Structures for Computer Science
Page 766: Mathematical Structures for Computer Science
Page 767: Mathematical Structures for Computer Science
Page 768: Mathematical Structures for Computer Science
Page 769: Mathematical Structures for Computer Science
Page 770: Mathematical Structures for Computer Science
Page 771: Mathematical Structures for Computer Science
Page 772: Mathematical Structures for Computer Science
Page 773: Mathematical Structures for Computer Science
Page 774: Mathematical Structures for Computer Science
Page 775: Mathematical Structures for Computer Science
Page 776: Mathematical Structures for Computer Science
Page 777: Mathematical Structures for Computer Science
Page 778: Mathematical Structures for Computer Science
Page 779: Mathematical Structures for Computer Science
Page 780: Mathematical Structures for Computer Science
Page 781: Mathematical Structures for Computer Science
Page 782: Mathematical Structures for Computer Science
Page 783: Mathematical Structures for Computer Science
Page 784: Mathematical Structures for Computer Science
Page 785: Mathematical Structures for Computer Science
Page 786: Mathematical Structures for Computer Science
Page 787: Mathematical Structures for Computer Science
Page 788: Mathematical Structures for Computer Science
Page 789: Mathematical Structures for Computer Science
Page 790: Mathematical Structures for Computer Science
Page 791: Mathematical Structures for Computer Science
Page 792: Mathematical Structures for Computer Science
Page 793: Mathematical Structures for Computer Science
Page 794: Mathematical Structures for Computer Science
Page 795: Mathematical Structures for Computer Science
Page 796: Mathematical Structures for Computer Science
Page 797: Mathematical Structures for Computer Science
Page 798: Mathematical Structures for Computer Science
Page 799: Mathematical Structures for Computer Science
Page 800: Mathematical Structures for Computer Science
Page 801: Mathematical Structures for Computer Science
Page 802: Mathematical Structures for Computer Science
Page 803: Mathematical Structures for Computer Science
Page 804: Mathematical Structures for Computer Science
Page 805: Mathematical Structures for Computer Science
Page 806: Mathematical Structures for Computer Science
Page 807: Mathematical Structures for Computer Science
Page 808: Mathematical Structures for Computer Science
Page 809: Mathematical Structures for Computer Science
Page 810: Mathematical Structures for Computer Science
Page 811: Mathematical Structures for Computer Science
Page 812: Mathematical Structures for Computer Science
Page 813: Mathematical Structures for Computer Science
Page 814: Mathematical Structures for Computer Science
Page 815: Mathematical Structures for Computer Science
Page 816: Mathematical Structures for Computer Science
Page 817: Mathematical Structures for Computer Science
Page 818: Mathematical Structures for Computer Science
Page 819: Mathematical Structures for Computer Science
Page 820: Mathematical Structures for Computer Science