Understanding Recursive Definitions of Sets and Strings

structural induction n.w
1 / 23
Embed
Share

Explore the concepts of recursive definitions of sets and strings, including basis steps, recursive steps, and exclusion rules. Learn how these concepts form the basis for regular expressions, enabling searches for patterns like email addresses.

  • Recursive Definitions
  • Sets
  • Strings
  • Regular Expressions
  • Basis Steps

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Structural Induction CSE 311 Autumn 20 Lecture 18

  2. Announcements More midterm logistics details on page. Practice materials also up. Exam will have fewer, longer questions. Would be comfortable giving as a time-constrained 2 hour exam. I m estimating most folks will need about 3 hours to account for typing time/collaboration time. But you re allowed as much time as you want, provided it s in by the due date. NO late days on the exam; if you run into issues, send Robbie an email. Once the exam is out, we will only answer clarifying questions on questions == not any on course content (like you were in an exam room). This also applies to HW5 if you re using two late days.

  3. Announcements Wednesday is a holiday, so lecture is cancelled. One (or more) TAs will lead a review session, Tentatively: in place of the A lecture slot. It ll be recorded. HW4 back today. Along with a note on common mistakes.

  4. Recursive Definition of Sets Define a set ? as follows: Basis Step: 0 ? Recursive Step: If ? ? then ? + 2 ?. Exclusion Rule: Every element of ? is in ? from the basis step (alone) or a finite number of recursive steps starting from a basis step. What is ??

  5. Recursive Definitions of Sets We ll always list the Basis and Recursive parts of the definition. Starting now we re going to be lazy and skip writing the exclusion rule. It s still part of the definition.

  6. Recursive Definitions of Sets Basis: 6 ?,15 ? Recursive: If ?,? ? then ? + ? ? 1 1 0 1 0 1 Basis: ? ? Recursive: if ? ?, then ?? ? for all ? . If ?,? ? then ? + ? ?. Write a recursive definition of {?:? = 3?for some ? }.

  7. Strings Why these recursive definitions? They re the basis for regular expressions, which we ll introduce next week. Answer questions like how do you search for anything that looks like an email address First, we need to talk about strings. will be an alphabet alphabet the set of all the letters you can use in words. is the set of all words words all the strings you can build off of the letters.

  8. Strings ? is the empty string The string with 0 characters in Java (not null!) : Basis: ? . Recursive: If ? and ? then ?? ?? means the string of ? with the character ? appended. You ll also see ? ? (a to mean concatenate i.e. + in Java)

  9. Functions on Strings Since strings are defined recursively, most functions on strings are as well. Length: len(?)=0; len(??)=len(?)+1 for ? , ? Reversal: ??= ?; ???= ???for ? , ? Concatenation ? ? = ? for all ? ; ? ?? = ? ? ? for ? ,? Number of ? s in a string #?? = 0 #??? = #?? + 1 for ? ; #??? = #?(?) for ? ,? {?}.

  10. Functions on Strings Since strings are defined recursively, most functions on strings are as well. Length: len(?)=0; len(??)=len(?)+1 for ? , ? Reversal: ??= ?; ???= ???for ? , ? Concatenation ? ? = ? for all ? ; ? ?? = ? ? ? for ? ,? Number of ? s in a string #?? = 0 #??? = #?? + 1 for ? ; #??? = #?(?) for ? ,? {?}.

  11. Structural Induction Every element is built up recursively So to show ?(?) for all ? ? Show ?(?) for all base case elements ?. Show if ?() holds for every named element in the recursive rule, then ?() holds for the new element (repeat for each rule).

  12. Structural Induction Example Let ? be: Basis: 6 S,15 ? Recursive: if ?,? ? then ? + ? ?. Show that every element of ? is divisible by 3.

  13. Structural Induction Let ?(?) be ? is divisible by 3 We show ?(?) holds for all ? ? by structural induction. Base Cases: Inductive Hypothesis: Inductive Step: We conclude ? ? ? S by the principle of induction. Basis: 6 S,15 ? Recursive: if ?,? ? then ? + ? ?.

  14. Structural Induction Let ?(?) be ? is divisible by 3 We show ?(?) holds for all ? ? by structural induction. Base Cases: 6 = 2 3 so 3|6, and ?(6) holds. 15 = 5 3, so3|15 and ?(15) holds. Inductive Hypothesis: Suppose ?(?) and ?(?) for arbitrary ?,?. Inductive Step: We conclude ? ? ? S by the principle of induction. Basis: 6 S,15 ? Recursive: if ?,? ? then ? + ? ?.

  15. Structural Induction Let ?(?) be ? is divisible by 3 We show ?(?) holds for all ? ? by structural induction. Base Cases: 6 = 2 3 so 3|6, and ?(6) holds. 15 = 5 3, so3|15 and ?(15) holds. Inductive Hypothesis: Suppose ?(?) and ?(?) for arbitrary ?,?. Inductive Step: By IH 3 x and 3 y. So ? = 3? and ? = 3? for integers ?,?. Adding the equations, ? + ? = 3(? + ?). Since ?,? are integers, we have 3|(? + ?) by definition of divides. This gives ?(? + ?). We conclude ? ? ? S by the principle of induction. Basis: 6 S,15 ? Recursive: if ?,? ? then ? + ? ?.

  16. Structural Induction Template 1. Define ?() Show that ?(?) holds for all ? ?. State your proof is by structural induction. 2. Base Case: Show ?(?) for all base cases ? in ?. 3. Inductive Hypothesis: Suppose ?(?) for all ? listed as in ? in the recursive rules. 4. Inductive Step: Show ?()holds for the new element given. You will need a separate step for every rule. 5. Therefore ? ? holds for all ? ? by the principle of induction.

  17. Claim len(xy)=len(x) + len(y) for all ?,? . Let ?(?)be len(x y)=len(x) + len(y) for all ? . Notice the strangeness of this ?()there is a for all ? inside the definition of ?(?). That means we ll have to introduce an arbitrary ? as part of the inductive step!

  18. Claim len(xy)=len(x) + len(y) for all ?,? . Define Let ?(?)be len(x y)=len(x) + len(y) for all ? . We prove ?(?) for all ? by structural induction. Base Case: Inductive Hypothesis: Inductive Step: :Basis: ? . Recursive: If ? and ? then ??

  19. Claim len(xy)=len(x) + len(y) for all ?,? . Define Let ?(?)be len(x y)=len(x) + len(y) for all ? . We prove ?(?) for all ? by structural induction. Base Case: Let ? be an arbitrary string, len(? ?)=len(x) =len(x)+0=len(x)+len(?) Inductive Hypothesis: Suppose ?(?) Inductive Step: Let ? be an arbitrary string. Therefore, len(xwa)=len(x) + len(wa) :Basis: ? . Recursive: If ? and ? then ??

  20. Claim len(xy)=len(x) + len(y) for all ?,? . Define Let ?(?)be len(x y)=len(x) + len(y) for all ? . We prove ?(?) for all ? by structural induction. Base Case: Let ? be an arbitrary string, len(? ?)=len(x) =len(x)+0=len(x)+len(?) Inductive Hypothesis: Suppose ?(?) Inductive Step: Let ? be an arbitrary string. len(xwa) =len(xw)+1 (by definition of len) =len(x) + len(w) + 1 (by IH) =len(x) + len(wa) (by definition of len) Therefore, len(xwa)=len(x) + len(wa) :Basis: ? . Recursive: If ? and ? then ??

  21. More Structural Sets Binary Trees are another common source of structural induction. Basis: A single node is a rooted binary tree. Recursive Step: If ?1 and ?2are rooted binary trees with roots ?1 and ?2, then a tree rooted at a new node, with children ?1,?2 is a binary tree. ?1 ?2

  22. Functions on Binary Trees size( )=1 size( ) = size(?1) + size(?2) + 1 ?1 ?2 height( ) = 0 height( ) = 1+max(height(?1),height(?2)) ?1 ?2

  23. Structural Induction on Binary Trees For every rooted binary tree ?, size(?) 2 ??? ? ? +1 1 We ll show this next time.

More Related Content