Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Class 15: Recursive Data Structures

David Evans
October 18, 2017

Class 15: Recursive Data Structures

cs2102: Discrete Mathematics
University of Virginia, Fall 2017

See course site for notes:
https://uvacs2102.github.io

David Evans

October 18, 2017
Tweet

More Decks by David Evans

Other Decks in Education

Transcript

  1. Class 15: Recursive Data Structures cs2102: Discrete Mathematics | F17

    uvacs2102.github.io David Evans University of Virginia
  2. Plan for Week Recursive Data Structures: Structural Induction (MCS Chapter

    7) o Book data structures: strings (balanced strings), non-negative integers, arithmetic expressions o Class data structures: lists (today), trees (Thurs) You should definitely read and understand what is in the book (and ask questions about anything), but we will (mostly) do different examples in class.
  3. Correctness of Slow Exponentiation ∷= ℕ × ℕ ∷= {

    , ⟶ , − 1 | ∀ ∈ ℕ, ∀ ∈ ℕ3} 6 ∷= (1, ) What preserved invariant would be useful?
  4. Proving Preserved Invariant = , ∷= = ;<= ∷= {

    , ⟶ , − 1 | ∀ ∈ ℕ, ∀ ∈ ℕ3}
  5. Invariant is Preserved ∷= { , ⟶ , − 1

    | ∀, ∈ ℕ3} = , ∷= = ;<= Does this prove slow_power(a, b) =
  6. Invariant Principle = , ∷= = ;<= Base case: If

    a preserved invariant is true for the start state, it is true for all reachable states.
  7. Invariant Principle = , ∷= = ;<= Prove slow_power(a, b)

    = Base case: (6 = 1, ) = ;<= since = 1 = 6 and − = 0. Inductive case: is a preserved invariant
  8. Partial Correctness = , ∷= ∈ ℕ ∧ = ;<=

    Prove slow_power(a, b) = If machine reaches a terminating state A = (, 0) the result is correct: = ;. What else do we need to show?
  9. Termination = , ∷= ∈ ℕ ∧ = ;<= Prove

    slow_power(a, b) = Eventually reaches state A = , 0
  10. Reachability Reaches state (, ): ∷= { , ⟶ ,

    − 1 | ∀, ∈ ℕ3} 6 ∷= (1, )
  11. Reachability Reaches state (, ): ∷= { , ⟶ ,

    − 1 | ∀, ∈ ℕ3} 6 ∷= (1, ) Is slow exponentiation fast enough? In = steps, the terminating state is reached.
  12. Fast Exponentiation Read this example in the book 6.3.1 -

    same idea, but more complex preserved invariant needed.
  13. Main Idea We can show a property holds for all

    objects of a data type by: 1. Showing the property holds for all base objects. 2. Showing that all the ways of constructing new objects, preserve the property
  14. Multuples A Quintuple is a Pair where the second part

    is a Quadruple A Sextuple is a Pair where the second part is a Quintuple A Septuple is a pair where the second part is a Sextuple An Octuple is group of octupi A ? is a Pair where the second part is a …? 24
  15. Lists 25 A List is a Pair where the second

    part is a List. But, then we can not create any lists!
  16. Lists 26 A List is either: a Pair where the

    second part is a List or, the empty list null in LISP, None in Python, for mathematical lists
  17. Defining List Definition 1. A List is an ordered sequence

    of objects. Definition 2. A List is (1) either a Pair where the second part is a List or (2) the empty list. Are these equivalent definitions?
  18. Charge Thursday: Structural Induction on lists Problem Set 6 due

    Friday Send any questions you want for Thursday’s class