Chapt. 3.1 Recursive Definition
Total Page:16
File Type:pdf, Size:1020Kb
Chapt. 3.1 Recursive Definition Reading: 3.1 Next Class: 3.2 1 Motivations Proofs by induction allow to prove properties of infinite, partially ordered sets of objects. In many cases the order is only given implicitly and we have to find it and write it explicitly in terms of the indices. In the inductive step in these proofs we have to relate the property for one object to the ones of previous objects. This often involves defining P(n + 1) in terms of P(n). Recursive Definitions 1 2 Terminologies A sequence is a list of objects that are enumerated in some order. A set of objects is a collection of objects on which no ordering is imposed. Inductive Definition/Recursive Definition: a definition in which the item being defined appears as part of the definition. Two parts: a basis step and an inductive/recursive step. As in the case of mathematical induction, both parts are required to give a complete definition. Recursive Definitions 3 Recursive Sequences Definition: A sequence is defined recursively by explicitly naming the first value (or the first few values) in the sequence and then defining later values in the sequence in terms of earlier values. Many sequences are easier to define recursively: Examples: S(1) = 2 S(n) = 2S(n-1) for n 2 • Sequence S 2, 4, 8, 16, 32,…. Fibonacci Sequence F(1) = 1 F(2) = 1 F(n) = F(n-1) + F(n-2) for n > 2 • Sequence F 1, 1, 2, 3, 5, 8, 13,…. Recursive Definitions 2 4 Recursive Sequences (cont’d) T(1) = 1 T(n) = T(n-1) + 3 for n 2 • Sequence T 1, 4, 7, 10, 13,…. The sequences of the sums S(n) of all positive integers 1 ≤ n S(1) = 0, S(n + 1) = S(n) + n. The sequence of the product of all positive integers smaller than n. S(1) = 1, S(n + 1) = S(n) * n. Recursive Definitions 5 Recursive Sets In a similar form to recursive sequences, the members of sets can be defined recursively. Examples: The set of strings representing the positive integers. 1. Basis Step: 1; 2; 3; 4; 5; 6; 7; 8; 9 are strings representing integers. 2. Inductive Step: d0; d1; d2; d3; d4; d5; d6; d7; d8; d9 are strings representing integers, where d is a string representing an integer. Recursive Definitions 3 6 Recursive Sets (cont’d) Example: A recursive definition for the set of people who are ancestors of James: 1. Basis Step: James’s parents are ancestors of James 2. Inductive Step: Every parent of an ancestor of James is an ancestor of James. Example: The set of all wffs in propositional logic. 1. Basis Step: Every statement symbol is a wff. 2. Inductive Step: Given wffs R and S, RVS, R ^ S, R → S, R’, and R↔S are also wffs. Recursive Definitions 7 Recursively defined operations Recurrence relation can also be used as the definition for an operation. Exponential operation 0 a = 1 n n-1 a = aa for n 1 Multiplication operation m*1 = m m*n = m*(n-1) + m for n 2 Factorial Operation F(0) = 1 F(n) = nF(n-1) for n 1 Recursive Definitions 4 8 Recursively Defined Algorithms If a recurrence relation exists for an operation, the algorithm for such a relation can be written either iteratively or recursively Example: Factorial, multiplication etc. S(1) = 2 S(n) = 2S(n-1) for n 2. Recursive Definitions 9 Recursively defined algorithms (cont’d) Approach one: Iterative Algorithm S(integer n) //function that iteratively computes the value S(n) Local variables: integer i ;//loop index CurrentValue ; if n =1 then output 2 i = 2 CurrentValue = 2 while i n CurrentValue = CurrentValue *2 i = i+1 end while return CurrentValue end if end function S Recursive Definitions 5 10 Recursively defined algorithms (cont’d) • Approach two: Recursive Algorithm S(integer n) //recursively calculates S(n) if n =1 then //base case output 2 else return (2*S(n-1)) end if end function S Recursive Definitions 11 Recursive algorithm for selection sort Algorithm to sort recursively a sequence of numbers in increasing or decreasing order Function sort(List s, Integer n) //This function sorts in increasing order if n =1 then output “sequence is sorted” //base case end if max_index = 1 //assume s1 is the largest for j = 2 to n do if s[j] > s[max_index] then max_index = j //found larger, so update end if end for exchange/swap s[n] and s[max_index] //move largest to the end return(sort(s,n-1)) end function sort Recursive Definitions 6 12 Selection Sort Example Sequence S to be sorted: S: 23 12 9 –3 89 54 After the 1st recursive call on the whole sequence (6 elements), the sequence is: Swap 89 and 54 S: 23 12 9 -3 54 89 After the 2nd recursive call (on the previous remaining 5 elements), : Nothing is swapped, the sequence remains the same: S: 23 12 9 -3 54 89 After the 3rd recursive call (on the previous remaining 4 elements), the sequence is: S: -3 12 9 23 89 54 After the 4th recursive call, the sequence is: S: -3 9 12 23 54 89 After the 5th recursive call, : Nothing is swapped, the sequence remains the same as S: -3 9 12 23 54 89 Recursive Definitions 13 Recursive algorithm for binary search Looks for a value in an increasing sequence and returns the index of the value if it is found or 0 otherwise. Function binary_search(sorted List L; integers i, j; key) if i > j then //not found return 0 else find the index m for the middle term in the list L[i] – L[j] if key = middle term then // base case return (m) else if key < middle term then binary_search(L, i, m-1, key) else binary_search(L, m+1, j, key) end if end if end binary_search Recursive Definitions 7 14 Binary Search Example Search for 81 in the sequence 3 6 18 37 76 81 92 Sequence has 7 elements. Calculate middle point: (1 + 7)/2 = 4. th Compares 81 to 37 (4 sequence element): no match. Search in the upper half since 81 > 37 New sequence for search: 76 81 92 Calculate midpoint: (5 + 7)/2 = 6 th 6 Element: 81 Compares 81 to 81: perfect match th Element 81 found as the 6 element of the sequence Recursive Definitions 15 Example The Fibonacci numbers: F(1) = F(2) =1 F(n+2) = F(n) + F(n+1) for n>2: Write the recursive algorithm: Recursive Definitions 8 16 Example What does the following recursive function Mystery return for an input value of n? Function mystery(integer n) if n = 1 then return 1 else return (mystery(n-1)+1) end if end function mystery Recursive Definitions 17 Example In an experiment, a certain colony of bacteria initially has a population of 50,000. A reading is taken every 2 hours, and at the end of every 2-hour interval, there are 3 times as many bacteria as before. a. Write a recursive definition for A(n), the number of bacteria present at the beginning of the nth time period. b. At the beginning of which interval are there 1,350,000 bacteria present? Recursive Definitions 9.