100% found this document useful (1 vote)
550 views

Theory of Computer Science

By KLP Mishra

Uploaded by

Sanyam Sindhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
550 views

Theory of Computer Science

By KLP Mishra

Uploaded by

Sanyam Sindhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 361
OU a aed OS ae Science: : [ eon ct Languages and Computation a iLL pore Se Tie ee |) ee | Third Edition 5 id ET | [ht M | MM) Mt ti Met] K.L.P. Mishra N. Chandrasekaran Rs. 225.00 THEORY OF COMPUTER SCIENCE: Automata, Languages and Computation, 3rd Ed. K.LP. Mishra and N. Chandrasekaran © 2007 by Prentice-Hall of india Private Limited, New Delhi. All rights reserved. No part of this book may be reproduced in any form, by mimeograph or any other means, without permission in writing ftom the publisher. ISBN-978-81-203-2968-3 The export rights of this book are vested solely with the publisher. Twenty-seventh Printing (Third Edition) —... see August, 2008 Published by Asoke K. Ghosh, Prentice-Hall of India Private Limited, M-97, Connaught Circus, New Delhi-110001 and Printed by Rajkamal Electric Press, B-35/9, G.T. Kamal Road Industries Area, Delhi-110033. Contents Preface ix Notations xi 1.1 Propositions (or Statements) 1 1.1.1 Connectives (Propositional Connectives or Logical Connectives) 2 1.1.2. Well-formed Formulas 6 Truth T: for a Wells Fe . zi ‘L414 Equivalence of Well-formed_ Formulas 9 1.1.5 Logical Identities 9 1.2.1 Construction to Obtain a Disjunctive Normal 1.2.2 Construction to Obtain the Principal . Disjunctive Normal Form of a Given Formula 2 1.3__Rules of Inference for Propositional Calculus (Statement Calculus) —_/5 1.6 Supplementary Examples 26 Self-Test 31 Exercises 22 Copyrighted material iv_&_ Contents ELIM ES. 36-7 2.1 Sets. Relations and Functions 36 < und Subs 2 2.1.2 Sets with One Binary Operation 37 2.1.3 Sets with Two Binary Operations 39 214 Relations 40 2.1.5 Closure of Relations 43 2.1.6 Functions 45 2.2 Graphs and Trees 47 2.2.1 Graphs 47 22.2 . 9 2.3 Strings and Their Properties 54 2.3.1 Operations on Strings S4 2.3.2. Terminal and Nonterminal Symbols 56 2.4 Principle of Induction oF 2.4.1 Method of Proof by Induction 57 2.4.2. Modified Method of Induction 58 2.4.3 Simultaneous Induction 60 2.5 Proof by Contradiction 67 2.6 Supplementary Examples 62 Self-Test 66 Exercises 67 3. THE THEORY OF AUTOMATA 71-106 3.1 Definition of an Automaton 71 2 Description of a Finite Automaton 73 3.3. Transition Systems 74 3.4 Properties of Transition Functions 75 3.5 Acceptability of a String by a Finite Automaton 77 3.6 Nondeterministic Finite State Machines 78 3.7__ The Equivalence of DFA and NDFA 80 3.8 Mealy and Moore Models 84 3.8.1 Finite Automata with Outputs 84 3.8.2 Procedure for Transforming a Mealy Machine 3.8.3 Procedure for Transforming a Moore Machine into a Mealy Machine: 87 3.9 Minimization of Finite Automata oI 3.9.1 Construction of Minimum Automaton 92 3.10 Supplementary Examples 97 Self-Test 103 Exercises 104 Contents Sv 4, FORMAL LANGUAGES 107-135 4.1 Basic Definitions and Examples 107 4.1.1 Definition of a Grammar 109 4.1.2 Derivations and the Language Generated by a Grammar 110 Chomsky Classif 120 4,3 Languages and Their Relation 123 4.4 Recursive and Recursively Enumerable Sets 124 4.5 Operations on Languages 126 4.6 Languages and Automata 128 4.7 Supplementary Examples 129 Self-Test 132 Exercises 134 REGULAR SETS AND REGULAR GRAMMARS 136-179 5.1 Regular Expressions 136 5.1.1 Identities for Regular Expressions 138 5.2 Finite Automata and Regular Expressions 140 5.2.1 Transition System Containing A-moves 140 5.2.2. NDFAs with A-moves and Regular Expressions 142 5.2.3 Conversion of Nondeterministic Systems to Deterministic Systems 146 5.2.4 Algebraic Method Using Arden’s Theorem 148 5.2.5 Construction of Finite Automata Equivalent toa Regular Expression 153 5.2.6 Equivalence of Two Finite Automata 157 5.2.7 Equivalence of Two Regular Expressions 160 5.3. Pumping Lemma for Regular Sets 162 5.4 Application of Pumping Lemma 163 5.5 Closure Properties of Regular Sets 165 5.6 Regular Sets and Regular Grammars 167 5.6.1 Construction of a Regular Grammar Generating TiM) for a Given DFA M 168 5.6.2 Construction of a Transition System M Accepting L(G) for a Given Regular Grammar G 169 5.7 Supplementary Examples 170 Self-Test 175 Exercises 176 CONTEXT-FREE LANGUAGES 180-226 6.1 Context-free Languages and Derivation Trees 80 6.1.1 Derivation Trees 181 6.2 Ambiguity in Context-free Grammars 188 vi Contents 6.3 Simplification of Context-free Grammars 189 6.3.2 Elimination of Null Productions 196 6.3.3 Elimination of Unit Productions 199 6.4 Normal Forms for Context-free Grammars 201 6.4.1 Chomsky Normal Form 201 6.4.2. Greibach Normal Form 206 6.5 Pumping Lemma for Context-free Languages 2/3 217 6.6 Decision Algorithms for Context-free Languages 6.7 Supplementary Examples 2/8 Self-Test 223 Exercises 224 PUSHDOWN AUTOMATA 7.1 Basic Definitions 227 7.2 Acceptance by pda —-233 7.3. Pushdown Automata and Context-free Languages 7.4 Parsing and Pushdown Automata 251 7.4.1 Top-down Parsing —-252 7.4.2. Top-down Parsing Using Deterministic pda’s 7.4.3 Bottom-up Parsing 258 7.5 Supplementary Examples 260 Self-Test 264 Exercises 265 LR(k) GRAMMARS 8.1 LR(k) Grammars 267 8.2 Properties of LR(k) Grammars 270 8.3 Closure Properties of Languages 272 8.4 Supplementary Examples 272 Self-Test 273 Exercises 274 TURING MACHINES AND LINEAR BOUNDED AUTOMATA 9.1 Turing Machine Model 278 9.2 Representation of Turing Machines 279 9.2.1 Representation by Instantaneous Descriptions 9.2.2 Representation by Transition Table 280 9.2.3. Representation by Transition Diagram 287 9.3 Language Acceptability by Turing Machines 283 9.4 Design of Turing Machines 284 9.5 Description of Turing Machines 289 227-266 256 267-276 277-308 279 Contents vii 10. 1. 9.6 Techniques for TM Construction 289 9.6.1 Turing Machine with Stationary Head 289 9.6.2 Storage in the State 290 9.6.3 Multiple Track Turing Machine 290 9.6.4 Subroutines 290 9.7. Variants of Turing Machines 292 9.7.1 Multtape Turing Machines 292 Nondeterministic Turing Machines: 295 9.8 The Model of Linear Bounded Automaton 297 9.8.1 Relation Between LBA and Context-sensitive Languages 299 9.9 Turing Machines and Type 0 Grammars 299 9.9.1 Construction of a Grammar Corresponding to TM 299 9.10 Linear Bounded Automata and Languages 30! 9.11 Supplementary Examples 303 Self-Test 307 Exercises 308 DECIDABILITY AND RECURSIVELY ENUMERABLE LANGUAGES 309-321 10.1 The Definition of an Algorithm 309 10.2 Decidability 370 10.3 Decidable Languages 3// 10.4 Undecidable Languages 313 10.5 Halting Problem of Turing Machine 314 10.6 The Post Correspondence Problem 315 10.7 Supplementary Examples 317 Self-Test 319 Exercises 319 COMPUTABILITY 322-345 1L.L Introduction and Basic Concepts 322 11.2 Primitive Recursive Functions 323 11.2.1 Initial Functions 323 11.2.2 Primitive Recursive Functions Over N 325 11.2.3 Primitive Recursive Functions Over {a, b} 327 11,3 Recursive Functions 329 114 Partial Recursive Functions and Turing Machines 332 11.4.1 Computability 332 A Turing Model for Computation 333 Turing-computable Functions 333 Construction of the Turing Machine That Can Compute the Zero Function Z 334 11.4.5 Construction of the Turing Machine for Computing— The Successor Function SIS viii & Contents 11.4.6 Construction of the Turing Machine for Computing the Projection U" 336 11.4.7. Construction of the Turing Machine That Can Perform Composition 338 11.4.8 Construction of the Turing Machine That Can Perform Recursion 339 11.4.9 Construction of the Turing Machine That Can Perform Minimization 340 11.5 Supplementary Examples 340 Self-Test 342 Exercises 343 12. COMPLEXITY 346-371 12.1 Growth Rate of Functions 346 12.2 12.6 12.7 12.8 12.9 The Classes P and NP 349 Polynomial Time Reduction and NP-completeness 351 Importance of NP-complete Problems 352 SAT is NP-complete 353 12.5.1 Boolean Expressions 353 12.5.2. Coding a Boolean Expression 353 12.5.3. Cook's Theorem 354 Other NP-complete Problems. 359 Use of NP-completeness 360 Quantum Computation 360 12.8.1 Quantum Computers 361 12.8.2. Church-Turing Thesis 362 12.8.3 Power of Quantum Computation 363 12.8.4 Conclusion 364 Supplementary Examples 365 Self-Test 369 Exercises 370 Answers to Self-Tests 373-374 Solutions (or Hints) to Chapter-end Exercises 375-415 Further Reading 417-418 Index 419-422 Preface The enlarged third edition of Theory of Computer Science is the result of the enthusiastic reception given to earlier editions of this book and the feedback received from the students and teachers who used the second edition for several years. The new edition deals with all aspects of theoretical computer science, namely automata, formal languages, computability and complexity. Very few books combine all these theories and give adequate examples. This book provides numerous examples that illustrate the basic concepts. It is profusely illustrated with diagrams. While dealing with theorems and algorithms, the emphasis is on constructions. Each construction is immediately followed by an example and only then the formal proof is given so that the student can master the technique involved in the construction before taking up the formal proof. The key feature of the book that sets it apart from other books is the provision of detailed solutions (at the end of the book) to chapter-end exercises. The chapter on Propositions and Predicates (Chapter 10 of the second edition) is now the first chapter in the new edition. The changes in other chapters have been made without affecting the structure of the second edition. The chapter on Turing machines (Chapter 7 of the second edition) has undergone major changes. A novel feature of the third edition is the addition of objective type questions in each chapter under the heading Self-Test. This provides an opportunity to the student to test whether he has fully grasped the fundamental concepts. Besides, a total number of 83 additional solved examples have been added as Supplementary Examples which enhance the variety of problems dealt with in the book. x & Preface The sections on pigeonhole principle and the principle of induction (both in Chapter 2) have been expanded. In Chapter 5, a rigorous proof of Kleene’s theorem has been included. The chapter on LR(k) grammars remains the same Chapter 8 as in the second edition. Chapter 9 focuses on the treatment of Turing machines (TMs). A new section on high-level description of TM has been added and this is used in later examples and proofs. Some techniques for the construction of TMs have been added in Section 9.6. The multitape Turing machine and the nondeterministic Turing machine are discussed in Section 9.7. A new chapter (Chapter 10) on decidability and recursively enumerable languages is included in this third edition. In the previous edition only a sketchy introduction to these concepts was given. Some examples of recursively enumerable languages are given in Section 10.3 and undecidable languages are discussed in Section 10.4. The halting problem of TM is discussed in Section 10.5. Chapter 11 on computability is Chapter 9 of the previous edition without changes. Chapter 12 is a new chapter on complexity theory and NP-complete problems. Cook's theorem is proved in detail. A section on Quantum Computation is added as the last section in this chapter. Although this topic does not fall under the purview of theoretical computer science, this section is added with a view to indicating how the success of Quantum Computers will lead to dramatic changes in complexity theory in the future. The book fulfils the curriculum needs of undergraduate and postgraduate students of computer science and engineering as well as those of MCA courses. Though designed for a one-year course, the book can be used as a one- semester text by a judicious choice of the topics presented. Special thanks go to all the teachers and students who patronized this book over the years and offered helpful suggestions that have led to this new edition. In particular, the critical comments of Prof. M. Umaparvathi, Professor of Mathematics, Seethalakshmi College, Tiruchirapalli are gratefully acknowledged. Finally, the receipt of suggestions, comments and error reports for further improvement of the book would be welcomed and duly acknowledged. K.L.P. Mishra N. Chandrasekran Notations Symbol Meaning Section in which the symbol appears first and is explained T ‘Truth value ll F False value 1 a The logical connective NOT Ll A The logical connective AND ray v The logical connective OR 1a => The logical connective IF ... THEN ... Ll e ‘The logical connective If and Only if 1 T Any tautology Ll F Any contradiction Ll v For every 14 a There exists 14 = Equivalence of predicate formulas 14 aeA ‘The element a.belongs to the set A 211 ACB ‘The set A is a subset of set B 2411 6 ‘The null set 244 AUB The union of the sets A and B 244 AUB The intersection of the sets A and B 2.4 A-B ‘The complement of B in A 24.1 a The complement of A 2A a The power set of A 241 AXB The cartesian product of A and B 2.11 xi xii_ Notations Symbol Meaning Section in which the symbol appears first and is explained VA The union of the sets Aj, Ay, ... Ay 212 “0 Binary operations 2.1.2, 2.1.3 xRy xis related to y under the relation 214 xR’y x is not related to y under the relation R 214 i=j modulo n i is congruent to j modulo n 214 G The equivalence class containing a 214 R The transitive closure of R 215 R The reflexive-transitive closure of R 21S R, OR, The composite of the relations R, and Ry 2415 pxor Map/function from a set X to a set ¥ 2.1.6 fe) ‘The image of x under f 2.1.6 fl The smallest integer 2 x 2.2.2 Et The set of all strings over the alphabet set E 23 A The emply string 23 z The set of all nonempty strings over E 23 lal The length of the string x 23 (Q. . & a0. Fi A finite automaton 3.2 ¢ Left endmarker in input tape 32 $ Right endmarker in input tape 32 @. 3. & Q, F) A transition system 33 (Q. 5. 4. & Aq) A Mealy/Moore machine 38 © Partition conesponding to equivalence of states 39 m Partition corresponding to k-equivalence of states 3.9 (Vy EP, S) A grammar 4.11 asp a directly derives 6 in grammar G 4.1.2 asp a@ derives B in grammar G 412 ap a: derives Bin n steps in grammar G 4.1.2 UG) ‘The language generated by G 412 Lo The family of type 0 languages 43 ai The family of context-sensitive languages 43 La ‘The family of context-free languages 43 La ‘The family of regular languages 43 R +R The union of regular expressions R, and Ry 5 RR; The concatenation of regular expressions R, and R; 5.1 R* The iteration (closure) of R 5.1 Notations B xi Symbol Meaning Section in which the symbol appears first and is explained a The regular expression corresponding to {a} $A Yl The number of elements in Vy 63 @iLrd 4 % FY A pushdown automaton 1 ID An instantaneous description of a pda 71 ke ‘A move relation in a pda A vat (50, 696,F) A Turing machine O41 - The move relation in a TM Tn) Maximum of the running time of Mf Abra Sets Aca Sets Acsc Sets Am Sets HALT ry Sets Za) The image of x under zero function See) The image of x under successor function uf ‘The projection function nil @) The image of x under nil function cons a(x) ‘The concatenation of a and x cons (3) ‘The concatenation of b and x pa) The image of x under predecessor function = ‘The proper subtraction function Example 11.8 ke ‘The absolute value of x 12 id Identity function 112 by The minimization function 113 Ma The characteristic function of the set A Exercise 11.8 Olgin)) The order of g(n) 124 own') The order of né 12.1 P and NP Classes 122 lw> quantum bit (qubit) 12.8.1 PSPACE Classes 12.83 EXP Classes 1283 BPP Classes BOP Classes, NPI Classes Propositions and Predicates Mathematical logic is the foundation on which the proofs and arguments rest. Propositions are statements used in mathematical logic, which are either que or false but not both and we can definitely say whether a proposition is tue or false. In this chapter we introduce propositions and logical connectives. Normal forms for well-formed formulas are given. Predicates are introduced. Finally, we discuss the rules of inference for propositional calculus and predicate calculus. 1.1 PROPOSITIONS (OR STATEMENTS) A proposition (or a statement) is classified as a declarative sentence to which only one of the truth values, i.e. true or false, can be assigned. When a proposition is true, we say that its truth value is 7. When it is false, we say that its truth value is F. Consider, for example, the following sentences in English: . New Delhi is the capital of India. . The square of 4 is 16. . The square of 5 is 27. . Every college will have a computer by 2010 a.p. Mathematical logic is a difficult subject. . Chennai is a beautiful city. . Bring me coffee. . No, thank you. . This statement is false. Pananawvne The sentences 1-3 are propositions. The sentences 1 and 2 have the truth value T. The sentence 3 has the truth value F. Although we cannot know the truth value of 4 at present, we definitely know that it is true or false, but not both. 1 2 Theory of Computer Science So the sentence 4 is a proposition. For the same reason, the sentences 5 and 6 are propositions. To sentences 7 and 8, we cannot assign truth values as they are not declarative sentences. The sentence 9 looks like a proposition. However, if we assign the truth value T to sentence 9, then the sentence asserts that it is false. If we assign the truth value F to sentence 9, then the sentence asserts that it is true. Thus the sentence 9 has either both the truth values (or none of the two truth values). Therefore, the sentence 9 is not a proposition. We use capital letters to denote propositions. 1.1.1 CONNECTIVES (PROPOSITIONAL CONNECTIVES OR LOGICAL CONNECTIVES) Just as we form new sentences from the given sentences using words like ‘and’, ‘but’, ‘if, we can get new propositions from the given propositions using ‘connectives’. But a new sentence obtained from the given propositions using connectives will be a proposition only when the new sentence has a truth value either T or F (but not both). The truth value of the new sentence depends on the (logical) connectives used and the truth value of the given propositions. We now define the following connectives. There are five basic connectives. (i) Negation (NOT) (ii) Conjunction (AND) (iii) Disjunction (OR) (iv) Implication (IF ... THEN ...) (v) If and Only If. Negation (NOT) If P is a proposition then the negation P or NOT P (read as ‘not P’) is a Proposition (denoted by — P) whose truth value is T if P has the truth value F, and whose truth value is F if P has the truth value 7. Usually, the truth values of a proposition defined using a connective are listed in a table called the truth table for that connective (Table 1.1). TABLE 1.1 Truth Table for Negation P =P T F F T Conjunction (AND) If P and Q are two propositions, then the conjunction of P and Q (read as ‘P and Q’) is a proposition (denoted by P A Q) whose truth values are as given in Table 1.2. Chapter |: Propositions and Predicates @ 3 TABLE 1.2 Truth Table for Conjunction P Q PAQ T T T T F F F r F F F F Disjunction (OR) If P and Q are two propositions, then the disjunction of P and Q (read as ‘P or Q’) is a proposition (denoted by P v Q) whose truth values are as given in Table 1.3. TABLE 1.3. Truth Table for Disjunction P Q PvQ r T T T F E F T T F F F It should be noted that P v Q is true if P is true or Q is true or both are true, This OR is known as inclusive OR, ic. either P is true or Q is true or both are true. Here we have defined OR in the inclusive sense. We will define another connective called exclusive OR (either P is true or Q is true, but not both, i.e. where OR is used in the exclusive sense) in the Exercises at the end of this chapter. EXAMPLE 1.1 If P represents ‘This book is good’ and Q represents “This book is cheap’, write the following sentences in symbolic form: (a) This book is good and cheap. (b) This book is not good but cheap. (c) This book is costly but good. (d) This book is neither good nor cheap. (e) This book is either good or cheap. Solution (a) PAQ (bl) nPAQ (c) nOAP (d) 3P A =Q fe) Pv@ Note: The truth tables for P ~ Q and Q a P coincide. So P ~ Q and Q A P are equivalent (for the definition, see Section 1.1.4). But in natural languages this need not happen. For example, the two sentences, 4 & Theory of Computer Science namely ‘I went to the railway station and boarded the train’, and ‘I boarded the train and went to the railway station’, have different meanings. Obviously, we cannot write the second sentence in place of the first sentence. Implication (IF ... THEN ...) If P and Q are two propositions, then “IF P THEN Q’ is a proposition (denoted by P => Q) whose truth values are given Table 1.4. We also read P = Q as ‘P implies Q’. TABLE 1.4 Truth Table for Implication anal ans sana ot We can note that P => Q assumes the truth value F only if P has the truth value T and Q has the truth value F. In all the other cases, P = Q assumes the truth value 7. In the case of natural languages, we are concerned about the truth values of the sentence ‘IF P THEN Q’ only when P is true. When P is false, we are not concerned about the truth value of ‘IF P THEN Q’. But in the case of mathematical logic, we have to definitely specify the truth value of P = Q in all cases. So the truth value of P => Q is defined as T when P has the truth value F (irrespective of the truth value of Q). EXAMPLE 1.2 Find the truth values of the following propositions: (a) If 2 is not an integer, then 1/2 is an integer. (b) If 2 is an integer, then 1/2 is an integer. Solution Let P and Q be ‘2 is an integer’, ‘1/2 is an integer’, respectively. Then the proposition (a) is true (as P is false and Q is false) and the proposition (b) is false (as P is true and Q is false). The above example illustrates the following: ‘We can prove anything if we start with a false assumption.’ We use P => Q whenever we want to ‘translate’ any one of the following: ‘P only if Q', *P is a sufficient condition for Q’, ‘@ is a necessary condition for P’, ‘Q follows from P’, ‘Q whenever P’, ‘Q provided P’ If and Only If If P and Q are two statements, then ‘P if and only if Q’ is a statement (denoted by P <> Q) whose truth value is 7 when the truth values of P and Q are the same and whose truth value is F when the statements differ. The truth values of P <= @Q are given in Table 1.5. Chapter 1: Propositions and Predicates & 5 TABLE 4.5 Truth Table for If and Only If P Q PeQ T Tr T T F F F T F F F T Table 1.6 summarizes the representation and meaning of the five logical connectives discussed above. TABLE 1.6 Five Basic Logical Connectives Connective Resulting proposition Read as Negation = =P Not P Conjunction » PrQ P and Q Disjunction v PvQ PoQ (or both) Implication = P=Q If P then Q (P implies Q) If and only if <= PeQ P if and only if Q EXAMPLE 1 Translate the following sentences into propositional forms: (a) If it is not raining and I have the time, then I will go to a movie. (b) It is raining and I will not go to a movie. (c) It is not raining. () I will not go to a movie. (e) I will go to a movie only if it is not raining. Solution Let P be the proposition ‘It is raining’. Let Q be the proposition ‘I have the time’. Let R be the proposition ‘I will go to a movie’. Then (a) (APAQ>R (b) PAAR oP If P, Q, R are the propositions as given in Example 1.3, write the sentences in English corresponding to the following propositional forms: aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 1: Propositions and Predicates B 7 Notes: (1) A wff is not a proposition, but if we substitute a proposition in place of a propositional variable, we get a proposition. For example: @) AP Vv QA A0A R) > Qis a wo. (ii) (GP A Q) & Qisa wif. (2) We can drop parentheses when there is no ambiguity. For example, in propositions we can remove the outermost parentheses. We can also specify the hierarchy of connectives and avoid parentheses. For the sake of convenience, we can refer to a wff as a formula. 1.1.3. TRUTH TABLE FOR A WELL-FORMED FORMULA If we replace the propositional variables in a formula a by propositions, we get a proposition involving connectives. The table giving the truth values of such a proposition obtained by replacing the propositional variables by arbitrary propositions is called the truth table of a. If @ involves n propositional constants, then we have 2” possible combinations of truth values of propositions replacing the variables. EXAMPLE 1.5 Obtain the truth table for @ = (P v Q) a (P > Q) A (Q => P). Solution The truth values of the given wff are shown in Table 1.7. TABLE 1.7 Truth Table of Example 1.5 P Q@ PvQ@ P3Q (Pv@nPs® @Q=P a r T T T T T T TO OF r F F t F F a t r r F F FOF F T F T F Le NN on Construct the truth table for @ = (P v Q) = ((P v R) = (Rv Q)). Solution The truth values of the given formula are shown in Table 1.8. 8 f_Theory of Computer Science TABLE 1.8 Truth Table of Example 1.6 PyvR RvQ (PvR=(RvVQ (Pv Q) aAaAMAAAAA| VD amasanss|o nanan] WANA WAST AA WHA Sassmnsss|e S44 Some formalas have the truth value T for all possible assignments of truth values to the propositional variables. For example, P v — P has the truth value T irrespective of the truth value of P. Such formulas are called tautologies. Definition 1.3 A tautology or a universally true formula is a well-formed formula whose truth value is T for all possible assignments of truth values to the propositional variables. For example, P v —P, (P A Q) => P, and (P > Q) A(Q > R) > (P => R) are tautologies. Note: When it is not clear whether a given formula is a tautology, we can construct the truth table and verify that the truth value is T for all combinations of truth values of the propositional variables appearing in the given formula. AMPLE 1 Show that a@ = (P = (Q = R)) = ((P = Q) = (P = R)) is a tautology. Solution We give the truth values of @ in Table 1.9, TABLE 1.9 Truth Table of Example 1.7 Q>R PS(Q>R) PSQ PSR (PSAS(PSR) ANAAVAA] V anasaansas|o manana] Sansa AAA AAT TAA A444 AAAS ANAS AA Definition 1.4 A contradiction (or absurdity) is a wff whose truth value is F for all possible assignments of truth values to the propositional variables. For example, PAP and (PAQ) AAO are contradictions. Note: ois a contradiction if and only if —@ is a tautology. Chapter 1: Propositions and Predicates & 9 1.1.4 EQUIVALENCE OF WELL-FORMED FORMULAS Definition 1.5 Two wifs o: and f in propositional variables P;, P2, ..-. Py are equivalent (or logically equivalent) if the formula a 2 B is a tautology. When @ and fi are equivalent, we write ao = B Notes: (1) The wffs o and 6 are equivalent if the truth tables for a and f are the same. For example, PAQ=QaP and PrP SP (2) It is important to note the difference between a = B and a = B. @ & Bisa formula, whereas o = fis not a formula but it denotes the relation between @ and B. EXAMPLE 1.8 Show that (P = (Q v R)) = ((P = Q) v (P => R)). Solution Let a = (P = (Q v R)) and B = (P = Q) v (P = R)). We construct the truth values of a and f for all assignments of truth values to the variables P, Q and R. The truth values of o and B are given in Table 1.10. TABLE 1.10 Truth Table of Example 1.8 QvRP=(QVR P3Q PaR P2QVE AR manasa]. maaan} o mana nans} Waa AAAS Saas SATs Sts As the columns corresponding to @ and f coincide, a = B. As the truth value of a tautology is 7, irrespective of the truth values of the propositional variables, we denote any tautology by T. Similarly, we denote any contradiction by F. 1.1.5 LOGICAL IDENTITIES Some equivalences are useful for deducing other equivalences. We call them identities and give a list of such identities in Table 1.11. The identities /,-I,. can be used to simplify formulas. If a formula B is part of another formula o, and f is equivalent to 8’, then we can replace B by B’ in & and the resulting wff is equivalent to @ 10 & Theory of Computer Science TABLE 1.11 Logical Identities J, Idempotent laws: _PvPeP, PaPeP __ 1, Commutative laws: PyvQ=QvP, PraQ=QaP t, Associative laws: Py (QvR)=(PvQ)y 1, Distributive laws: PY(QAR=(PYQAPVR, PAQYR)=(PAQV PAR) ig Absorption laws Py(PAQ=R Pa(PvQ=P Ig DeMorgan’s laws: a(P vy Q) = =P A 1, Double negation PeabhP) Ip PvaP=T, Pa ly PvT#T, Pats PyF=P PaF= Io 3 Qa (P33 = 5P_ ty Contrapositive: (Qa R= (PAQAR ha eae ae Show that (P 4 Q) v (PA 4Q) = P. Solution L.H.S. = (P A Q) v (PA 7Q) Pra@v-7Q) by using the distributive law (i.e. 14) PAT by using Js =P by using fo =sRHS. EXAMPLE 1.10 Show that (P > Q) a(R > Q) =(PvVR)>OQ Solution LHS. = (P > Q) a(R = Q) (GP Vv Q) A (GRY Q) by using 11 (Q v =P) A (@ v AR) by using the commutative law (ie. 12) QV (GP ARR) by using the distributive law (i.e. 1) QvaPvR) by using the DeMorgan’s law (i.e. Je) (AYP V R)VQ by using the commutative law (i.e. /) =(PVR>Q by using [3 = RHS. Chapter 1: Propositions and Predicates @ 11 1.2 NORMAL FORMS OF WELL-FORMED FORMULAS We have seen various well-formed formulas in terms of two propositional variables, say, P and Q. We also know that two such formulas are equivalent if and only if they have the same truth table. The number of distinct truth tables for formulas in P and Q is 2*. (As the possible combinations of truth values of P and Q are TT, TF, FT, FF, the truth table of any formula in P and Q has four rows. So the number of distinct truth tables is 2*.) Thus there are only 16 distinct (nonequivalent) formulas, and any formula in P and Q is equivalent to one of these 16 formulas. In this section we give a method of reducing a given formula to an equivalent form called the ‘normal form’. We also use ‘sum’ for disjunction, ‘product’ for conjunction, and ‘literal’ either for P or for — P, where P is any propositional variable. Definition 1.6 An elementary product is a product of literals. An elementary sum is a sum of literals. For example, P A AQ, aPAAQ, PAO, AP AQ are elementary products. And P v —Q, P v -R are elementary sums. Definition 1.7 A formula is in disjunctive normal form if it is a sum of elementary products. For example, P v (Q 4 R) and P v (4Q ~ R) are in disjunctive normal form. P a (Q v R) is not in disjunctive normal form. 1.2.1 CONSTRUCTION TO OBTAIN A DisjuNCTIVE NORMAL FORM OF A GIVEN FORMULA Step 1. Eliminate = and'¢ using logical identities. (We can use [ix ic. P=>Q=s=(-P v Q).) Step 2. Use DeMorgan’s laws (/,) to eliminate — before sums or products. The resulting formula has — only before the propositional variables, i.e. it involves sum, product and literals. Step 3 Apply distributive laws (J;) repeatedly to eliminate the product of sums. The resulting formula will be a sum of products of literals, i.e. sum of elementary products. EXAMPLE 1.11 Obtain a disjunctive normal form of Pv(AP>(@Qv(@>-R)) Solution Pv (AP =(Q@v(@Q => -—=8))) = PV (AP = (QV AQ Vv -R)) (step 1 using 412) =Pv (Pv @v GQ v-R)) (step LV using 1,2 and ;) 12_ Theory of Computer Science =PVPVQvVAQVAR by using 7; =SPVQvAQGQVoAR by using /, Thus, P v Qv =Q v Ris a disjunctive normal form of the given formula. iN eae Obtain the disjunctive normal form of (Pa 7A(Q 4 Rf) v (P = Q) Solution (PAa(Q AR) v (P= Q) (PaAA(QAR)V (Pv Q) (step 1 using 12) PaGO@v7R))v GP Vv Q) (step 2 using 4) (PARQ) V(PATR)V AP VQ (step 3 using 4 and 45) Therefore, (P 4 + Q) v (PAAR) V =P v Qis a disjunctive normal form of the given formula. For the same formula, we may get different disjunctive normal forms. For example, (PA Qa R) v (PA QA aR) and P AQ are disjunctive normal forms of P ~ Q. So, we introduce one more normal form, called the principal disjunctive normal form or the sum-of-products canonical form in the next definition. The advantages of constructing the principal disjunctive normal form are: (i) For a given formula, its principal disjunctive normal form is unique. (ii) Two formulas are equivalent if and only if their principal disjunctive normal forms coincide. Definition 1.8 A minterm in n propositional variables Pj, .... P, Q, AQ)... * Q,, where each Q; is either P; or =P; For example, the minterms in P; and P, are P; A P aP, A Po, P| APs —P; A a P> The number of minterms in n variables is 2". n iS Definition 1.9 A formula cris in principal disjunctive normal form if a is a sum of minterms. 1.2.2 CONSTRUCTION TO OBTAIN THE PRINCIPAL DIsJUNCTIVE NORMAL FORM OF A GIVEN FORMULA Step 1 Obtain a disjunctive normal form. Step 2 Drop the elementary products which are contradictions (such as Pa-P). Step 3 If P; and —P; are missing in an elementary product @ replace a@ by (aa P) v (@ A AP). Chapter 1: Propositions and Predicates @ 13 Step 4 Repeat step 3 until all the elementary products are reduced to sum of minterms. Use the idempotent laws to avoid repetition of minterms. EXAMPLE 1.13 Obtain the canonical sum-of-products form (i.e. the principal disjunctive normal form) of a= Pv(APAAOAR) Solution Here a is already in disjunctive normal form. There are no contradictions. So we have to introduce the missing variables (step 3). a P A 1Q a R in @is already a minterm. Now, P=(PaQ)v (Pa3Q) E(PAQARVV(PAQAAR)V (PATQAR)V (PATO AAR) E(PAOAR)V(PAQAR)V (PASOAR)V (PATOA-=R)) Therefore, the canonical sum-of-products form of @ is (PAQAR)V(PAQAAR)V(PATO AR) VP ASQATR)V (AP ATOAR) CN ee Obtain the principal disjunctive normal form of a@=(4Pv4Q) > (AP AR) Solution @= (AP Vv 30) = GP AR) = (AP v AQ) v (AP AR) by using I)> =(PAQ)V (PAR) by using DeMorgan’s law B(PAQAR)V (PAQ AAR) V (APARA QV APARA-Q)) =(PAQAR)V(PAQ ATR) V APAQARV (AP AAQAR) So, the principal disjunctive normal form of @ is (PAQARV(PAQGAARY GPAQAR)Y GPAAQAR) A minterm of the form Q; A Q) A... A @, can be represented by 4a)... dy, Where a; = 0 if Q; = +P; and a; = 1 if Q; = P; So the principal disjunctive normal form can be represented by a ‘sum’ of binary strings. For example, (P A QAR) Vv (PA QA-R)V (AP AQAA R) can be represented by 111 v 110 v 001. The minterms in the two variables P and Q are 00, 01, 10, and 11. Each wff is equivalent to its principal disjunctive normal form. Every principal disjunctive normal form corresponds to the minterms in it, and hence to a 14 _&_Theory of Computer Science subset of {00, Ol, 10, 11}. As the number of subsets is 24, the number of distinct formulas is 16. (Refer to the remarks made at the beginning of this section.) The truth table and the principal disjunctive normal form of @ are closely related. Each minterm corresponds to a particular assignment of truth values to the variables yielding the truth value T to @ For example, PA QAR corresponds to the assignment of T, T, F to P, Q and R, respectively. So, if the truth table of @ is given, then the minterms are those which correspond to the assignments yielding the truth value T to a EXAMPLE 1.15 For a given formula @, the truth values are given in Table 1.12. Find the principal disjunctive normal form. TABLE 1.12 Truth Table of Example 1.15 mans] oD aN! oO mans nans| D sansa ® Solution We have T in the column corresponding to the rows 1, 4, 5 and 8. The minterm corresponding to the first row is Pa QAR. Similarly, the minterms corresponding to rows 4, 5 and 8 are respectively PAAQATR, APAQAR and 3PAAQA-AR. Therefore, the principal disjunctive normal form of @ is (PAQARV(PATQAATRVGPAQARV GP ATQA AR) We can form the ‘dual’ of the disjunctive normal form which is termed the conjunctive normal form. Definition 1.10 A formula is in conjunctive normal form if it is a product of elementary sums. If @ is in disjunctive normal form, then + @ is in conjunctive normal form. (This can be seen by applying the DeMorgan’s laws.) So to obtain the conjunctive normal form of @, we construct the disjunctive normal form of = a and use negation. Definition 1.11 A maxterm in n propositional variables P;, P2, «++, Py is Q,| Vv Q) v «++ Vv Q, where each Q; is either P; or 5 P;. Chapter 1: Propositions and Predicates & 15 Definition 1.12 A formula o: is in principal conjunctive normal form if a is a product of maxterms. For obtaining the principal conjunctive normal form of , we can construct the principal disjunctive normal form of — a and apply negation. La TN de a) Find the principal conjunctive normal form of @ = P v (Q => R). Solution na=A(Pv (@ => R)) =A(PV(AQvR) by using fz AP «(4 (4@ Vv R))_ by using DeMorgan’s law aPA(Qa-7R) by using DeMorgan’s law and J, —PaQA-R is the principal disjunctive normal form of = a. Hence, the principal conjunctive normal form of @ is AGPAQAAR=PVAQVR The logical identities given in Table 1.11 and the normal forms of well-formed formulas bear a close resemblance to identities in Boolean algebras and normal forms of Boolean functions. Actually, the propositions under v, ~ and — form a Boolean algebra if the equivalent propositions are identified. T and F act as bounds (ie. 0 and 1 of a Boolean algebra). Also, the statement formulas form a Boolean algebra under v, A and — if the equivalent formulas are identified. The normal forms of well-formed formulas correspond to normal forms of Boolean functions and we can ‘minimize’ a formula in a similar manner. 1.3. RULES OF INFERENCE FOR PROPOSITIONAL CALCULUS (STATEMENT CALCULUS) In logical reasoning, a certain number of propositions are assumed to be true, and based on that assumption some other propositions are derived (deduced or inferred). In this section we give some important rules of logical reasoning or mules of inference. The propositions that are assumed to be true are called hypotheses or premises. The proposition derived by using the rules of inference is called a conclusion. The process of deriving conclusions based on the assumption of premises is called a valid argument. So in a valid argument we are concerned with the process of arriving at the conclusion rather than obtaining the conclusion. The rules of inference are simply tautologies in the form of implication (i.e. P = Q). For example, P = (P v Q) is such a tautology, and it is a rule of inference. We write this in the form Poe Here P denotes a premise. The proposition below the line, ic. P VQ is the conclusion. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 1: Propositions and Predicates W 17 de ed Can we conclude $ from the following premises? @P=@Q Gi) PR (iii) —(Q a R) (iv) SVP Solution The valid argument for deducing S from the given four premises is given as a sequence. On the left, the well-formed formulas are given. On the right, we indicate whether the proposition is a premise (hypothesis) or a conclusion. If it is a conclusion, we indicate the premises and the rules of inference or logical identities used for deriving the conclusion. 1P=3Q Premise (i) 2.P=>R Premise (ii) 3. (P > Q) a (P => R) Lines 1, 2 and RI, 4. 3(Q 0 R) Premise (iii) 5.n0vVAR Line 4 and DeMorgan’s law (Ig) 6. a~Pv AP Lines 3, 5 and destructive dilemma (Rio) 7.4P Idempotent law /; 8. SvP Premise (iv) 9. S Lines 7, 8 and disjunctive syllogism Rl Thus, we can conclude $ from the given premises. ae de) Derive S from the following premises using a valid argument: (i) P>=@Q Gi) QR (iii) Pv S (iv) R Solution LPs@g Premise (i) 2.Q@354R Premise (ii) 3.P=>4R Lines 1, 2 and hypothetical syllogism Rly 4k Premise (iv) 5. a(R) Line 4 and double negation 6. AP Lines 3, 5 and modus tollens Ris 7,.PVS Premise (iii) 8.8 Lines 6, 7 and disjunctive syllogism Rls Thus, we have derived S from the given premises. 18 _& Theory of Computer Science EXAMPLE 1.19 Check the validity of the following argument: If Ram has completed B-E. (Computer Science) or MBA, then he is assured of a good job. If Ram is assured of a good job, he is happy. Ram is not happy. So Ram has not completed MBA. Solution We can name the propositions in the following way: P denotes “Ram has completed BE. (Computer Science)’. Q denotes ‘Ram has completed MBA’. R denotes ‘Ram is assured of a good job’. S denotes ‘Ram is happy’. The given premises are: i) (PVQ>R ii) R=>S Gii) 4S The conclusion is — Q. 1. (PVQ)3R Premise (i) 2R=35 Premise (ii) 3. (PV Q) = S__Lines 1, 2 and hypothetical syllogism Rf, 4.58 Premise (iii) 5. A(P v Q) Lines 3, 4 and modus tollens R/; 6. AP AAG DeMorgan’s law Js 7.30 Line 6 and simplification R/5 Thus the argument is valid. EXAMPLE 1.20 Test the validity of the following argument: If milk is black then every cow is white. If every cow is white then it has four legs. If every cow has four legs then every buffalo is white and brisk. The milk is black. Therefore, the buffalo is white. Solution We name the propositions in the following way: P denotes ‘The milk is black’. Q denotes ‘Every cow is white’. R denotes ‘Every cow has four legs’. S denotes ‘Every buffalo is white’. T denotes ‘Every buffalo is brisk’. Chapter 1: Propositions and Predicates 19 The given premises are: (i) P=>Q Gi) Q>R Gil) R > SAT (iv) P The conclusion is S. 1 P Premise (iv) 2.P>@Q Premise (i) 3.Q Modus ponens Ri 4.Q0>5R Premise (ii) 5.R Modus ponens RI, 6. R=>SAT Premise (iii) 7SAT Modus ponens Rly 8. 8 Simplification Rl, Thus the argument is valid. 1.4. PREDICATE CALCULUS Consider two propositions ‘Ram is a student’, and ‘Sam is a student’. As propositions, there is no relation between them, but we know they have something in common. Both Ram and Sam share the property of being a student. We can replace the two propositions by a single statement ‘x is a student’. By replacing x by Ram or Sam (or any other name), we get many propositions. The common feature expressed by ‘is a student’ is called a predicate. In predicate calculus we deal with sentences involving predicates. Statements involving predicates occur in mathematics and programming languages. For example, ‘2x + 3y = 4z’, ‘IF (D. GE. 0.0) GO TO 20° are statements in mathematics and FORTRAN, respectively, involving predicates. Some logical deductions are possible only by ‘separating’ the predicates. 1.4.1 PREDICATES A part of a declarative sentence describing the properties of an object or relation among objects is called a predicate. For example, ‘is a student’ is a predicate. Sentences involving predicates describing the property of objects are denoted by P(x), where P denotes the predicate and x is a variable denoting any object. For example, P(x) can denote ‘x is a student’. In this sentence, x is a variable and P denotes the predicate ‘is a student’. The sentence ‘x is the father of y’ also involves a predicate ‘is the father of’. Here the predicate describes the relation between two persons. We can write this sentence as F(x, y), Similarly, 2x + 3y = 4z can be described by S@ y, 2) aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 1: Propositions and Predicates mM 21 The logical connectives involving predicates can be used for declarative sentences involving predicates. The following example illustrates the use of connectives. EXAMPLE 1.21 Express the following sentences involving predicates in symbolic form: . All students are clever. . Some students are not successful. . Every clever student is successful. . There are some successful students who are not clever. . Some students are clever and successful. ARUN Solution ‘As quantifiers are involved, we have to specify the universe of discourse. We can take the universe of discourse as the set of all students. Let C(x) denote ‘x is clever’. Let S(x) denote ‘x is successful’. Then the sentence 1 can be written as Vx C(x). The sentences 2-5 can be written as Ax ( S(x)), Vx (C(x) = S(x)), Ar (Sx) A A CQ), Ax(C(x) — SQ) 1.4.2 WELL-FORMED FORMULAS OF PREDICATE CALCULUS A well-formed formula (wff) of predicate calculus is a string of variables such AS X1, X, ..., Xn, Connectives, parentheses and quantifiers defined recursively by the following rules: @) P(x, .. + %_) is a wff, where P is a predicate involving n variables Xp Xa os Me (ii) If @ is a wff, then 5 @ is a wff. (iii) If a and B are wffs, then av B anf a> ZB ae Bare also wffs. (iv) If @ is a wff and x is any variable, then Vx(q@), Ix (a) are wffs. (v) A string is a wff if and only if it is obtained by a finite number of applications of rules (i)-(iv). Note: A proposition can be viewed as a sentence involving a predicate with 0 variables. So the propositions are wffs of predicate calculus by rule (i). We call wffs of predicate calculus as predicate formulas for convenience. The well-formed formulas introduced in Section 1.1 can be called proposition formulas (or statement formulas) to distinguish them from predicate formulas. 22 Theory of Computer Science Definition 1.14 Let @ and fi be two predicate formulas in variables 4, .. ., 4% and let U be a universe of discourse for @ and f& Then a@ and f are equivalent to each other over U if for every possible assignment of values to each variable in @ and f the resulting statements have the same truth values. We can write a = B over U. We say that a and f are equivalent to each other (@ = 8) if @ = B over U for every universe of discourse U. Remark In predicate formulas the predicate variables may or may not be quantified. We can classify the predicate variables in a predicate formula, depending on whether they are quantified or not. This leads to the following definitions. Definition 1.15 If a formula of the form Ax P(x) or Vx P(x) occurs as part of a predicate formula a, then such part is called an x-bound part of @ and the occurrence of x is called a bound occurrence of x. An occurrence of x is free if it is not a bound occurrence. A predicate variable in @ is free if its occurrence is free in any part of a In & = Ay PCr, x) A (Vx, OC, x5), for example, the occurrence of 4 in Sx, Pa), x2) is a bound occurrence and that of x2 is free. In Vx7 Q(x, x3), the occurrence of x2 is a bound occurrence. The occurrence of x; in a is free. Note: The quantified parts of a predicate formula such as Vx P(x) or Sx P(x) are propositions. We can assign values from the universe of discourse only to the free variables in a predicate formula a Definition 1.16 A predicate formula is valid if for all possible assignments of values from any universe of discourse to free variables, the resulting propositions have the truth value 7. Definition 1.17 A predicate formula is satisfiable if for some assignment of values to predicate variables the resulting proposition has the truth value T. Definition 1.18 A predicate formula is unsatisfiable if for all possible assignments of values from any universe of discourse to predicate variables the resulting propositions have the truth value F. We note that valid predicate formulas correspond to tautologies among proposition formulas and the unsatisfiable predicate formulas correspond to contradictions. Chapter 1: Propositions and Predicates BQ 23 1.5 RULES OF INFERENCE FOR PREDICATE CALCULUS Before discussing the rules of inference, we note that: (i) the proposition formulas are also the predicate formulas; (ii) the predicate formulas (where all the variables are quantified) are the proposition formulas. Therefore, all the rules of inference for the proposition formulas are also applicable to predicate calculus wherever necessary. For predicate formulas not involving connectives such as A(x), P(x, y), we can get equivalences and rules of inference similar to those given in Tables 1.11 and 1.13. For Example, corresponding to Js in Table 1.11 we get (PQ) v Q()) = 4 (P(X) A 7 (Q(x)). Corresponding to Ri; in Table 1.13 P AQ = P, we get P(x) » Q(x) = P(x). Thus we can replace propositional variables by predicate variables in Tables 1.11 and 1.13. Some necessary equivalences involving the two quantifiers and valid implications are given in Table 1.14. TABLE 1.14 Equivalences Involving Quantifiers hs Distributivity of 3 over v: Ax(P(x) v QQ) = Ax Pls) v Fx Q(x) 3x(P v Qi) = Pv Gr Qt) 7 Distributivity of ¥ over a: Vx(Plx) A Q(x) = Vx P(x) a Wx Q(x) va(PA Qi) = Pa (Wx Ql) vx (PQ) he Al¥xP(a) = 3e (PQ) hr B(PA QQ) =P A GrQH) he Wx(P v Q(x) = Pv (¥xQ()) Rho Vx P(x) = 3x Pix) “Ri; (EPL) v Vr Qh) > WP VOW) Rhz 3r(PQ) 0 QQ) > PG) A 2rQG) Sometimes when we wish to derive some conclusion from a given set of premises involving quantifiers, we may have to eliminate the quantifiers before applying the rules of inference for proposition formulas. Also, when the conclusion involves quantifiers, we may have to introduce quantifiers. The necessary rules of inference for addition and deletion of quantifiers are given in Table 1.15. 24 & Theory of Computer Science TABLE 1.15 Rules of Inference for Addition and Deletion of Quantifiers Rig: Universal instantiation Yx P(x) Pc) ¢ is some element of the universe. Riy: Existential instantiation Bx P(x) P(c) ¢ is some element for which P(c) is true. "Rls. Universal generalization — P(x) VeP(s) x should not be free in any of the given premises. Rig: Existential generalization P(e) “3x PQ) 218 some element of the universe. EXAMPLE 1.22 Discuss the validity of the following argument: All graduates are educated. Ram is a graduate. Therefore, Ram is educated. Solution Let G(x) denote ‘x is a graduate’. Let E(x) denote ‘x is educated’. Let R denote ‘Ram’. So the premises are (i) Vx (G(x) => E(x)) and (ii) G(R). The conclusion is E(R). Vx(G@) = EQ) Premise (i) G(R) = E(R) Universal instantiation Ri,3 G(R) Premise (ii) <. E(R) Modus ponens Ri, Thus the conclusion is valid. Chapter 1: Propositions and Predicates & 25 EXAMPLE 1.23 Discuss the validity of the following argument: All graduates can read and write. Ram can read and write. Therefore, Ram is a graduate. Solution Let G(x) denote ‘x is a graduate’. Let L(x) denote ‘x can read and write’. Let R denote ‘Ram’. The premises are: Vx (G(x) =» L(x) and L(R). The conclusion is G(R). (GR) > L(R)) 4 L(R)) = G(R) is not a tautology. So we cannot derive G(R). For example, a school boy can read and write and he is not a graduate. EXAMPLE 1.24 Discuss the validity of the following argument: All educated persons are well behaved. Ram is educated. No well-behaved person is quarrelsome. Therefore, Ram is not quarrelsome. Solution Let the universe of discourse be the set of all educated persons. Let P(x) denote ‘x is well-behaved’. Let y denote ‘Ram’. Let Q(x) denote ‘x is quarrelsome’. So the premises are: (i) Vx PQ). (ii) y is a particular element of the universe of discourse. (ii) Vx (P@) => 4). To obtain the conclusion, we have the following arguments: 1. Wx P(x) Premise (i) 2. Poy) Universal instantiation Riy3 3. Vx(PQ@) = 4 Q(x) Premise (iii) 4. PO) = =Q0) Universal instantiation RI,3 5. P(y) Line 2 6. = 00) Modus ponens Ri, 1 Q() means that ‘Ram is not quarrelsome’. Thus the argument is valid. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 28 & Theory of Computer Science Opposite—If the triangle is not isoceles, then two of its sides are not equal. Contrapositive—If two of the sides of a triangle are not equal, then the triangle is not isoceles. (b) Converse—If the Indians won't go to the USA for cmployment, then there is no unemployment in India. Opposite—If there is unemployment in India, then the Indians will go to the USA for employment. (c) Contrapositive—If the Indians go to the USA for employment, then there is unemployment in India. EXAMPLE 1.29 Show that: GPAAAAR)VQAARV(PAR GR Solution GP A(QAR)VQAR)V (PAR) @ (GP AQ) AR) V (QAR) (PA R) by using the associative law @ (Pv Q) AR) V (QAR) VY (PAR) by using the DeMorgan’s law ee (PV OAR VY PYAR) by using the distributive law Se PVOViPV OAR by using the commutative and distributive laws eTAR by using Js OR by using fy EXAMPLE 1.30 Using identities, prove that: Ov (Pa 7Q)v (AP AQ) is a tautology Solution Qv(PaxnQ)v (Pa -7Q) @ (@Q v P) a (Av AQ) v (GP A 7Q) by using the distributive law @ (Ov PAT) V (APA 79) by using Jy @ (Ov P)v A(P Vv Q) by using the DeMorgan’s law and fo @ (PV Qv a(P Vv Q) by using the commutative law eT by using Jg Hence the given formula is a tautology. Chapter 1: Propositions and Predicates mM 29 EXAMPLE 1.31 Test the validity of the following argument: If I get the notes and study well, then I will get first class. I didn’t get first class. So either I didn’t get the notes or I didn’t study well. Solution Let P denote ‘I get the notes’. Let @ denote ‘I study well’. Let R denote ‘I will get first class.” Let § denote ‘I didn’t get first class.” The given premises are: G) PAQ>R Gi) aR The conclusion is —P v —Q. 1 PAQSR Premise (i) 2.4R Premise (ii) 3. a(P a Q) Lines 1, 2 and modus tollens. 4.3PVA@ DeMorgan’s law Thus the argument is valid. EXAMPLE 1.32 Explain (a) the conditional proof rule and (b) the indirect proof. Solution (a) If we want to prove A = B, then we take A as a premise and construct a proof of B. This is called the conditional proof rule. It is denoted by CP. (b) To prove a formula a, we construct a proof of ca => F. In particular, to prove A => B, we construct a proof of A, —B > F. E MPLE x Test the validity of the following argument: Babies are illogical. Nobody is despised who can manage a crocodile. Illogical persons are despised. Therefore babies cannot manage crocodiles. 30 & Theory of Computer Science Solution Let B(x) denote ‘x is a baby’. Let I(x) denote ‘x is illogical’. Let D(x) denote ‘x is despised’. Let C(x) denote ‘x can manage crocodiles’. Then the premises are: @) Vx (BG) = Ie) Gi) Vx (CQ) = 3 DQ) (iii) Vx UG) => D(X) The conclusion is Vx (B(x) > — C(x)). 1. Vx (B@) = IG) Premise (i) 2. Wx (C(x) + +D(x)) Premise (ii) 3. Vx (Ix) = Dw) Premise (iii) 4. Bix) = Ia) 1, Universal instantiation 5. C(x) > AD) 2, Universal instantiation 6. (x) => Dex) 3, Universal instantiation 7. BX) Premise of conclusion 8. Kx) 4,7 Modus pollens 9. D(x) 6,8 Modus pollens 10. + C(x) 5,9 Modus tollens ll. B®) = 3 C@) 7,10 Conditional proof 12. Vx (B(x) = =C(x)) ‘UU, Universal generalization. Hence the conclusion is valid. XAMPLE 1.34 Give an indirect proof of GOPS30PVS3S Solution We have to prove S. So we include (iv) +5 as a premise. L PVs Premise (iii) 2 A8 Premise (iv) 3. P 1,2, Disjunctive syllogism 4.P3Q Premise (ii) 5. Q 3,4, Modus ponens 6. 39 Premise (i) 71.0239 5,6, Conjuction 8. F Is We get a contradiction. Hence (1 Q, P = Q, Pv S) => S. Chapter 1: Propositions and Predicates B 31 EXAMPLE 1.35 Test the validity of the following argument: All integers are irrational numbers. Some integers are powers of 2. Therefore, some irrational number is a power of 2. Solution Let Z(x) denote ‘x is an integer’. Let (x) denote ‘x is an irrational number’. Let P(x) denote ‘x is a power of 2°. The premises are: @) Vx (Z@) = Ie) Gi) Fe (Z@) a PQ) The conclusion is Sx (I(x) a P(x). 1. dx (Zx) A P(x) Premise (ii) 2. Z(b) a P(b) 1, Existential instantiation 3. Z(b) 2, Simplification 4. P(b) 2, Simplification 5. Vx (Z@) = IQ)) Premise (i) 6. 2b) => Kb) 5, Universal instantiation 7. Ib) 3,6, Modus ponens 8. 1(b) A P(b) 7,4 Conjunction 9. Ax (Mx) « PQ) 8, Existential instantiation. Hence the argument is valid. SELF-TEST Choose the correct answer to Questions 1-5: 1. The following sentence is not a proposition. (a) George Bush is the President of India. (b) Jo7 is a real number. (c) Mathematics is a difficult subject. (d) I wish you all the best. 2. The following is a well-formed formula. (a) P AQ) => (Pv Q) (b) (PA Q) => (Pv QAR) (c) PaA@AR)) = (PA Q)) (d) 7(@ a 7(P v 7Q) 32_& Theory of Computer Science 3. * 1. PAQ is called: (a) Addition (b) Conjunction (c) Simplification (d) Modus tollens Modus ponens is (a) 5@ P>Q oP (b) =P Pv@ Q (c) P P=>@Q Q (d) none of the above aPAAQ a Ris a minterm of: (a) Pv @ (bl) nPAAQAR (c) PAQARAS (d) PAR Find the truth value of P => @Q if the truth values of P and Q are F and T respectively. For what truth values of P, Q and R, the truth value of (P > Q) > R is F? (P, Q, R have the wuth values F, T, F or F, T, T) If P, Q, R have the truth values F, T, F, respectively, find the truth value of (P > Q) v (P = R). State universal generalization. }. State existential instantiation. EXERCISES Which of the following sentences are propositions? (a) A triangle has three sides. (b) LILI is a prime number. (c) Every dog is an animal. Chapter 1: Propositions and Predicates S33 (d) Ram ran home. (e) An even number is a prime number. (f) 10 is a root of the equation x7 - 1002x + 10000 = 0 (g) Go home and take rest. 1.2 Express the following sentence in symbolic form: For any two numbers a and b, only one of the following holds: a < b, a = b, and a> db. 1.3 The truth table of a connective called Exclusive OR (denoted by ¥) is 14 15 16 18 19 1.10 111 shown in Table 1.18. TABLE 1.18 Truth Table for Exclusive OR Pv¥Q DNA! D marnalo mas < Give an example of a sentence in English (i) in which Exclusive OR is used, (ii) in which OR is used. Show that V is associative, commutative and distributive over a. Find two connectives, using which any other connective can be described. The connective NAND denoted by 7 (also called the Sheffer stroke) is defined as follows: P T Q@ = (P « Q). Show that every connective can be expressed in terms of NAND. The connective NOR denoted by J (also called the Peirce arrow) is defined as follows: P 1 Q = —(P v Q). Show that every connective can be expressed in terms of NOR. Construct the truth table for the following: (a) (Pv Q) = (Pv R) = (Rv Q)) (b) (P v (Q = R)) & (Pv AR) = Q) Prove the following equivalences: @ GAPSCGPS>(APAQ)=FPVE (b) P= (Pv Q)a (Pv AQ) () A(P & QO) = (PATO) V (APAQ) Prove the logical identities given in Table 1.11 using truth tables. Show that P = (Q > (R > (AP => (AQ => —R))) is a tautology. Is (P => 4 P) > —P (i) a tautology, (ii) a contradiction, (iii) neither a tautology nor a contradiction? 34 Q Theory of Computer Science 1.12 Is the implication (P - (P => — Q)) v (Q > = Q) > 4 Q a tautology? 1.13 Obtain the principal disjunctive normal form of the following: (fa) P= (P= Qa G(R v -P))) &b) @anRaaAS\v (Ra S). 1.14 Simplify the formula whose principal disjunctive normal form is 110 v 100 v 010 v 000. 1.15 Test the validity of the following arguments: 1.16 1.17 1.18 @ P=@ R>-Q “P=>AR (b) R> AQ P=@ aR>S PSs () P Q aQ>R Q=> aR OR (d) P>QAR OQvS=T SvP Test the validity of the following argument: If Ram is clever then Prem is well-behaved. If Joe is good then Sam is bad and Prem is not well-behaved. If Lal is educated then Joe is good or Ram is clever. Hence if Lal is educated and Prem is not well-behaved then Sam is bad. A company called for applications from candidates, and stipulated the following conditions: (a) The applicant should be a graduate. (b) If he knows Java he should know C++. (c) If he knows Visual Basic he should know Java. (d) The applicant should know Visual Basic. Can you simplify the above conditions? For what universe of discourse the proposition Vx (x 2 5) is true? Chapter 1: Propositions and Predicates B35 1.19 1.20 1.21 1.22 By constructing a suitable universe of discourse, show that Ax (P@) = Q(x) @ Ax P@) = 3rQ@) is not valid. Show that the following argument is valid: All men are mortal. Socrates is a man. So Socrates is mortal. Is the following sentence true? If philosophers are not money-minded and some money-minded persons are not clever, then there are some persons who are neither philosphers nor clever. Test the validity of the following argument: No person except the uneducated are proud of their wealth. Some persons who are proud of their wealth do not help others. Therefore, some uneducated persons cannot help others. Mathematical Preliminaries In this chapter we introduce the concepts of set theory and graph theory. Also, we define strings and discuss the properties of strings and operations on strings. In the final section we deal with the principle of induction, which will be used for proving many theorems throughout the book. 2.1 SETS, RELATIONS AND FUNCTIONS 2.1.1 SETS AND SUBSETS A set is a well-defined collection of objects, for example, the set of all students in a college. Similarly, the collection of all books in a college library is also a set. The individual objects are called members or elements of the set. We use the capital letters A, B, C, ... for denoting sets. The small letters a, b, c, ... ate used to denote the elements of any set. When a is an element of the set A, we write a € A. When a is not an element of A, we write a ¢ A. Various Ways of Describing a Set (i) By listing its elements. We write all the elements of the set (without repetition) and enclose them within braces. We can write the elements in any order. For example, the set of all positive integers divisible by 15 and less than 100 can be written as {15, 30, 45, 60, 75, 90}. (ii) By describing the properties of the elements of the set. For example, the set {15, 30, 45, 60, 75, 90} can be described as: {n | 2 is a positive integer divisible by 15 and less than 100}. (The description of the property is called predicate. In this case the set is said to be implicitly specified.) 36 Chapter 2: Mathematical Preliminaries 37 (iii) By recursion. We define the elements of the set by a computational rule for calculating the elements. For example, the set of all natural numbers leaving a remainder 1 when divided by 3 can be described as {an a, = 1, dur = ay + 3} When the computational rule is clear from the context, we simply specify the set by some initial elements. The previous set can be written as (1, 4, 7, 10, ...}. The four elements given suggest that the computational rule is: Subsets and Operations on Sets A set A is said to be a subset of B (written as A B) if every element of A is also an element of B. Two sets A and B are equal (we write A = B) if their members are the same. In practice, to prove that A = B, we prove AC B and BCA. A set with no element is called an empty set, also called a null set or a void set, and is denoted by @ We define some operations on sets. AU B= {x|x € A or x © B}, called the union of A and B. A B= {x|x € A and x € B}, called the intersection of A and B. A- B= {x|x A and x ¢ B), called the complement of B in A. A® denotes U - A, where U is the universal set, the set of all elements under consideration. The set of all subsets of a set A is called the power set of A. It is denoted by 24. Let A and B be two sets. Then A x B is defined as {(a, b)|a € A and b € B}. ((@, 6) is called an ordered pair and is different from (b, a).) Definition 2.1 Let S be a set. A collection (A;, A2,...,.A,) of subsets of S is ” called a partition if A; 0 A; = OG #f) and S = U A; (ie. AV U ADU... ia U A,). For example, if § = {1, 2, 3, ..., 10}, then {{1, 3, 5, 7, 9}, (2, 4, 6 8, 10}} is a partition of S. 2.1.2 SETS WITH ONE BINARY OPERATION A binary operation * on a set S is a rule which assigns, to every ordered pair (a, b) of elements from S, a unique element denoted by a * b. Addition, for example, is a binary operation on the set Z of all integers. (Throughout this book, Z denotes the set of all integers.) Union is a binary operation on 2", where A is any nonempty set. We give below five postulates on binary operations. Postulate 1: Closure. If a and b are in S, then a * b is in S. 38 Theory of Computer Science Postulate Postulate Associativity. If a, b, c are in S, then (a+ b) +c = a+ (b+o). Identity element. There exists a unique element (called the identity element) e in S such that for any element x in S, xeenerxen Postulate 4: Inverse, For every element x in S there exists a unique element x” in S such that x + x’ = x’ + x = e. The element .’ is called the inverse of x w.r.t. +. Postulate 5: Commutativity. If a, b € S, then a+b = b+a. It may be noted that a binary operation may satisfy none of the above five postulates. For example, let S = {1, 2, 3, 4,...}, and let the binary operation be subtraction (i.e. a * b = b). The closure postulate is not satisfied since 2-3=-1¢ S. Also, (2 - 3) - 4 #2 - (3 — 4), and so associativity is not satisfied. As we cannot find a positive integer such that x - e = e —x =.x, the postulates 3 and 4 are not satisfied. Obviously, a — 6 # b — a. Therefore, commutativity is not satisfied. Our interest lies in sets with a binary operation satisfying the postulates. Definitions (i) A set S with a binary operation + is called a semigroup if the postulates | and 2 are satisfied. (ii) A set S with a binary operation + is called a monoid if the postulates 1-3 are satisfied. (iii) A set S with + is called a group if the postulates 1-4 are satisfied. (iv) A semigroup (monoid or group) is called a commutative or an abelian semigroup (monoid or group) if the postulate 5 is satisfied. Figure 2.1 gives the relationship between semigroups, monoids, groups, etc. where the numbers refer to the postulate number. Set No operation I Postulates 1, 2 ‘Semigroup wo, 3 ‘Abelian semigroup Monoid 5 3. YS Abelian monoid Group 4 5 tif ‘Abelian group Fig. 2.4. Sets with one binary operation. We interpret Fig. 2.1 as follows: A monoid satisfying postulate 4 is a group. A group satisfying postulate 5 is an abelian group, ete. Chapter 2: Mathematical Preliminaries B 39 We give below a few examples of sets with one binary operation: (i) Z with addition is an abelian group. i) Z with multiplication is an abelian monoid. (It is not a group since it does not satisfy the postulate 4.) (iii) {1, 2, 3, ...} with addition is a commutative semigroup but not a monoid. (The identity element can be only 0, but 0 is not in the set.) (iv) The power set 24 of A(A # 9) with union is a commutative monoid. (The identity element is 9.) (v) The set of all 2 x 2 matrices under multiplication is a monoid but not an abelian monoid. 2.1.3 SETS WITH TWO BINARY OPERATIONS Sometimes we come across sets with two binary operations defined on them (for example, in the case of numbers we have addition and multiplication). Let S be a set with two binary operations + and 0. We give below 11 postulates in the following way: (i) Postulates 1-5 refer to + postulates. (ii) Postulates 6, 7, 8, 10 are simply the postulates 1, 2, 3, 5 for the binary operation 0. (iii) Posmulate 9: If S under « satisfies the postulates 1-5 then for every x in S, with x # e, there exists a unique element x in S such that x’ o x = x0 x =, where e” is the identity element corresponding to o. (iv) Postulate 11: Distributivity. For a, b, c, in S ao (b+c)=(a0b)* (aoc) A set with one or more binary operations is called an algebraic system. For example, groups, monoids, semigroups are algebraic systems with one binary operation. We now define some algebraic systems with two binary operations. Definitions (i) A set with two binary operations « and o is called a ring if (a) it is an abelian group w.r.t. +, and (b) © satisfies the closure, associativity and distributivity postulates (i.e. postulates 6, 7 and 11). Gi) A ring is called a commutative ring if the commutativity postulate is satisfied for o. (iii) A commutative ring with unity is a commutative ring that satisfies the identity postulate (i.e. postulate 8) for o. (iv) A field is a set with two binary operations + and o if it satisfies the postulates 1-11. We now give below a few examples of sets with two binary operations: (i) Z with addition and multiplication (in place of + and 0) is a commutative ring with identity. (The identity element w.r.t. addition is ©, and the identity element w.r.t. multiplication is 1.) 40 i Theory of Computer Science (ii) The set of all rational numbers (i.e. fractions which are of the form a/b, where a is any integer and b is an integer different from zero) is a field. (The identity element w.r.t. multiplication is 1. The inverse of a/b, alb # 0 is bia.) (iii) The set of all 2 x 2 matrices with matrix addition and matrix multiplication is a ring with identity, but not a field. (iv) The power set 2 (A ¥ ®) is also a set with two binary operations U and m. The postulates satisfied by U and - are 1, 2, 3, 5, 6, 7, 8, 10 and 11. The power set 2* is not a group or a ring or a field. But it is an abelian monoid w.r.t. both the operations U and -. Figure 2.2 illustrates the relation between the various algebraic systems we have introduced. The interpretation is as given in Fig. 2.1. The numbers refer to postulates. For example, an abelian group satisfying the postulates 6, 7 and 11 is a ring. Abelian group | 1-5 Jen Ring 10 r 8 ‘Commutative ring Ring with identity 8 10 1 ‘Commutative ring with identity 9 + Field | 1-14 Fig. 2.2 Sets with two binary operations. 2.1.4 RELATIONS The concept of a relation is a basic concept in computer science as well as in real life. This concept arises when we consider a pair of objects and compare one with the other. For example, ‘being the father of’ gives a relation between two persons. We can express the relation by ordered pairs (for instance, ‘a is the father of b’ can be represented by the ordered pair (a, b)). While executing a progra.n, comparisons are made, and based on the result, different tasks are performed. Thus in computer science the concept of relation arises just as in the case of data structures. Definition 2.2 A relation R in a set S is a collection of ordered pairs of elements in S (i.e. a subset of S x S). When (x, y) is in R, we write xRy. When G, y) is not in R, we write xR’y. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 42_& Theory of Computer Science EXAMPLE 2.5 We can define an equivalence relation R on any set S by defining aRb if a= b. (Obviously, a = a for every a. So, R is reflexive. If a = b then b = a. So R is symmetric. Also, if a = b and b = c, then a = c. So R is transitive.) EXAMPLE 2.6 Define a relation R on the set of all persons in New Delhi by ab if the persons a and b have the same date of birth. Then R is an equivalence relation. Let us study this example more carefully. Corresponding to any day of the year (say, 4th February), we can associate the set of all persons born on that day. In this way the set of all persons in New Delhi can be partitioned into 366 subsets. In each of the 366 subsets, any two elements are related. This leads to one more property of equivalence relations. Definition 2.4 Let R be an equivalence relation on a set S. Let a € S. Then C, is defined as {b € S|aRb} The C, is called an equivalence class containing a. In general, the C,’s are called equivalence classes. eae For the congruence modulo 3 relation on {1, 2, .... 7), C= {25}, GQ ={h 47 G= (3, 6} For the equivalence relation ‘having the same birth day’ (discussed in Example 2.6). the set of persons bom on 4th February is an equivalence class, and the number of equivalence classes is 366. Also, we may note that the union of all the 366 equivalence classes is the set of all persons in Delhi. This is true for any equivalence relation because of the following theorem. Theorem 2.1 Any equivalence relation R on a set S partitions S into disjoint equivalence classes. Proof Let U C, denote the union of distinct equivalence classes. We have to prove that: 75 @) S= UG, acs (ii) C, 0 Cy = O if C, and C, are different, ie. C, # Cy Let s € S. Then s € C, (since sRs, R being reflexive). But C, © UCo. ae: So Sc’ U C,. By definition of C,, C, ¢ S for every ain S.So UC, cS. acs aes Thus we have proved (i). Before proving (ii), we may note the following: Cy = Cy — if aRb (2.1) aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 2: Mathematical Preliminaries B49 And v3eqv4es¥> is a path from v3 to v2, We call vzeqvgesv2 a directed path since the edges ¢ and es have the forward direction. (But 1je3y3e3v9 is not a directed path as e, is in the forward direction and e3 is in the backward direction.) Definition 2.15 A graph (directed or undirected) is connected if there is a path between every pair of vertices. The graphs given by Figs. 2.3 and 2.4, for example, are connected. Definition 2.16 A circuit in a graph is an alternating sequence v,e,r2¢> . .. €,.1¥, Of vertices and edges starting and ending in the same vertex such that e; has v; and v;,, as the end vertices and no-edge or vertex other than y is Tepeated. In Fig. 2.3, for example, vzeqvzesy4eqv3, Vieavze4vaesv2e1¥ are circuits. In Fig. 2.4, vjeqv;eqv2ey and v2eqvzeqvgesv2 are circuits. 2.2.2 TREES Definition 2.17 A graph (directed or undirected) is called a tree if it is connected and has no circuits. The graphs given in Figs. 2.6 and 2.7, for example, are trees. The graphs given in Figs. 2.3 and 2.4 are not trees. Note: A directed graph G is a tree iff the corresponding undirected graph is a tree. © @) () Fig. 2.6 A tree with four vertices. Fig, 2.7 ‘A tree with seven vertices. We now discuss some properties of trees (both directed and undirected) used in developing transition systems and studying grammar rules. 50 _& Theory of Computer Science Property 1 A tree is a connected graph with no circuits or loops. Property 2 In a tree there is one and only one path between every pair of vertices. Property 3 If in a graph there is a unique (i.e. one and only one) path between every pair of vertices, then the graph is a tree. Property 4 A tree with n vertices has n — 1 edges. Property 5 If a connected graph with n vertices has n — 1 edges, then it is a tree. Property 6 If a graph with no circuits has n vertices and n — 1 edges, then itis a tree, A leaf in a tree can be defined as a vertex of degree one. The vertices other than leaves are called internal vertices. In Fig. 2.6, for example, vj, v3, v4 are leaves and v3 is an internal vertex. In Fig. 2.7, vy vs, v6 v7 are leaves and v, vz vs are internal vertices. The following definition of ordered trees will be used for representing derivations in context-free grammars. Definition 2.18 An ordered directed tree is a digraph satisfying the following conditions: T,: There is one vertex called the root of the tree which is distinguished from all the other vertices and the root has no predecessors. T>; There is a directed path from the root to every other vertex. T3: Every vertex except the root has exactly one predecessor. Ty: The successors of each vertex are ordered ‘from the left’. Note: The condition 7, of the definition becomes evident once we have the diagram of the graph. Figure 2.7 is an ordered tree with v, as the root. Figure 2.8 also gives an ordered directed tree with v, as the root. In this figure the successors of v, are ordered as v2v3. The successors of v3 are ordered as vsv6. Fig. 2.8 An ordered directed tree. Chapter 2: Mathematical Preliminaries Q 51 By adopting the following convention, we can simplify Fig. 2.8. The root is at the top. The directed edges are represented by arrows pointing downwards. As all the arrows point downwards, the directed edges can be simply represented by lines sloping downwards, as illustrated in Fig. 2.9. Fig. 2.9 Representation of an ordered directed tree. Note: An ordered directed tree is connected (which follows from 7;). It has no circuits (because of T;). Hence an ordered directed wee is a tree (see Definition 2.17). As we use only the ordered directed trees in applications to grammars, we refer to ordered directed trees as simply trees. Definition 2.19 A binary tree is a tree in which the degree of the root is 2 and the remaining vertices are of degree 1 or 3. Note: Ina binary tree any vertex has at most two successors. For example, the trees given by Figs. 2.11 and 2.12 are binary trees. The tree given by Fig. 2.9 is not a binary tree. Theorem 2.5 The number of vertices in a binary tree is odd. Proof Let n be the number of vertices. The root is of degree 2 and the remaining m—1 vertices are of odd degree (by Definition 2.19). By Theorem 2.4, n — 1 is even and hence n is odd. 1 ‘We now introduce some more terminology regarding trees: (i) A son of a vertex v is a successor of v. (ii) The father of v is the predecessor of v. ii) If there is a directed path from v, to v2, v; is called an ancestor of v2, and v2 is called a descendant of v). (Convention: v is an ancestor of itself and also a descendant of itself.) (iv) The number of edges in a path is called the length of the path. (v) The height of a tree is the length of a longest path from the root. For example, for the tree given by Fig. 2.9, the height is 2. (Actually there are three longest paths, v; > v, > vy, Vj > ¥3 > V5, 1 > V2 > VG Each is of length 2.) (vi) A vertex v in a tee is at level & if there is a path of length k from the root to the vertex v (the maximum possible level in a tree is the height of the tree). 52_&_ Theory of Computer Science Figure 2.10, for example, gives a tree where the levels of vertices are indicated. Fig. 2.10 Illustration of levels of vertices. XAMPLE 2.15 For a binary tree T with n vertices, show that the minimum possible height is [loga(m + 1) — 11, where[ k 1] is the smallest integer 2 &, and the maximum possible height is (n — 1)/2. Solution In a binary tree the root is at level 0. As every vertex can have at most two successors, we have at most two vertices at level 1, at most 4 vertices at level 2, etc. So the maximum number of vertices in a binary tree of height k is 14+2+2+4-- 42 As [has n vertices, 1+ 2+ 27+ +--+ Hon, ic. #1 — Di -1) 2 n, so k 2 loga(n + 1) — 1. As & is an integer, the smallest possible value for & is log,(n + 1) — 11. Thus the minimum possible height is [ log,(n + 1) - 11. To get the maximum possible height, we proceed in a similar way. In a binary tree we have the root at zero level and at least two vertices at level 1, 2,.... When Tis of height k, we have at least 1+ 2 +--+ +2 (2 repeated k times) vertices. So, 1 + 2k 9 is the longest path). 2.3 STRINGS AND THEIR PROPERTIES A string over an alphabet set Z is a finite sequence of symbols from 2. NOTATION: %* denotes the set of all strings (including A, the empty string) over the alphabet set E. That is, £* = L* - {A}. 2.3.1 OPERATIONS ON STRINGS The basic operation for strings is the binary concatenation operation. We define this operation as follows: Let x and y be two strings in D*. Let us form a new string < by placing y after x, ice. ay. The string z is said to be obtained by concatenation of x and y. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 2: Mathematical Preliminaries B63 It is given that: |W| = 250, |R| = 260, [Fl =260, |WoF|=90, IWoOR|=110, [ROF[=80, |[WARAF| = 30 IWURUVFP| |Wi+|RI+1FI-|Wa Fl -|WORI[-|IROF/+|WARDF| = 250 + 260 + 260 - 90 - 110 - 80 + 30 = 520 So the solution for (a) is 520. (b) The number of people who read none of the magazines = 600 - 520 = 80 Using the data, we fill up the various regions of the Venn diagram. (c) The number of people who read only one magazine = 80 + 100 + 120 = 300 AMPLE 2.28 Prove that (AU BU OS = (AU BY N (AU CY Solution AUBUC)=(AUAUBUC AVUAUBUCSAUBUAUO Hence (A U BU C)’ = (A U BY (A U CY (by DeMorgan’s law) E v2) Define aRb if b = a‘ for some positive integer k; a, b € z Show that R is a partial ordering. (A relation is a partial ordering if it is reflexive, antisymmetric and transitive.) Solution As a =a, we have aRa. To prove that R is antisymmetric, we have to prove that @Rb and bRa => a = b. As aRb, we have b = at. As bRa, we have a = bi. Hence a = b! = (a)! = a". This is possible only when one of the following holds good: @ asl Gi) a =-1 Gi) A= 1 In case (i), b = a’ = 1. Soa=b. In case (ii), a = -1 and so Al is odd. This implies that both k and / are odd. So b=at=(-1f = a In case (iii), kl = 1. As k and J are positive integers, k = 1 = 1. So =ad=a. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 68 & Theory of Computer Science 2.2 2.3 24 2. te 2.6 2.7 28 29 2.10 (a) A* 0 B* (e) (A ~ B)* (f) (B - Ay* Let S = {a, b}* For x, y € S, define x o y = xy, ie. x 0 y is obtained by concatenating x and y. (a) Is S closed under 0? (b) Is 0 associative? (c) Does S$ have the identity element with respect to 0? (d) Is 0 commutative? Let $ = 2%, where X is any nonempty set. For A, Bc X. let AoB=AUB. (a) Is o commutative and associative? (b) Does S have the identity element with respect to 0? (c) If A 0 B =A oC, does it imply that B = C? Test whether the following statements are true or false. Justify your answer. (a) The set of all odd integers is a monoid under multiplication. (b) The set of all complex numbers is a group under multiplication. (c) The set of all integers under the operation o given by a 0 b = a +b — ab is a monoid. (d) 2° under symmetric difference Y defined by AY B = (A - B) U (B — A) is an abelian group. Show that the following relations are equivalence relations: (a) On a set S, aRb if a = b. (b) On the set of all lines in the plane, [,Rlp if 1, is parallel to by. (c) On N = {0, 1, 2, ...}, mRn if m differs from n by a multiple of 3. Show that the following are not equivalence relations: (a) On a set S, aRb if a # b. (b) On the set of lines in the plane, 1,Rlp if f; is perpendicular to J. (c) On N = {0, 1, 2, ...}, mRn if m divides n. (d) On S = {1, 2,..., 10}, aRb if a+b = 10. For x, y in {a, b}*, define a relation R by xRy if |x| = |y|. Show that R is an equivalence relation. What are the equivalence classes? For x, y in {a, b}*, define a relation R by xRy if x is a substring of y (@ is a substring of y if y = zxz for some string z), z,). Is R an equivalence relation? Let R = {(1, 2), (2, 3), Cl, 4), , 2), (3, 4)}. Find Rt, R*. Find R* for the following relations: @) R= (C1, D, C1, 2), @ D, 2, 3). GB. Dt () R= {G, D, 2 3, GB, 4), 3, 2} Chapter 2: Mathematical Preliminaries fi 69 ©) R= {G, D, @ 2), 3, 3) 4, 4} @) R= (G, 2), 2 3), BD, 4 4} 2.11 If R is an equivalence relation on S, what can you say about R*, R*? 2412 Let f:{a, b}* > {a, b}* be given by f(x) = ax for every x € {a, b}*. Show that f is one-to-one but not onto. 2.13 Let g:{a, b}* > {a, b}* be given by g(x) = x’. Show that g is ‘one-to-one and onto. 2.14 Give an example of (a) a tree with six vertices and (b) a binary tree with seven vertices. 2.15 For the tree T given in Fig. 2.18, answer the following questions: (a) Is 7 a binary tree? (b) Which vertices are the leaves of T? (c) How many internal vertices are in T? (d) What is the height of 7? (e) What is the left-to-right ordering of leaves? (f) Which vertex is the father of 5? (g) Which vertices are the sons of 3? OQ (2 (2) Oo O8 © @ ® Fig. 2.18 The tree for Exercise 2.15. 2.16 In a get-together, show that the number of persons who know an odd number of persons is even. [Hint: Use a graph.] 2.17 If X is a finite set, show that |2*| = 2". 2.18 Prove the following by the principle of induction: (a) ¥ a Mat Dn +) del . 1 n ©) 2 kKk+D Hed (c) 10% — 1 is divisible by 11 for all n > 1. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 3: The Theory of Automata @ 75 In other words, if (gq), w, gz) is in 6, it means that the graph starts at the vertex qj, goes along a set of edges, and reaches the vertex q7. The concatenation of the label of all the edges thus encountered is w. Definition 3.3 A transition system accepts a string w in £* if (@ there exists a path which originates from some initial state, goes along the arrows, and terminates at some final state; and (ii) the path value obtained by concatenation of all edge-labels of the path is equal to w. sou Consider the transition system given in Fig. 3.6. V0 Fig. 3.6 Transition system for Example 3.2. Determine the initial states, the final states, and the acceptability of 101011, 111010. Solution The initial states are gq and qj. There is only one final state, namely 93 The path-value of qoqo924s is 101011. As 3 is the final state, 101011 is accepted by the transition system. But, 111010 is not accepted by the transition system as there is no path with path value 111010. Note: Every finite automaton (Q, Z, § qo, F) can be viewed as a transition system (Q, E, 5° Qo, F) if we take Qo = {qo} and 6’= {(@, w, 6(g, w)|q € Q, w € &*}. But a wansition system need not be a finite automaton. For example, a transition system may contain more than one initial state. 3.4 PROPERTIES OF TRANSITION FUNCTIONS Property 1 5(q, A) = ¢ is a finite automaton. This means that the state of the system can be changed only by an input symbol. 76 fa _Theory of Computer Science Property 2 For all strings w and input symbols a, 5, aw)= 5(5(q, a), w) 5(q, wa) = 5(6(q, w), a) This property gives the state after the automaton consumes or reads the first symbol of a string aw and the state after the automaton consumes a prefix of the string wa. ON eee Prove that for any transition function 5 and for any two input strings x and y, 6, xy) = 6(6@, »), ¥) G.I) Proof By the method of induction on | y|, ie. length of y. Basis: When |y|= 1, y=aeE LHS. of (3.1) = 5(q, xa) = 6(6(q, x), a) by Property 2 = RHS. of (3.1) Assume the result, ic. (3.1) for all strings x and strings y with | y| =n. Let y be a string of length n + 1. Write y = ya where |») |= 7. LHS. of (3.1)= 5@ xa) = 6g, xa), x = 2) = 6(6@, »), @) by Property 2 = XG x), = &&5q, x), y:), @) — by induction hypothesis RHS. of (3.1)= KKq », ya) = HASG, x), y:), 4) by Property 2 * Hence, L.H.S. = R.H.S. This proves (3.1) for any string y of length n + 1. By the principle of induction, (3.1) is true for all strings. I EX eee Prove that if &(q, x) = d(q, y), then (g, xz) = 5(g, yz) for all strings z in Z*. Solution 5g, x2) = 5(5(G, »), 2 by Example 3.3 = 65g»), 9 @.2) By Example 3.3, 5G, ye) = H5G, y), D = &q, x2) @.3) aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 3: The Theory of Automata A 79 Fig. 3.8 Transition system for a nondeterministic automaton. 1 oa 9 —2—+ 99 —2? ~~ a CONN % % qs 9% \ % Fig. 3.9 States reached while processing 0100. 0, Definition 3.6 A string w € E* is accepted by NDFA M if 5(qo, w) contains some final state. Note: As M is nondeterministic, 6(q), w) may have more than one state. So w is accepted by M if a final state is one among the possible states that M can reach on application of w. We can visualize the working of an NDFA M as follows: Suppose M reaches a state g and reads an input symbol a. If d(g, a) has n elements, the automaton splits into n identical copies of itself; each copy pursuing one choice determined by an element of 5(q, a). This type of parallel computation continues. When a copy encounters (q, a) for which 5(q, a) = 9, this copy of the machine ‘dies’; however the computation is pursued by the other copies. If any one of the copies of M reaches a final state after processing the entire input string w, then we say that M accepts w. Another way of looking at the computation by an NDFA M is to assign a tree structure for computing 6(q, w). The root of the tree has the label g. For every input symbol in w, the tree branches itself. When a leaf of the tree has a final state as its label, then M accepts w. Definition 3.7 The set accepted by an automaton M (deterministic or nondeterministic) is the set of all input strings accepted by M. It is denoted by 1M). 80 & Theory of Computer Science 3.7 THE EQUIVALENCE OF DFA AND NDFA We naturally try to find the relation between DFA and NDFA. Intuitively we now feel that: (i) A DFA can simulate the behaviour of NDFA by increasing the number of states. (In other words, a DFA (Q, £, 6, go, F) can be viewed as an NDFA (Q, %, 4, qo. F) by defining 5g, a) = (5, a)}.) (ii) Any NDFA is a more general machine without being more powerful. We now give a theorem on equivalence of DFA and NDFA. Theorem 3.1 For every NDFA, there exists a DFA which simulates the behaviour of NDFA. Alternatively, if L is the set accepted by NDFA, then there exists a DFA which also accepts L. Proof Let M=(Q, 5, & qy F) be an NDFA accepting L. We construct a DFA M’as: M=(Q,% 6 q F’) where (i) Q = 22 (any state in Q’ is denoted by [g) ga, «+ +1 qh, where gi, q2, so GE Os Gi) ¢’o = [go]: and (iii) F’ is the set of all subsets of Q containing an element of F. Before defining 5% let us look at the construction of Q’, qo and F’. M is initially at qo. But on application of an input symbol, say a, M can reach any of the states 5(gq, a). To describe M, just after the application of the input symbol a, we require all the possible states that M can reach after the application of a. So, M’has to remember all these possible states at any instant of time. Hence the states of M’ are defined as subsets of Q. As M starts with the initial state qo, q’o is defined as [qo]. A string w belongs to 7(M) if a final state is one of the possible states that M reaches on processing w. So, a final state in M’ (i.e. an element of F’) is any subset of Q containing some final state of M. Now we can define 5% iv) Sqn da ++ GL ® = 6, a V 6@, aU... U 6G, a. Equivalently, Og. 9 + Gh @ = Dy +» Bl if and only if Sqn ++ Gils @) = (Py Pr» ++» Bib Before proving L = T(M’), we prove an auxiliary result 5@o, *) = lau. +--+ dh G4) if and only if 5(qp, x) = (q, ..-. 9;) for all x in E*. We prove by induction on |x|, the ‘if part, i.e. So 2) = (an a2 + Gd 3.5) if Bo, ) = {ay -+5 aid Chapter 3: The Theory of Automata 81 When |x| = 0, 5(q, A) = {qo}, and by definition of 5% 5/(q4 A) = 96 = [gol- So, (3.5) is true for x with |x| = 0. Thus there is basis for induction. Assume that (3.5) is true for all strings y with |y| < m. Let x be a string of length m + 1. We can write x as ya, where |y| =m and ae ©. Let 5(qo, y) = {P1, «+++ pj} and S(qo, ya) = {ry ry «+5 me}. As |y| Sm, by induction hypothesis we have 5146, 9) = Pr --» Pil (3.6) Also, {rp ry te} = (qo ya) = 5(5o y), 2) = Spy -- + Ph a By definition of 5; (py... BL a) = In, .s rd 3.7) Hence, 5's, ya) = 5(5q'0, y), @) = Spy -- 5 pj @ by (3.6) = In... nd by (3.7) Thus we have proved (3.5) for x = ya. By induction, (3.5) is true for all strings x. The other part (.e. the ‘only if part), can be proved similarly, and so (3.4) is established. Now, x € 7(M) if and only if 5(g, x) contains a state of F. By (3.4), &(qo, x) contains a state of F if and only if 5%(q’p 2) is in F’. Hence, x € T(M) if and only if x € T(M’). This proves that DFA M’ accepts L. 1 Note: In the construction of a deterministic finite automaton M, equivalent to a given nondeterministic automaton M, the only difficult part is the construction of 6” for M,. By definition, k (ig --- ahs a = USC a) So we have to apply 6 to (q;, a) for each i = 1, 2,..., & and take their union to get 5’([q, -.. ql a). When 6 for M is given in terms of a state table, the construction is simpler. 5(q;, a) is given by the row corresponding to q; and the column corresponding to a. To construct 5‘([q ..- 9], 2), consider the states appearing in the rows corresponding to qj, .. ., 4, and the column corresponding to a. These states constitute 5’([q; ... qJ, a). Note: We write 5’ as 6 itself when there is no ambiguity. We also mark the initial state with — and the final state with a circle in the state table. ce 3 Construct a deterministic automaton equivalent to M = (qo. a1}. (0. 1}, 5 go. {g0}) where 6 is defined by its state table (see Table 3.2). aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 3: The Theory of Automata B85 TABLE 3.8 A Moore Machine Present state Next state 5 Output a=0 a= a 4% & a 0 qn a R 1 a ® eB oO os ® Go oO For the input string 0111, the transition of states is given by gp > 93 > Jo — 4; > 42 The output string is 00010. For the input string A, the output is A(qo) = 0. Transition Table 3.9 describes a Mealy machine. TABLE 3.9 A Mealy Machine Present state Next state a=0 ‘state ‘output ‘state ‘output ah % oO b 0 a un 1 % 0 % ® 1 n 1 Ga qs, 1 % 0 Note: For the input string 0011, the transition of states is given by g; > 93 = 92 44 43, and the output string is 0100. In the case of a Mealy machine, we get an output only on the application of an input symbol. So for the input string A, the output is only A. It may be observed that in the case of a Moore machine, we get A(qo) for the input string A. Remark A finite automaton can be converted into a Moore machine by introducing A = {0, 1} and defining A(q) = 1 if q € F and Aq) = 0 if qé@F. For a Moore machine if the input string is of length n, the output string is of length n + 1. The first output is A(qo) for all output strings. In the-case of a Mealy machine if the input string is of length n, the output string is also of the same length n. 3.8.2 PROCEDURE FOR TRANSFORMING A MEALY MACHINE INTO A MooRE MACHINE We develop procedures for transforming a Mealy machine into a Moore machine and vice versa so that for a given input string the output strings are the same (except for the first symbol) in both the machines. 86 Theory of Computer Science EXAMPLE 3. Consider the Mealy machine described by the transition table given by Table 3.10. Construct a Moore machine which is equivalent to the Mealy machine. TABLE 3.10 Mealy Machine of Example 3.9 Present siate Next state Input a = 0 Input a = 4 ‘state outpil ‘Sale_—___ output 7h % ° qe 0 a a 1 % 0 % % 1 an 1 % % 1 % 0 Solution At the first stage we develop the procedure so that both machines accept exactly the same set of input sequences. We look into the next state column for any state, say q; and determine the number of different outputs associated with q; in that column. We split q; into several different states, the number of such states being equal to the number of different outputs associated with q; For example, in this problem, gj is associated with one output 1 and q) is associated with two different outputs 0 and 1. Similarly, q3 and g, are associated with the outputs 0 and 0, 1, respectively. So, we split q2 into qz9 and qy,. Similarly, qq is split into qap and gq). Now Table 3.10 can be reconstructed for the new states as given by Table 3.11. TABLE 3.11 State Table for Example 3.9 Present state Next state Input Input ‘state state output 7% % 0 Go 0 Go a 1 dao oO qa a 1 G40 0 ® a 1 un 1 qa, qa 1 % 0 Ge Ge 1 cA 0 The pair of states and outputs in the next state column can be rearranged as given by Table 3.12. Chapter 3: The Theory of Automata & 87 TABLE 3.12 Revised State Table for Example 3.9 Present state Next state Output a=0 ant on % Go 1 0 a G0 oO Ge a 0 1 % 7a % 0 Go us % 0 Ga a qs. 4 Table 3.12 gives the Moore machine. Here we observe that the initial state 4 is associated with output 1. This means that with input A we get an output of 1, if the machine starts at state q,. Thus this Moore machine accepts a zero- length sequence (null sequence) which is not accepted by the Mealy machine. To overcome this situation, either we must neglect the response of a Moore machine to input A, or we must add a new starting state qo, whose state transitions are identical with those of q, but whose output is 0. So Table 3.12 is transformed to Table 3.13. TABLE 3.13 Moore Machine of Example 3.9 Present state Next state Output 7% % eo 0 n % 0 1 to a C7 ° cy a do 1 % cy n o 0 8 % 0 Gas Ws cA 1 From the foregoing procedure it is clear that if we have an m-output, n- state Mealy machine, the corresponding m-output Moore machine has no more than mn + 1 states. 3.8.3 PROCEDURE FOR TRANSFORMING A MOORE MACHINE INTO A MEALY MACHINE We modify the acceptability of input string by a Moore machine by neglecting the response of the Moore machine to input A. We thus define that Mealy Machine M and Moore Machine M’ are equivalent if for all input strings w, bZyAw) = Zy(w), where b is the output of the Moore machine for its initial state. We give the following result: Let M, = (Q, Z, A, 6 A, qo) be a Moore machine. Then the following procedure may be adopted to construct an equivalent Mealy machine M3. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 3: The Theory of Automata & 97 where Q = {Ia3h [40 gol lai. 9s). (42. Ga). Card} % = (40 ab =F’ = [asl and 6’ is defined by Table 3.24. TABLE 3.24 Transition Table of Minimum State Automaton for Example 3.14 StatelE a b [ap. 5] (a1, 9s] [ao, 9s] Ia, 9s] {90 de] [9 aa) Ig, ad (9s) Ia, 9s} 9a (9s) (qo. 9s) tal (90. 96) [as] Note: The transition diagram for M’ is given by Fig. 3.15. Fig. 3.15 Minimum state automaton of Example 3.14. 3.10 SUPPLEMENTARY EXAMPLES aa e) Construct a DFA equivalent to the NDFA M whose transition diagram is given by Fig. 3.16. Fig. 3.16 NDFA of Example 3.15. 98 GQ Theory of Computer Science Solution The transition table of M is given by Table 3.25. TABLE 3.25 Transition Table for Example 3.15 State a b 7% % 9. a % %, 9% % % - @® % - — = For the equivalent DFA: (i) The states are subsets of O = {go, 41. 92s Gas Ga}- (ii) [go] is the initial state. (iii) The subsets of Q containing q3 or q, are the final states. (iv) dis defined by Table 3.26. We start from [qo] and construct 6, only for those states reachable from [q] (as in Example 3.8). TABLE 3.26 Transition Table of DFA for Example 3.15 State a b (aol [a0] [qo 92 ao. a] lao. ga) [a0 aa) (qo, aa) [a0 [90.1 > Cd a oa) Construct a DFA equivalent to an NDFA whose transition table is defined by Table 3.27. TABLE 3.27 Transition Table of NDFA for Example 3.16 State a b MW Ws a. Ass % % % n Solution Let M be the DFA defined by M = (2!% % 4 45), fa, bY, 6 [qoh F) Chapter 3: The Theory of Automata & 99 where F is the set of all subsets of {qo, 4), 92, 93) containing g3. 6 is defined by Table 3.28. TABLE 3.28 Transition Table of DFA for Example 3.16 State a b (ao) las, as} a2, aa] lar, sl fal lal (al fal [asl lal 6 4 [a2. as) fas] fa] (al la feel a 6 a EXAMPLE 3.17 Construct a DFA accepting all strings w over {0, 1} such that the number of 1’s in w is 3 mod 4. Solution Let M be the required NDFA. As the condition on strings of 7(M) does not at all involve 0, we can assume that M does not change state on input 0. If 1 appears in w (4k + 3) times, M can come back to the initial state, after reading 4 1's and to a final state after reading 3 1's. The required DFA is given by Fig. 3.17. Fig. 3.17 OFA of Example 3.17. EXAMPLE 3.18 Construct 2 DFA accepting all strings over {a, b} ending in ab. Solution We require two transitions for accepting the string ab. If the symbol 6 is processed after aa or ba, then also we end in ab. So we can have states for aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 116 _& Theory of Computer Science EXAMPLE Construct a grammar G generating {a"b'c"|n > 1} W Solution Let L = (a"b"c"| n > 1}. We try to construct L by recursion. We already know how to construct a"b" recursively. As it is difficult to construct abc” recursively, we do it in two stages: (i) we construct aa" and (ii) we convert a” into b’c". For stage (i), we can have the following productions S > aSo|aa A natural choice for @ (to execute stage (ii)) is bc. But converting (bc)” into b’c” is not possible as (bc)" has no variables. So we can take @ = BC, where B and C are variables. To bring B’s together we introduce CB — BC. We introduce some more productions to convert B’s into b’s and C's into c’s. So we define G as G= (CS, B, Ch, {a, b, ch, P, S) where P consists of S — aSBC| aBC, CB > BC, aB > ab, bB > bb, bC > be, eC > ce S = aBC = abC = abc Thus, abe € L(G) Also, S => a 'S(BCy" by applying S > aSBc (n — 1) times = a™aBC(BC)"' _ by applying S > aBC => a" B"C" by applying CB > BC several times (since CB > BC interchanges B and C) => a™abB™'c" by applying aB —> ab once => alpic" by applying bB > bb _ several times = ab bcc by applying bC > be once => a'btc! by applying cC — ce several times Therefore, L(G) & {a'b"c"|n 2 Ib To show that {a"b"c"| n= 1} ¢ L(G), it is enough to prove that the only way to arrive at a terminal string is to proceed as above in deriving a"b"c" (2D. To start with, we have to apply only S-production. If we apply S > aBC, first we get abc. Otherwise we have to apply S > aSBC once or several times and get the sentential form a’"!S(BC)"". At this stage the only production we can apply is S aBC, and the resulting string is a"(BC)". aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 120 & Theory of Computer Science To prove (iii), consider any string w of length 2n. Then it is of the form Gay +. Gy, «a, involving n ‘parameters’ ay, dz... -, dy Each a; can be either a or b. So the number of such strings is 2". This proves (iii). 4.2 CHOMSKY CLASSIFICATION OF LANGUAGES In the definition of a grammar (Vy, 5, P, S), Vy and are the sets of symbols and S € Vy. So if we want to classify grammars, we have to do it only by considering the form of productions. Chomsky classified the grammars into four types in terms of productions (types 0-3). A type O grammar is any phrase structure grammar without any restrictions. (All the grammars we have considered are type 0 grammars.) To define the other types of grammars, we need a definition. In a production of the form @ Ay > dary, where A is a variable, @ is called the left context, y the right context, and gay the replacement string. EXAMPLE 4.15 (a) In abAbcd — abABbcd, ab is the left context, bed is the right context, a = AB. (b) In AC > A, A is the left context, A is the right context. a = A. The production simply erases C when the left context is A and the right context is A. (c) For C > A, the left and right contexts are A. And a = A. The production simply erases C in any context. A production without any restrictions is called a type 0 production. A production of the form ¢Ay > gary is called a type 1 production if a@# A. In type 1 productions, erasing of A is not permitted. EXAMPLE 4.16 (a) aAbcD — abcDbcD is a type 1 production where where a, bcD are the left context and right context, respectively. A is replaced by bcD # A. (b) AB — AbBc is a type 1 production. The left context is A, the right context is A. (c) A — aba is a type 1 production. Here both the left and right contexts are A. Definition 4.7 A grammar is called type 1 or context-sensitive or context- dependent if all its productions are type | productions. The production S > A is also allowed in a type 1 grammar, but in this case S does not appear on the right-hand side of any production. Definition 4.8 The language generated by a type 1 grammar is called a type | or context-sensitive language. Chapter 4: Formal Languages B 121 Note: In a context-sensitive grammar G, we allow $ > A for including A in L(G). Apart from S$ — A, all the other productions do not decrease the length of the working string. A type | production ¢Ay — goxy does not increase the length of the working string. In other words, | ¢Ay| < | daw| as a # A. But if a— B is a production such that | @| < | 8], then it need not be a type 1 production. For example, BC —> CB is not of type 1. We prove that such productions can be replaced by a set of type | productions (Theorem 4.2). Theorem 4.1 Let G be a type 0 grammar. Then we can find an equivalent grammar G, in which each production is either of the form a > 8, where a and f are strings of variables only, or of the form A — a, where A is a variable and a is a terminal. G, is of type 1, type 2 or type 3 according as G is of type 1, type 2 or type 3. Proof We construct G, as follows: For constructing productions of G,, consider a production a — f in G, where @ or 8 has some terminals. In both @ and B we replace every terminal by a new variable C, and get a’ and ’. Thus, corresponding to every a — f, where o or B contains some terminal, we construct a’ — f’ and productions of the form C, — a for every terminal a appearing in @ or B. The construction is performed for every such a — $. The productions for G, are the new productions we have obtained through the above construction. For G, the variables are the variables of G together with the new variables (of the form C,). The terminals and the start symbol of G, are those of G. G; satisfies the required conditions and is equivalent to G. So L(G) = UG). I Definition 4.9 A grammar G = (Vy, 3, P, S) is monotonic (or length- increasing) if every production in P is of the form a — B with | a| < |B] or S—> A. In the second case, S$ does not appear on the right-hand side of any production in P. Theorem 4.2. Every monotonic grammar G is equivalent to a type 1 grammar. Proof We apply Theorem 4.1 to get an equivalent grammar G,. We construct G’ equivalent to grammar G, as follows: Consider a production AjAp ... Am B,B; ... B, with n > m in G). If m = 1, then the above production is of type 1 (with left and right contexts being A). Suppose m 2 2. Corresponding to A}A) ... Am — B,Bz ... B,, we construct the following type 1 productions introducing the new variables C), Cy, .. «5 Cue Ay Ad -6+Am 9 Cra s+ Am CyAge An > CiO2A3 0. Am C1Cp Ag ccs A > CCC Ag oo Ames os Ca oes Cut An CCr oo CoBmar Bar oo By 122 _ Theory of Computer Science CLs... CpBmat + By > BCy --. CBs «+ By BiCrCy... By, > BBC... B BBy woe CyBigaess By PD ByBy ses The above construction can be explained as follows. The production A\Az «Ay > BiB... By is not of type 1 as we replace more than one symbol on L.HLS. In the chain of productions we have constructed, we'replace A, by Cy, Ay by Cy..., An by CuBnai--- By Afterwards, we start replacing C, by By, C, by By, etc. As we replace only one variable at a time, these productions are of type 1. We repeat the construction for every production in G, which is not of type 1. For the new grammar G’, the variables are the variables of G, together with the new variables. The productions of G’ are the new type 1 productions obtained through the above construction. The terminals and the start symbol of G’ are those of G}. G’ is context-sensitive and from the construction it is easy to see that LG) = L(G,) = L(G). 1 Definition 4.10 A type 2 production is a production of the form A > a, where A € Vy and a € (Vy U Z)*. In other words, the L.H.S. has no left context or right context. For example, S > Aa, A > a, B > abc, A > Aare type 2 productions. Definition 4.11 A grammar is called a type 2 grammar if it contains only type 2 productions. It is also called a context-free grammar (as A can be replaced by ain any context). A language generated by a context-free grammar is called a type 2 language or a context-free language. Definition 4.12 A production of the form A — a or A > aB, where A, B & Vy and ae ¥, is called a type 3 production. Definition 4.13 A grammar is called a type 3 or regular grammar if alll its productions are type 3 productions. A production S — A is allowed in type 3 grammar, but in this case S does not appear on the right-hand side of any production. ede Find the highest type number which can be applied to the following productions: (a) S$ Aq A c|Ba, Babe (b) $+ ASB|d, =A aA (c) S > aS| ab aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 5: Regular Sets and Regular Grammars & 139 hy @ + QO = P*Q*y* = P* + QH* hy (@+QR=PR+QR and RP+Q)=RP+RQ Note: By the ‘set P’ we mean the set represented by the regular expression P. The following theorem is very much useful in simplifying regular expressions (i.e. replacing a given regular expression P by a simpler regular expression equivalent to P). Theorem 5.1 (Arden’s theorem) Let P and Q be two regular expressions over 2. If P does not contain A, then the following equation in R, namely R=Q+RP GD has a unique solution (i.e. one and only one solution) given by R = QP*. Proof Q + (QP*)P = Q(A + P*P) = QP* by I, Hence (5.1) is satisfied when R = QP*. This means R = QP* is a solution of (5.1). To prove uniqueness, consider (5.1). Here, replacing R by Q + RP on the R.HLS., we get the equation Q+RP=Q+(Q+ RPP =Q+ QP + RPP =Q+ QP + RP Q+QP + QP+ =QAt+P+ P+ + QP! + RP#! P) + RP“! From (5.1), R=QA+P+P4+---+ Pi) + RPM fori 20 (5.2) We now show that any solution of (5.1) is equivalent to QP*. Suppose R satisfies (5.1), then it satisfies (5.2). Let w be a string of length i in the set R. Then w belongs to the set O(A + P+ P? +... + P') + RP". As P does not contain A, RP‘*' has no string of length less than i + 1 and so w is not in the set RP**'. This means that w belongs to the set QA +P+P?+... + P'), and hence to QP*. Consider a string w in the set QP*. Then w is in the set QP* for some k 2 0, and hence in Q(A + P + P? +--+ + P*). So w is on the RHS. of (5.2). Therefore, w is in R (L.HS. of (5.2)). Thus R and QP* represent the same set. This proves the uniqueness of the solution of (5.1). I Note: Henceforth in this text, the regular expressions will be abbreviated r.e. (a) Give an re. for representing the set L of strings in which every 0 is immediately followed by at least two I's. (b) Prove that the regular expression R = A + 1*(011)*(1* (011)*)* also describes the same set of strings. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 5: Regular Sets and Regular Grammars @& 141 Step 3 If v, is an initial state, make v2 also as initial state. Step 4 If v, is a final state, make v, also as the final state. EXAMPLE 5.5 Consider a finite automaton, with A-moves, given in Fig. 5.1. Obtain an equivalent automaton without A-moves. Fig. 5.1 Finite automaton of Example 5.5. Solution We first eliminate the A-move from go to qi to get Fig. 5.2(a). gy is made an initial state. Then we eliminate the A-move from qo to q2 in Fig. 5.2(a) to get Fig. 5.2(b). As gp is a final state, qo is also made a final state. Finally, the A-move from q, to qs is eliminated in Fig. 5.2(c). Fig. 5.2 Transition system for Example 5.5, without A-moves. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 168 Theory of Computer Science 5.6.1 CONSTRUCTION OF A REGULAR GRAMMAR GENERATING T(M) FoR A GivEN DFA M Let M = ({qo, «-» nls E & qo F). If w is in T(M), then it is obtained by concatenating the labels corresponding to several transitions, the first from qo and the last terminating at some final state. So for the grammar G to be constructed, productions should correspond to transitions. Also, there should be provision for terminating the derivation once a transition terminating at some final state is encountered. With these ideas in mind, we construct G as G = ({Ao, Ai, +5 Aub EP. Ao) where P is defined by the following rules: (i) A; > aA; is included in P if 5(g;, a) = qj € F. (ii) A; — @A; and A; — a are included in P if 5(g;, a) = qj € F. We can show that L(G) = T(M) by using the construction of P. Such a construction gives Ay=? adj iff 5gi, a) = Gy Aj>a iff 5(q, a) € F So, Ay => aA, => aarAy =>... = ay... Oy Ay => aay... iff 5(Go 41) = Gv 5(qi, 42) = Qa - os 5(qy a4) € F This proves that w = a, ... a € L(G) iff 6(g, a... a) € F, ice. iff we TM). a eA Ieee Construct a regular grammar G generating the regular set represented by P = atb(a + b)*. Solution We construct the DFA corresponding to P using the construction given in Section 5.2.5. The construction is shown in Fig. 5.31 Cj at a b OB (a+b) © a,b O—Bd—o0-0 —3—© Fig. 5.31 DFA of Example 5.24, with A-moves. Afier eliminating the A-moves, we get the DFA straightaway, as shown in Fig. 5.32. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. 190 & Theory of Computer Science following points regarding the symbols and productions which are eliminated: (i) C does not derive any terminal string. (ii) E and c do not appear in any sentential form. (iii) E > A is a null production. (iv) B — C simply replaces B by C. In this section, we give the construction to eliminate (i) variables not deriving terminal strings, (ii) symbols not appearing in any sentential form, (iii) null productions, and (iv) productions of the form A > B. 6.3.1 CONSTRUCTION OF REDUCED GRAMMARS Theorem 63 If G is a CFG such that L(G) # @, we can find an equivalent grammar G’ such that each variable in G’ derives some terminal string. Proof Let G = (Vy, , P, S). We define G’ = (V’y, Z. G’, S) as follows: (a) Construction of V'y: We define W, C Vy by recursion: W, = {A © Vplthere exists a production A > w where we E*}. (if W, = 8, some variable will remain after the application of any production, and so L(G) = @.) Wat = W; U {A © Vy| there exists some production A > a with ae (© U W)*} By the definition of W;, W; © W,,; for all i. As Vy has only a finite number of variables, W; = Wyy, for some k < | Vy|. Therefore, W, = Wey for j 2 1. We define Vy = W,. (b) Construction of P’: P’={A > alA, ae (VyU X)} We can define G’ = (Vx, £, P’, S). Sis in Vy (We are going to prove that every variable in Vy derives some terminal string. So if S € Vy. L(G) = @. But L(G) # .) Before proving that G’ is the required grammar, we apply the construction to an example. EXAMPLE 6.5 Let G = (Vy, 2, P, S) be given by the productions 5 > AB, A > a, B > b, B — C. Ec. Find G’ such that every variable in G’ derives some terminal string. Solution (a) Construction of V'y: W, = {A, B, E} since A» a, B > b, E ~> c are productions with a terminal string on the R.H.S. Chapter 6: Context-Free Languages B 191 = W,U {A € VylAy > @ for some a € (ZU {A, B, E})*} W, uv {S} = {A, B, E, S} W3 = W, U {A; € Vy|A, > @ for some ae (© U {S, A, B, E})*} = W,U O= Wy Therefore, Vy = {S, A, B, Ft (b) Construction of P’: P’= {A, > @|Ay, we (Vy U E)*} = {S > AB, A> a, B > b, E> c} Therefore, G = (S, A, B, E}. {a, b, c}, P’, S) Now we prove: (i) If each A € Vy, then A => w for some w € E*; conversely, if A > w, then Ae Vy fi Gi) LG’) = L@). To prove (i) we note that W, = W, U W;... U W,. We prove by induction on i that for i = 1, 2, ..., 4, A © W; implies A 2 w for some we L*. If Ae Wi), then A zw So the production A — w is in P’. Therefore, A = w. Thus there is basis for induction. Let us assume the result C for i. Let A € Wis. Then either A © Wi, in which case, A 2 w for some w € &* by induction hypothesis. Or, there exists a production A —> @ with a@e (XU w)*. By definition of P’, A > @ is in P’. We can write = XiXp ... Xp where X,¢ ZU Wy If % € W, by induction hypothesis, Xj = w; for some w; € L*. So, A 2 WiW2.-. Wy € Z* (when X; is a terminal, = X;). By induction the sani is true for i = 1, 2, The converse part can be proved in a similar way ‘by induction on the number of steps in the derivation A +> w. We see immediately that L(G’) c G L(G) as Vy & Vy and P’ ¢ P. To prove L(G) & L(G’), we need an auxiliary result ASw ifA = w for some we E* (6.5) We prove (6.5) by induction on the number of steps in the derivation A Sw. G IA = w, then A > w is in P and Ae W, ¢ Vi. As Ae Viyand we Z*, 4 > wis in P'. SoA = w, and there is basis for induction. Assume (6.5) for derivations in at most k steps. Let A s w. By Remark appearing after G 192 & Theory of Computer Science Theorem 6.1, we can split this as A =P X,X>... Ny =P wyry ++ Wy stich that X; % w;, If X; © E, then w; = X; ¢ ‘ If X;e Vy then by (i), Xj © Vy. As Xj ZB wy im at most & steps, x; = Also, X,, X3, X» € (ZU V'y)* implies that A > X\X2... Xp is in PY. Thus, A => XiXz..- Ny 22 Wi «++ Wye Hence by induction, (6.5) is true for all derivations. In particular, $ Zw implies 5 = w. This proves that L(G) & L(G’), and (ii) is completely proved. 4 Theorem 6.4 For every CFG G = (Vy, E, P, S), we can construct an equivalent grammar G’ = (Vy, 2’, P’, S) such that every symbol in Vy U © appears in some sentential form (i.e. for every X in V’y U ©’ there exists a such that § => o and X is a symbol in the string a). e Proof We construct G’ = (Vy, 2’, P’, S) as follows: (a) Construction of W, for i> 1: @ W, = {S). Gi) Wi = WU {X € Vy U Z| there exists a production A > @ with A € W; and @ containing the symbol X}. We may note that W; c Vy UE and W; & Wis. As we have only a finite number of elements in Vy U 3, Wy = Wy, for some k This means that W, = We,; for all j 2 0. (b) Construction of Vy, 2’ and P’: We define Vee WoW, Ysbu wy P’= {A> aA € Wy}. Before proving that G’ is the required grammar, we apply the construction to an example. EXAMPLE 6.6 Consider G = ({S, A, B, E}. {a, b, c}, P, S), where P consists of S > AB, A ta BobEaa Solution W, = {5} W, = {S} U {X © Vy UE | there exists a production A > @ with A € W, and @ containing X} {S} U (A, B} aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 6: Context-Free Languages & 203 A in S > A) or two or more variables. We define G, = (Vx, Z, Py, S) as follows: (i) All productions in P, are added to P, if they are in the required form. All the variables in Vy are added to Vi (ii) Consider A -> A,Az ... A, where m 2 3. We introduce new productions A — A,C,, C; ACs, ---. Cuz > AmtAm and new variables C), C2, ..., Cy.2 These are added to P” and Vy, respectively. Thus, we get G2 in Chomsky normal form. Before proving that G2 is the required equivalent grammar, we apply the construction to the context-free grammar given m Example 6.11. oN Roo Reduce the following grammar G to CNF. G is S > aAD, A — aB | bAB, Bob Dod. Solution As there are no null productions or unit productions, we can proceed to step 2. Step 2 Let G, = (V4, {a, b, d}, P;, 5), where P, and Vj are constructed as follows: (i) B > b, D > d are included in P). Gi) S — GAD gives rise to S > C,AD and C, > a. A aB gives rise to A > C,B. A — DAB gives rise to A > C,AB and C, > b. y = {S, A, B, D, C,, Cy}. Step 3 P, consists of S > C,AD, A > C,B| C,AB, B > b, D > d, C, > a, gob A> C,B, B > b, D> d, C, > a, C, > b are added to P; 5S > C,AD is replaced by S$ - C,C, and C, > AD. A — C,AB is replaced by A — C,C, and C, - AB. Let Gz = (1S, A, B, D, Cis Cy Ci, Ca}, (a, b, d}, Pr, S) where P, consists of S > C,C,, A > C,B|C,C C, 3 AD, Cy > AB, B+ b,D—>d,C,— a, C, > b. G, is in CNF and equivalent to G. Step 4 L(G) = L(G,). To complete the proof we have to show that L(G) = L(G) = UG). To show that L(G) c L{G,), we start with w € L(G). If A > X\X)...X_ is used in the derivation of w, the same effect can be achieved by using the corresponding production in P, and the productions involving the new variables. Hence, A > X,X,... Xq Thus, L(G) ¢ L(G). i 204 & Theory of Computer Science Let w € L(G,). To show that w © L(G), it is enough to prove the following: ASw ifde Yn A w (6.7) We prove (6.7) by induction on the number of steps in A > we If A = w, then A — wis a production in Pj. By construction of P,, w is a single terminal. So A — w is in P, ie. A = w. Thus there is basis for induction. & Let us assume (6.7) for derivations in at most k steps. Let A ‘Ss w. We can split this derivation as A => AyA2... An 2 Wy “Wy = w such that A 2 Wp. Each A; is either in V, or a new variable, say Cu When A; € Vy, A; Zz Wi is a derivation in at most steps, and so by induction hypothesis, A; =? w,. When A; = C,,. the production C,, — a; is applied to get A; => w;. The production A ~» A\Az ... Ap, is induced by a production A > X\Xp... Xm in P where X;= Ay if Ay € Vy and Xj = w/if Ay = Cop SOA => XiX2 «++ Xm > WiW2 +e. Wms Le. A > w. Thus, (6.7) is true for all derivations. Therefore, L(G) = L(G;). The effect of applying A — A,A2 ...A,, in a derivation for w € L(G) can be achieved by applying the productions A > A,Cy, C, > A2C>, - 5 Cn-2 2 An-tAm in P Hence it is easy to see that L(G}) ¢ L(G). To prove L(G:) C L{G,), we can prove an auxiliary result As+>w ifde Vy ADw (6.8) Condition (6.8) can be proved by induction on the number of steps in A => w. G Applying (6.7) to S, we get L(G:) ¢ L(G). Thus, L(G) = L(G) = LG) 1 ee 2 Find a grammar in Chomsky normal form equivalent to S > @AbB, A > aA|a, B > bB|b. Chapter 6: Context-Free Languages i 205 Solution As there are no unit productions or null productions, we need not carry out step 1. We proceed to step 2. Step 2 Let G, = (Vy {a, b}, Py, S), where P, and V'y are constructed as follows: (i) A > a, B — b are added to P). (ii) S — aAbB, A > aA, B > bB yield S > C,AC,B, A > C,A, B>C,B, C, > a, C, > b. Vy = {S, A, B, Ca, Cy}. Step 3 P, consists of S + C,AC,B, A > C,A, B > C,B, C, > a, G7bA7a BOD. S — C,AC;B is replaced by S > C,C,, C; > AG, C2 > CB The remaining productions in P, are added to P>. Let Gz = ({S, A, B, Car Cir Ci, C2}, {a, b}, Pa, 5), where P, consists of S > C,C,, C, > AC), C; > C,B, A> C,A, B > C,B, CG, > 4, C, > b, A a, and B > b. G, is in CNF and equivalent to the given grammar. EXAMPLE 6.13 Find a grammar in CNF equivalent to the grammar S > ~S|[S>) S]|p|q — (S being the only variable) Solution As the given grammar has no unit or nul] productions, we omit step 1 and proceed to step 2. Step 2° Let G, = (Vx, E, P;, S). where P, and Vy are constructed as follows: @ S— plq are added to Pj. (ii) S$ — ~ S induces S > AS and A > ~. (ii) S$ > [S > S] induces 5 > BSCSD, B > [,C + >,D >] Vy = {S, A, B, C, D} Step 3 P, consists of S > p|g, S > AS,A > ~,B > LC 3 23,D-), S— BSCSD. S —» BSCSD is replaced by S > BC,, C, > SCx, Cp > CC3, Cx > SD. Let Gz = ({S, A, B, C, D, Cy, Cz, Ca}, Z, P2, S) where P, consists of S > p|q|AS|BC,, A > ~,B > [, C3 3,D-], C; > SC, C, > CC;, Cs > SD. Gy is in CNF and equivalent to the given grammar. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 7: Pushdown Automata E231 The next two results are properties of the relation |* and are frequently used in constructions and proofs. Result 1 If GG @ F (x A, B) (7.1) then for every y € E*, (qs xy. @ [* (a>, », B) (7.2) Conversely, if (g1, xy. @) [ (qx, ¥, B) for some y € Z*, then (q;, x, a) FE Gy A, B). Proof The result can be easily understood once we refer to Fig. 7.2. providing an illustration of the move relation. If the pda is in state g, with o in PDS, and the moves given by (7.1) are effected by processing the string x, the pda moves to state g2 with B in PDS. The same transition is effected by starting with the input string xy and processing only x. In this case, y remains to be processed and hence we get (7.2). We can prove the converse part, i.e. (7.2) implies (7.1) in a similar way. Result 2 If G@urHae Gan (7.3) then for every B € I'*, q. x, OB) |= (d’, A, ¥B) (74) Proof The sequence of moves given by (7.3) can be split as (q. % OF py XO) Fy 2» %) Fo FG AD Consider (qj, x» &%) | — Girir Xi Oy). Let Oj = Z)Z,-.. Zy As a result of this move, Z; is erased and some string is placed above Z, ... Zp So, Z, ... Zy is not affected. If we have B below Z, ... Zy, then also Z, ... ZnB is not affected. So we obtain (4), xj, O%B) L- ists Xie Gr1B)- Therefore, we get a sequence of moves @ x, OB) F ry x, %B) F --. F GA 7B) (qs OB) | A,B) 4 Note: In general, (7.4) need not imply (7.3). Consider, for instance, A = ({qo}. {a, b}, {Zo}, 5. go. Zo. 8) where 5(qo, a, Zs) = {(go. A)}. (do. b, 2) = (qo, ZoZs)} (qo, aab, ZoZZZ0) LK (qo, ab, ZoZZ) F (qo. b, ZoZ) F (do. A, 22%) 232 _S_ Theory of Computer Science ie. (qo, aab, ZoZoZoZo) | (dor A, ZoZoZo) However, (qo, aab, Zo) | (qo ab. A); hence the pda cannot make any more transitions as the PDS is empty. This shows that (7.4) does not imply (7.3) if we assume 0: = ZoZoZo, B = Zy Y= ZoLo- EXAMPLE 7.2 A = (ldo Ms Us {ay bc}, {ay by Zo}s 8 Gor Zo (GA) is a pda, where 5 is defined as S(dor & Zo) = {gor aZ)}, S(qo, b, ZH) = ((qo. bZ)} (7.5) S(qo 4, a) = {(qo, aay}, — 5(qo, Bb, a) = {(Go, ba} (7.6) 5(qo. a. bY = {(qo. ab}. 5(qo, b, bY = ((qo. bby} 7.72) 5(go. & a) = {QD}, 5(qo. cb) = {(q1, b)}, S(Go &. ZH) = (a, Zo)} (7.8) 5(q. a, a) = 5(q,, b. b) = {Ca MI} (7.9) Ogi, A, Zo) = (Gp 4)} (7.10) We can explain 6 as follows: If A is in initial ID, then using Rule (7.5), A pushes the first symbol of the input string on PDS if it is a or b. By Rules (7.6) and (7.7), the symbols of the input string are pushed on PDS until it sees the centre-marker c. By Rule (7.8), on seeing c, the pda moves to state g; without making any changes in PDS. By Rule (7.9), the pda erases the topmost symbol if it coincides with the current input symbol (ie. if they do not match, the pda halts). By Rule (7.10), the pda reaches the final state qy only when the input string is exhausted, and then the PDS has only Zo. We can explain the concepts of ID, moves, etc. for this pda A. Suppose the input siring is acab. We will see how the pda processes this string. An initial configuration is (qo, bacab, Zo). We get the following moves: (qo, bacab, Zp) | (qo, acab, bZ) by Rule (7.5) [+ (qo cab, abZp) by Rule (7.7) L (a1. ab, abZo) by Rule (7.8) L- (ao b. bz) by Rule (7.9) L (a. A, %) by Rule (7.10) K (gp A. Z) by Rule (7.10) (qo, bacab, 29) = (gp Z) 236_& Theory of Computer Science if the PDS has no symbols from [T° (since B can reach g, only by the application of R;). This suggests that T(B) = N(A). Now we prove rigorously that N(A) = 7(B). Suppose w € N(A). Then by the definition of N(A), (Go. w. Z) Ep (g. A. A) for some q € Q. Using R2, we see that (go. w. Zo) Hr (qs Ay A) By Result 2, (qo. . 22'0) Gr (QA. Z0) (7.12) By Rj. (do. A, Zo) be Go. A. ZoZo) By Result 1. we have (a’o. *; Zo) Le Go. Z'o%Zo) (7.13) By R3. @ As Z0) Hr Gp AL A) (7.14) Combining (7.12)-(7.14), we have (do. w, Z0) Hr (gp A. AY This proves that w € 7(B), ie. M(A) C T(B). To prove 7(B) © NA), we start with w © 7(B). Then (qo. , Zo) tie (ap Ay @ (7.15) But B can reach q only by the application of R3. To apply R3, Zo should be the topmost element on PDS. 2’ is placed initially, and so when it is on the top there are no other elements in PDS. So a = A, and (7.15) actually reduces to (do . Z0) tr (dp A. A) (7.16) In (7.16), the initial and final steps are effected only by A-moves. The intermediate steps are induced by the corresponding moves of A. So (7.16) can be split as (4, Aw. Zp) te (am w, 2020) He (q, A, Zo) for some q © Q Thus, (4 Aw, Z6) be (do Z0Zo) ter (A Z0) Gp Gap As AD. As we get (qo, W, ZoZ) Hy (9, A. Zo) by applying Rz several times and Ry does not affect Z@ at the bottom, we have (qo. w, Zo) Hy (q, A, A). By the construction of Ro, we have (qo, w, Zo) Kr (q, A, A), which means w € M(A). Thus, 7(B) ¢ N(A), and hence T(B) = MA) = L. # aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. aa You have either reached a page that is unavailable for viewing or reached your viewing limit for this book. Chapter 9: Turing Machines and Linear Bounded Automata i 293 There are k tapes, each divided into cells. The first tape holds the input string w. Initially, all the other tapes hold the blank symbol. Initially the head of the first tape (input tape) is at the left end of the input w. All the other heads can be placed at any cell initially. 6 is a partial function from Q x I into Q x I x {L, R, S}*. We use implementation description to define 6. Figure 9.8 represents a multitape TM. A move depends on the current state and k tape symbols under k tape heads. Finite control Fig. 9.8 Multitape Turing machine. In a typical move: (i) M enters a new state. (ii) On each tape, a new symbol is written in the cell under the head. (iii) Each tape head moves to the left or right or remains stationary. The heads move independently; some move to the left, some to the right and the remaining heads do not move. The initial ID has the initial state go, the input string w in the first tape (input tape), empty strings of b's in the remaining k— 1 tapes. An accepting ID has a final state, some strings in each of the k tapes. Theorem 9.1 Every language accepted by a multitape TM is acceptable by some single-tape TM (that is, the standard TM). Proof Suppose a language L is accepted by a k-tape TM M. We simulate M with a single-tape TM with 2k tracks. The second, fourth, . . ., (2k)th tracks hold the contents of the k-tapes. The first, third, ..., (2k — 1)th tracks hold a head marker (a symbol say X) to indicate the position of the respective tape head. We give an ‘implementation description’ of the simulation of M with a single- tape TM M,. We give it for the case k = 2. The construction can be extended to the general case. Figure 9.9 can be used to visualize the simulation. The symbols Az and Bs are the current symbols to be scanned and so the headmarker X is above the two symbols.

You might also like