section 5 - university of rhode islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·...

21
Section 5.3

Upload: votuyen

Post on 12-May-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Section 5.3 

Page 2: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Recursively Defined Sets and Structures    Recursive definitions of sets have two parts: 

  The basis step specifies an initial collection of elements.   The recursive step gives the rules for forming new elements in the set from those already known to be in the set. 

Page 3: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Recursively Defined Sets and Structures   Sometimes the recursive definition has an exclusion rule, which specifies that the set contains nothing other than those elements specified in the basis step and generated by applications of the rules in the recursive step – this is usually indicated by the phrase: the smallest set generated by these rules. 

 We will always assume that the exclusion rule holds, even if it is not explicitly mentioned.  

 We will develop a form of induction, called structural induction, to prove results about recursively defined sets.  

Page 4: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Recursively Defined Sets and Structures Example: The natural numbers N. 

BASIS STEP: 0 ∊ N. RECURSIVE STEP: If n is in N, then n + 1 is in N.   

  Initially 0 is in S, then 0 + 1 = 1, then 1 + 1 = 2, etc. 

 Note: the natural numbers are a recursively defined set, later on we’ll see that mathematical induction is just a special case of structural induction. 

Page 5: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Recursively Defined Sets and Structures Example :  Subset of Integers  S: 

BASIS STEP: 3 ∊ S. RECURSIVE STEP: If x ∊ S and y ∊ S, then x + y is in S. 

  Initially 3 is in S, then 3 + 3 = 6, then 3 + 6 = 9, etc.  

Page 6: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Strings    Definition:  The set  Σ* of strings over the alphabet Σ: 

BASIS STEP: λ ∊ Σ* (λ is the empty string) RECURSIVE STEP: If w is in Σ* and x is in Σ,                   

then wx ∈ Σ*.     

  

Page 7: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Strings Example:  If Σ = {0,1}, the strings in in Σ* are the set of all bit strings, λ,0,1, 00,01,10, 11, etc. 

  

Page 8: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Strings Example:  If Σ = {a,b}, show that aab is in Σ*.

  Since λ ∊ Σ* and a ∊ Σ, a ∊ Σ*.   Since a ∊ Σ* and a ∊ Σ, aa ∊ Σ*.   Since aa ∊ Σ* and b ∊ Σ, aab ∊ Σ*. 

  

Page 9: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

String Length Opera8on   Definition: We can define a string length operator, say l:Σ*N, recursively, BASIS STEP: l(λ)= 0 with λ ∈ Σ*. RECURSIVE STEP: If w ∈ Σ* and x ∈ Σ, then  

l(w x)=l(w)+1. 

Page 10: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Balanced Parentheses    Example: Give a recursive definition of the set  of balanced parentheses P. 

   Solution: BASIS STEP:  () ∊ P RECURSIVE STEP: If w ∊ P, then  () w ∊ P,  (w) ∊ P and        w ()  ∊ P. 

  Show that (() ()) is in P.  Why is ))(() not in P? 

Page 11: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Well‐Formed Formulae in Proposi8onal Logic    Definition: The set of well‐formed formulae (WFF) in propositional logic involving T, F, propositional variables, and operators from the set {¬,∧,∨,→,↔}. BASIS STEP:  T,F, and V, where V is the set of propositional variables, are well‐formed formulae. We write T,F,∊WFF and V⊂WFF. 

RECURSIVE STEP: If E,F∊WFF, then    (¬ E),  (E ∧ F), (E ∨ F), (E → F), (E ↔ F) ∊WFF. 

    

Page 12: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Well‐Formed Formulae in Proposi8onal Logic Examples:  p∊V is a WFF T is a WFF ((p ∨q) → (q ∧ F)) is a WFF pq ∧  is not a  WFF. 

Note: in general, the syntax of formal languages can be defined inductively! This include programming languages. 

Page 13: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

The Language of Algebraic Expressions  We can define the set LA of algebraic expressions recursively:   BASIS: any number and any variable name is in LA.   RECURSIVE STEP: let A,B∊LA, thenF  A+B, A‐B, A/B, A*B, (A) ∊ LA  

Page 14: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Full Binary Trees    Definition: The set of full binary trees can be defined recursively by these steps. BASIS STEP: There is a full binary tree consisting of only a single vertex r. 

RECURSIVE STEP: If T1 and T2 are disjoint full binary trees, there is a full binary tree consisting of a root r together with edges connecting the root to each of the roots of the left subtree T1 and the right subtree T2.  

Page 15: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Building Up Full Binary Trees 

Page 16: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Structural Induc8on    Definition: To prove a property of the elements of a recursively defined set, we use  structural induction.  BASIS STEP: Show that the result holds for all elements specified in the basis step of the recursive definition. 

INDUCTIVE STEP: Assume that the property holds for the elements currently in the recursively defined set. Show that it is true for each of the rules used to construct new elements in the recursive step of the definition.  

Page 17: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Example  Observe, if n is divisible by 3, then we can write n=3k for some integer k. 

 Now, show that every element in the set S defined recursively as   basis: 3∊S   recursive step: if p∊S then p+3∊S

    is divisible by 3.   Cannot do it by exhaustively listing elements, inXinite set.

  Proof by structural induction. 

Page 18: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Example  Proof by structural induction 

  basis: 3 is divisible by 3, therefore basis case holds.   inductive step: Assume q∊S, that is, q=3k for some integer k.  We now show that q+3∊S.  Using our inductive hypothesis we have FFFFWhich shows that q+3 is divisible by 3. (QED) 

q+3= 3k +3

= 3(k +1)

Page 19: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Another Example    Definition: The height h(T) of a full binary tree T is defined recursively as follows:   BASIS STEP: The height of a full binary tree T consisting of only a root r is h(T) = 0. 

  RECURSIVE STEP: If T1 and T2 are full binary trees, then the full binary tree T = T1∙T2 has height                                           h(T) = 1 + max(h(T1),h(T2)). 

Page 20: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Another Example  Definition: The number of vertices  n(T) of a full binary tree T satisfies the following recursive formula:   BASIS STEP: The number of vertices of a full binary tree T consisting of only a root r is n(T) = 1. 

  RECURSIVE STEP: If T1 and T2 are full binary trees, then the  full binary tree T = T1∙T2 has the number of vertices                                                                    n(T) = 1 + n(T1) + n(T2). 

Page 21: Section 5 - University of Rhode Islandhomepage.cs.uri.edu/faculty/hamel/courses/2012/fall201… ·  · 2012-10-22induction, to prove results about recursively defined sets ... The

Another Example   Theorem: If T is a full binary tree, then   n(T) ≤ 2h(T)+1 – 1.    Proof: Use structural induction. 

  BASIS  STEP: The result holds for a full binary tree consisting only of a root, n(T) = 1 and h(T) = 0.  Hence, n(T) = 1  ≤ 20+1 – 1   = 1.

  RECURSIVE STEP:  Assume n(T1) ≤ 2h(T1)+1 – 1 and also                   n(T2) ≤ 2h(T2)+1  – 1 whenever T1 and T2 are full binary trees. 

 n(T)   =  1 + n(T1) + n(T2)                      (by recursive formula of n(T))           ≤ 1 + (2h(T1)+1 – 1) + (2h(T2)+1 – 1)  (by inductive hypothesis)           ≤ 2∙max(2h(T1)+1 ,2h(T2)+1 ) – 1             = 2∙2max(h(T1),h(T2))+1 – 1                   (max(2x , 2y)= 2max(x,y) )           = 2∙2h(t)  – 1                                     (by recursive definition of h(T))           = 2h(t)+1  – 1  

−2 .