Summary of topics COMP2111 Week 3 Recursion Term 1, 2020 Recursive Data Types Recursion and induction Induction Structural Induction 1 2 Summary of topics Recursion Fundamental concept in Computer Science Recursion in algorithms: Solving problems by reducing to smaller cases Factorial Towers of Hanoi Recursion Mergesort, Quicksort Recursive Data Types Recursion in data structures: Finite definitions of arbitrarily large objects Induction Natural numbers Structural Induction Words Linked lists Formulas Binary trees Analysis of recursion: Proving properties Recursive sequences (e.g. Fibonacci sequence) Structural induction 3 4
Recursion Recursion Fundamental concept in Computer Science Fundamental concept in Computer Science Recursion in algorithms: Solving problems by reducing to Recursion in algorithms: Solving problems by reducing to smaller cases smaller cases Factorial Factorial Towers of Hanoi Towers of Hanoi Mergesort, Quicksort Mergesort, Quicksort Recursion in data structures: Finite definitions of arbitrarily Recursion in data structures: Finite definitions of arbitrarily large objects large objects Natural numbers Natural numbers Words Words Linked lists Linked lists Formulas Formulas Binary trees Binary trees Analysis of recursion: Proving properties Analysis of recursion: Proving properties Recursive sequences (e.g. Fibonacci sequence) Recursive sequences (e.g. Fibonacci sequence) Structural induction Structural induction 5 6 Recursion Example: Factorial Example Consists of a basis (B) and recursive process (R). Factorial: A sequence/object/algorithm is recursively defined when (typically) ( B ) 0! = 1 (B) some initial terms are specified, perhaps only the first one; ( R ) ( n + 1)! = ( n + 1) · n ! (R) later terms stated as functional expressions of the earlier terms. NB fact ( n ): (R) also called recurrence formula (especially when dealing ( B ) if ( n = 0): 1 with sequences) ( R ) else : n ∗ fact ( n − 1) 7 8
Example: Towers of Hanoi Example: Towers of Hanoi There are 3 towers (pegs) n disks of decreasing size placed on the first tower You need to move all disks from the first tower to the last tower Larger disks cannot be placed on top of smaller disks The third tower can be used to temporarily hold disks 9 10 Example: Towers of Hanoi Example: Towers of Hanoi 11 12
Example: Towers of Hanoi Example: Towers of Hanoi 13 14 Example: Towers of Hanoi Example: Towers of Hanoi 15 16
Example: Towers of Hanoi Example: Towers of Hanoi 17 18 Example: Towers of Hanoi Example: Towers of Hanoi 19 20
Example: Towers of Hanoi Example: Towers of Hanoi Questions Describe a general solution for n disks How many moves does it take? M ( n ) = 2 M ( n − 1) + 1 21 22 Summary of topics Example: Natural numbers Example A natural number is either 0 (B) or one more than a natural Recursion number (R). Recursive Data Types Induction Formal definition of N : Structural Induction (B) 0 ∈ N (R) If n ∈ N then ( n + 1) ∈ N 23 24
Example: Fibonacci numbers Example: Linked lists Example Example The Fibonacci sequence starts 0 , 1 , 1 , 2 , 3 , . . . where, after 0 , 1, Recall: A linked list is zero or more linked list nodes: each term is the sum of the previous two terms. · · · ⊥ Formally, the set of Fibonacci numbers: F = { F n : n ∈ N } , where the n -th Fibonacci number F n is defined as: (B) F 0 = 0, head (B) F 1 = 1, (I) F n = F n − 1 + F n − 2 In C: struct node { NB int data; Could also define the Fibonacci sequence as a function struct node *next; fib : N → F . Choice of perspective depends on what structure you } view as your base object ( ground type ). 25 26 Example: Linked lists Example: Linked lists Example Recall: A linked list is zero or more linked list nodes: · · · ⊥ Example We can view the linked list structure abstractly. A linked list is either: head (B) an empty list, or (R) an ordered pair ( Data , List). In C: struct node { int data; struct node *next; } 27 28
Example: Words over Σ Example: Propositional formulas Example Example A well-formed formula (wff) over a set of propositional variables, A word over an alphabet Σ is either λ (B) or a symbol from Σ Prop is defined as: followed by a word (R). (B) ⊤ is a wff (B) ⊥ is a wff Formal definition of Σ ∗ : (B) p is a wff for all p ∈ Prop (B) λ ∈ Σ ∗ (R) If ϕ is a wff then ¬ ϕ is a wff (R) If w ∈ Σ ∗ then aw ∈ Σ ∗ for all a ∈ Σ (R) If ϕ and ψ are wffs then: ( ϕ ∧ ψ ), NB ( ϕ ∨ ψ ), This matches the recursive definition of a Linked List data type. ( ϕ → ψ ), and ( ϕ ↔ ψ ) are wffs. 29 30 Programming over recursive datatypes Programming over recursive datatypes Recursive datatypes make recursive programming/functions easy. Recursive datatypes make recursive programming/functions easy. Example Example Summing the first n natural numbers: The factorial function: fact ( n ): sum ( n ): ( B ) if ( n = 0): 1 ( B ) if ( n = 0): 0 ( R ) else : n ∗ fact ( n − 1) ( R ) else : n + sum ( n − 1) 31 32
Programming over recursive datatypes Programming over recursive datatypes Recursive datatypes make recursive programming/functions easy. Example Recursive datatypes make recursive programming/functions easy. Concatenation of words (defining wv ): Example For all w , v ∈ Σ ∗ and a ∈ Σ : Summing elements of a linked list: (see tutorial) ( B ) λ v = v ( R ) ( aw ) v = a ( wv ) 33 34 Programming over recursive datatypes Programming over recursive datatypes Recursive datatypes make recursive programming/functions easy. Recursive datatypes make recursive programming/functions easy. Example Length of words: Example “Evaluation” of a propositional formula ( B ) length( λ ) = 0 ( R ) length( aw ) = 1 + length( w ) 35 36
Summary of topics Recursive datatypes Describe arbitrarily large objects in a finite way Recursion Recursive functions Recursive Data Types Define behaviour for these objects in a finite way Induction Induction Structural Induction Reason about these objects in a finite way 37 38 Inductive Reasoning Inductive Reasoning Suppose we would like to reach a conclusion of the form Suppose we would like to reach a conclusion of the form P ( x ) for all x (of some type) P ( x ) for all x (of some type) Inductive reasoning (as understood in philosophy) proceeds from Inductive reasoning (as understood in philosophy) proceeds from examples. examples. E.g. From “This swan is white, that swan is white, in fact every E.g. From “This swan is white, that swan is white, in fact every swan I have seen so far is white” swan I have seen so far is white” Conclude: “Every Swan is white” Conclude: “Every Swan is white” NB NB This may be a good way to discover hypotheses. This may be a good way to discover hypotheses. But it is not a valid principle of reasoning! But it is not a valid principle of reasoning! Mathematical induction is a variant that is valid. Mathematical induction is a variant that is valid. 39 40
Mathematical Induction Basic induction Mathematical Induction is based not just on a set of examples, but also a rule for deriving new cases of P ( x ) from cases for which P is known to hold. Basic induction is this principle applied to the natural numbers. General structure of reasoning by mathematical induction: Goal: Show P ( n ) holds for all n ∈ N . Base Case [B]: P ( a 1 ) , P ( a 2 ) , . . . , P ( a n ) for some small set of examples a 1 . . . a n (often n = 1) Approach: Show that: Inductive Step [I]: A general rule showing that if P ( x ) holds for Base case (B): P (0) holds; and some cases x = x 1 , . . . , x k then P ( y ) holds for some new case y , Inductive case (I): If P ( k ) holds then P ( k + 1) holds. constructed in some way from x 1 , . . . , x k . Conclusion: Starting with a 1 . . . a n and repeatedly applying the construction of y from existing values, we can eventually construct all values in the domain of interest. 41 42 Example Example Recall the recursive program: Let P ( n ) be the proposition that: n Example i = n ( n + 1) � P ( n ) : . 2 Summing the first n natural numbers: i =0 We will show that P ( n ) holds for all n ∈ N by induction on n . sum ( n ): if ( n = 0): 0 Proof. else : n + sum ( n − 1) [B] P (0), i.e. 0 i = 0(0 + 1) � Another attempt: 2 i =0 Example [I] ∀ k ≥ 0 ( P ( k ) → P ( k + 1)), i.e. sum2 ( n ): return n ∗ ( n + 1) / 2 k k +1 i = k ( k + 1) i = ( k + 1)( k + 2) � � → 2 2 i =0 i =0 Induction proof guarantees that these programs will behave the same. (proof?) 43 44
Recommend
More recommend