Download Sets, Logical Implication, and Relations in Automata, Grammars & Languages and more Study notes Computer Science in PDF only on Docsity! CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 1 C SC 473 Automata, Grammars & Languages Theory of Computation Lecture 02 Preliminaries C SC 473 Automata, Grammars & Languages 2 Sets • Set: primitive notion of “aggregate”—from which all of mathematics and logic can be constructed • One small hierarchy of concepts in this course: set tuple integer rational real relation function sequence string Grammar ( , , , )G V R S= Σ derives G ⇒ C SC 473 Automata, Grammars & Languages 3 Sets (cont’d) • Predicate P(x): a statement about a variable x that is true or false when x is replaced by a particular object P(x) = x is odd Main predicate for set--membership: “x ∈ A” • Some axioms of set theory Axiom of Extension: a set is determined by its “extension”: Axiom of Specification: For every set A and predicate P(x) there is a set of all elements of A for which P is true. • Ex: {x∈Z: x is positive and not prime} = {4,6,8,9,10,12,…} iff ( )A B x x A x B= ∀ ∈ ⇔ ∈ { : ( )}x A P x∈ { : is a string over {a,b}}x x ∗ Σ = Σ = { : ends in letter }L w w b ∗ = ∈ Σ { : 3 ( )( )( )[ 0 0 0 } n n n F n n x y z x y z x y z = ∈ ≥ ∧ ∃ ∃ ∃ > ∧ > ∧ > ∧ + = N CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 2 C SC 473 Automata, Grammars & Languages 4 Sets (cont’d) • Operations and relations on sets subset proper subset union intersection complement difference size of set • Special sets empty set natural numbers integers • Sets of sets Power set A B⊆ A B⊂ A B∩ A B∪ A ∅ {0,1, }= …N { 2, 1,0,1,2 }= − −… …Z A ( ) { : } 2 A A X X A= ⊆P A B− C SC 473 Automata, Grammars & Languages 5 Logical Implication (Material implication) • R → S If you do not pay us $1M by midnight (R), we will shoot your ambassador (S) FF ~shotT ~pay TT shotF pay TF ~shotF pay TT shotT ~pay R→SSR C SC 473 Automata, Grammars & Languages 6 Logical Implication (cont’d) • P → Q If you pay us $1M by midnight (P), we will not shoot your ambassador (Q) FF shotT pay TT ~shotF ~pay TF shotF ~pay TT ~shotT pay P→QQP CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 5 C SC 473 Automata, Grammars & Languages 13 Why Relations? Generalize Functions. • Ex: functions • Ex: division with remainder E ⊆ N×N×N×N • Ex: circle C ⊆ R×R • Ex: Relational Database • Grammar “derives” relation 2 issquareof( , ) issquareof {(0,0),(1,1),(2,4),(3,9), } y x x y= = … ( , , , ) 0 {(1,1,1,0),(1,2,0,1), ,(5,3,1,2), } E n m q r n mq r r m E ⇔ = + ∧ ≤ < = … … 2 2 {( , ): }C x y x y= + 13 R Time Faculty Room R Time Roomπ ⊆ × × ⊆ × * G G G E N E E N E E N ⇒ ⇒ + ⇒ C SC 473 Automata, Grammars & Languages 14 Relational Calculus 1 {( , ):( , ) } If , then {( , ):( )( , ) ( , ) } R A B R b a a b R R A B S B C R S a c b B a b R b c S − ⊆ × = ∈ ⊆ × ⊆ × = ∃ ∈ ∈ ∧ ∈ • • • • • • • • • • • A B C R S a c b C SC 473 Automata, Grammars & Languages 15 Relational Inverse • R ⊆ A × B • ________________ • < • ⊆ • FatherOf • DivisorOf • Hits • • R -1 ⊆ B × A • _______________ • > • ⊇ • ChildOf • MultipleOf • Is hit by • R R-1 A B B A CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 6 C SC 473 Automata, Grammars & Languages 16 The Calculus • • Proposition. If A A {( , ): } A I a a a A= ∈ , , ,R A B S B C T C D⊆ × ⊆ × ⊆ × 1 1 1 1 1 1 1 1 ( ) ( ) ( ) ( ) ( ) ( ) B A R S T R S T R S S R R R R I I R R R S R S R S T R T S T R R − − − − − − − − = = = = = ∪ = ∪ ∪ = ∪ ∅ = ∅ = ∅ C SC 473 Automata, Grammars & Languages 17 Proving a Proposition about Relations • Thm. • Pf: (a) Let Then and by definition of °. So and so Since (c, a) was chosen arbitrarily, (b) Let Then and So implying Hence Since (c,a) was chosen arbitrarily, (b) follows. 1 1 1 ( )R S S R − − − = 1 1 1 ( )R S S R − − − ⊆ 1 ( , ) ( ) .c a R S − ⊆ ( b B)(a,b) R (b,c) S∃ ∈ ∈ ∧ ∈ 1 1 ( , ) and ( , )b a R c b S − − ⊆ ∈ 1 1 ( , ) .c a S R − − ⊆ 1 1 1 ( ) .R S S R − − − ⊆ 1 1 1 ( )S R R S − − − ⊆ 1 1 ( , ) .c a S R − − ⊆ 1 (b,a) .R − ∈ -1 ( b B)(c,b) S∃ ∈ ∈ (b,c) S (a,b) R,∈ ∧ ∈ (a,c) .R S∈ 1 ( , ) ( ) .c a R S − ⊆ (a,c) R S∈ C SC 473 Automata, Grammars & Languages 18 Relational Properties R ⊆ A×B Relational calculus predicate calculus name 1 1 1 1 . total ( ) . single- b=c valued . 1-1 =c (injection) . onto (surjection) ( ) A B A B R R I a b aRb dom R A R R I abc aRb aRc R R I acb aRb cRb a R R I b aaRb range R B − − − − ⊇ ∀ ∃ = ⊆ ∀ ∧ ⇒ ⊆ ∀ ∧ ⇒ ⊇ ∀ ∃ = -1 1 1 (1): .( , ) .( ) . . Pf a A a a R R aaR R a a b aRb bR a a b aRb − − ∀ ∈ ∈ ⇔ ∀ ⇔ ∀ ∃ ∧ ⇔ ∀ ∃ -1 -1 (2): . ( ) [ . ] [ . ] (3): like 2. (4): like 1. Pf bb b R R b b b bb a bR a aRb b b bb aaRb aRb b b Pf Pf ′ ′ ′∀ → = ⇔ ′ ′ ′∀ ∀ ∧ → = ⇔ ′ ′ ′∀ ∀ ∧ → = CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 7 C SC 473 Automata, Grammars & Languages 19 Family Relationships i c h b f g d ea is Parent of xPy x y≡ 1 2 2 2 1 1 1 1 , 0 n P P P PP P P P n P P P P P P I PP − − − − − ≡ ≡ ≥ ∩ Self (er…asexual reproduction only …) is Child of Grandparent Great n Grandparent Parent Sibling …Sibling or self! Sibling …Parent (of child with offspring!) P F F⊆ × C SC 473 Automata, Grammars & Languages 20 Family Relationships (cont’d) i c h b f g d ea 2 2 1 1 2 2 3 2 3 2 1 2 3 2 2 3 2 3 ( ) ( ) P P P P P P PP P P P P P P P P P P P P P P P − − − − − − − − − + = = ∪ ∪ ∪ ∪ ≡ ∪ ∪ ∪ Nephew, niece … or child Uncle, Aunt or … 1st Cousin Once Removed…or … 1st Cousin Once Removed or … Ancestor Transitive closure of P Parent or Grandparent Child (w. offspring) C SC 473 Automata, Grammars & Languages 21 Binary Relations on A to itself (A) • Ex: 0 1 i i A R A A S A A R S R S R A A R R R R R R I R R ⊆ × ⊆ × ∪ ∩ = × − = = = 2 3 2 3 [ 1] { { { { { Thm. ( ) xRy y x R R R R R R R R R R I R R I + ∗ + ∗ + ≡ = + ⊆ × = = = = ∪ ∪ ∪ = ∪ = = ∪ N N CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 10 C SC 473 Automata, Grammars & Languages 28 Relations, Digraphs, Matrices (cont’d) 2 3 4 1 1 1 1 1 1 1 1 ( ) ( ) 0 0 0 1 0 0 0 0 ( ) R R R R R M R M R G R + + + + = ∪ ∪ ∪ = = = a b c d C SC 473 Automata, Grammars & Languages 29 Transitive Closure (finite graph) a b c d a b c d a b c d a b c d R 2 R+ 3 R+ R + = 24 RR =+ C SC 473 Automata, Grammars & Languages 30 Transitive Closure ≡ Reachability • Defn: a reaches b in relation (digraph) R iff • Prop: a reaches b in R iff aR+b • Thm: Let be a relation where Then Pf: Longest possible path in G(R+) that will not repeat an edge is of length n. This path will result in an edge in Rn. • Ex: may need to go up to Rn 0 1 0 1 ( 0. .) ( ,0 1.) k k i i k a a a a a a b i i k a Ra + ∃ > ∃ = ∧ = ∧ ∀ ≤ ≤ − … R A A⊆ × | | .A n= 2 . n R R R R + = ∪ ∪ ∪ a b c a b c a b cR 2 R 3 R CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 11 C SC 473 Automata, Grammars & Languages 31 Strings and Languages • In this course, a “language” is simply a set of strings; a “programming language” is much more complex • alphabet Σ - a finite set of symbols • String (word) over Σ - finite sequence of symbols • ε - the empty or null string • |w| - length of string w What is a string precisely? String w of length n is a function • String ops concatenation powers ε ≠ ∈ : {1, , } ( ) th symbol w n w j j → Σ = … 0 1 ( ) ( ) i i x y xy xy z x yz x x x w w w w ε ε ε + ⋅ = = = = = = C SC 473 Automata, Grammars & Languages 32 Strings and Languages (cont’d) • Σ∗ = {w: w is a string over Σ} ε ∈ Σ∗ • Language L over Σ - a subset L ⊆ Σ∗ • Ex: • Ex: Σ = ASCII codes ( = blank = \040 = ∈ Σ ) 0 1 2 3 2 4 { , } { : prime} { , , , } {( ) : } n n a b L L L a n L aa ab ba bb L ab n ∗ Σ = = ∅ = Σ = = = ∈ N ㄷ { } {}ε∅ ≠ ≠ ㄷ C SC 473 Automata, Grammars & Languages 33 Strings and Languages (cont’d) • Language ops Set operators Concatenation Powers • Ex: • Ex: 1 2 1 2 1 2 1 2 , , , L L L L L L L L L L ∗ ∗ ∗ ⊆ Σ ⊆ Σ ∪ ∩ − = Σ − 1 2 1 2 { : }L L x y x L y L⋅ = ⋅ ∈ ∧ ∈ 0 1 { } i i L L L Lε += = ⋅ 1 2 1 2 { , } { , } { , } { , , } a b L a ab L bc c L L abc ac abbc Σ = = = ⋅ = 2 3 4 3 4 3 3 3 2 1 3 { : } { , } ({ } { }) { } { } { : } { : } n n i L a n L a L L L a L a L a n L a i ε ε ε + = ∈ = = ∪ = ∪ = ∈ ∪ = ∈ N N N CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 12 C SC 473 Automata, Grammars & Languages 34 Strings and Languages (cont’d) • Language ops (cont’d) Defn: Kleene Closure (Star) Note: Defn: • Ex: • Ex: 1 1 2 { :( 0)( , , ) } k k L w k w w L w w w w ∗ = ∃ ≥ ∃ ∈ = … { , } { } { : has one } { }{ }{ } { : has one } {} {} ? a b a w w at least a b a b w w exactly a a a ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ Σ = Σ Σ = ∈ Σ = ∈ Σ Σ Σ Σ = Lε ∗∈ L LL + ∗ = { }ε∗ +∅ = ∅ = ∅ C SC 473 Automata, Grammars & Languages 35 Strings and Languages (cont’d) • Theorem: • Pf: • Ex: 1 1 2 ( 0)( ) ( ) } ( 0) k k k w L k w L w L w w w w k w L ∗ ∈ ⇔ ∃ ≥ ∃ ∈ ∃ ∈ = ⇔ ∃ ≥ ∈ … { , } {{ }{ } { }{ } { }{ }{ }{ } { }{ }{ }{ }{ }{ } } { } { } { }{ : 0 } { }{ : 1 } { }{ : 2 } { }{ : 3 } { } { } { : 0 1 2 3 } { } { } { , } a b b a b a b a b a b a b a b a b w b b w b b w b b w b b w b b b b b a b ε ε ε ε ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ Σ = = ∪ ∪ ∪ ∪ = ∪ ∪ ∪ ∪ ∪ = ∪ ⋅ ∨ ∨ ∨ ∨ = ∪ ⋅ 0 1 2 0 i i L L L L L ∞ ∗ = = ∪ ∪ ∪ = ∪ C SC 473 Automata, Grammars & Languages 36 Strings and Languages (cont’d) • Ex: • Ex: • Ex: {{ }{ }}a b ∗ ∗ ∗ = ( { })L ε ∗∪ = 2 3 { , } ? L a a L ∗ = = 2 { } { }{}a aε ∗∪ { , }a b ∗ L ∗ CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 15 C SC 473 Automata, Grammars & Languages 43 Parentheses (cont”d) ( ( ( ) ( ) ) ( ( ( ) ) ) ) ( ) ) ( ( ) ( ) | | | |D x x x= − ( ( D x C SC 473 Automata, Grammars & Languages 44 Parentheses (cont’d) • Thm: A word w is balanced iff it has the prefix property. • Lemma 1: w balanced ⇒ w has prefix property C. Pf: Induction on |w| Base: |w|=0 ⇒ w=ε ⇒ w satisfies C. Step: Let |w|=n. Assume (IH) all strings shorter than n that are balanced satisfy C. Let w be balanced. Two cases are possible: Case w=uv where u,v are balanced. By IH, u, v satisfy C. Then and so w satisfies C(a). Next consider a prefix s of w=uv. If s is a prefix of u then because by IH, then w satisfies C(b) for this prefix. ( ( ( ) ) ) | | | | | | | | | | | |w u v u v w= + = + = ( ) | | | |s s≥ C SC 473 Automata, Grammars & Languages 45 Parentheses (cont’d) If s = ut where t is a prefix of v then by IH, and so and so w satisfies C(b) in this case. Case w=(u) where u is balanced. By IH u satisfies C, and so clearly so does (u) ( ( ( ) ( ) ) ) | | | | | | | | | | | | | | | | s u t u t u t s = + = + ≥ + = ( ) | | | |t t≥ CSC 473 Automata, Grammars & Languages 8/22/2007 Lecture 02 16 C SC 473 Automata, Grammars & Languages 46 Parentheses (cont’d) • Lemma 2: w satisfies C ⇒ w is balanced. Pf: Induction on |w| Base: w=ε is balanced by definition. Step: Let |w|=n >0. Assume (IH) all strings shorter than n that satisfy C are balanced. Let w have prefix property C. Let x be the shortest prefix of w such that Such a prefix exits since w has this property. Case x=w Then w =(u) where u satisfies C. By IH, u is balanced, and so then so is w. Case xv=w with v≠ε. Now x satisfies C(a) by assumption and satisfies C(b) since w does. So by IH x is balanced. ( ) | | | |x x= C SC 473 Automata, Grammars & Languages 47 Parentheses (cont’d) We claim that v has property C. Since and then and so v satisfies C(a). Suppose there were a prefix y of v such that Then Which would violate the prefix property of w. Thus it must be that So v satisfies C(b). By IH, v is balanced. Since both x and v are balanced, w=xv is balanced. ( ) | | | |x x= ( ) | | | |v v=( )| | | |w w= ( ) | | | |.y y< ( ) | | | |,xy xy< ( ) | | | |.y y≥