68ac2a120bfa5455ff5e731aeb465990.ppt
- Количество слайдов: 16
More on Recursion
What’s the answer? § What value is returned by the following recursive function Mystery for an input value of n? Mystery (integer n) If n = 1 then return 1 else return Mystery(n-1) + 1 end if end function Mystery
Answer § Mystery(n) = n
What does this iterative method do? public static void mystery (String s) { for (int j = s. length() – 1; j >= 0; j --) System. out. print (s. char. At(j)); } // How would we write it recursively?
Recursive version
What is the output? public static void mystery (int n) { if (n < 1) { System. out. print (n); } else { mystery(n/2); System. out. print (”, “ + n); } } // end mystery § When call is § mystery(1) § mystery(2) mystery(3) mystery(4) mystery(7) mystery(30) RUN CODE ON NEXT SLIDE TO SEE ANSWERS.
public class Nov 53 { public static void main (String [] args) { mystery(1); System. out. println (); mystery(2); System. out. println (); mystery(3); System. out. println (); mystery(4); System. out. println (); mystery(7); System. out. println (); mystery(30); System. out. println (); } // end main public static void mystery (int n) { if (n < 1) System. out. print (n); else { mystery(n/2); System. out. print (", " + n); } } // end mystery }
Examples § A collection of M numbers is defined recursively by – 2 and 3 belong to M – If X and Y belong to M, so does X*Y Which of the following belong to M? 6 9 16 21 26 54 72 218
Recursively defined sets of strings § A collection S of strings of characters is defined recursively by – a and b belong to S – If X belongs to S, so does Xb Which of the following belong to S? a ab aba aaab bbbbb
Selection Sort § Simulate the execution of algorithm Selection. Sort on the following list L; write the list after every exchange that changes the list. § 4, 10, -6, 2, 5
Answer § § 4 10 -6 4 5 -6 4 2 -6 -6 2 4 2 2 5 5 5 10 10 10
Binary Search § The binary search algorithm is used with the following list; x has the value “flour” § Name the elements against which x is compared. § butter, chocolate, eggs, flour, shortening, sugar
Algorithm for recursive Binary Search Binary. Search (list L; integer i; integer j, itemtype x) // searches sorted list L from L[i] to l[j] for item x If i > j then write (“not found”) else find the index k of the middle item in the list L[i] to L[j] if x = middle item then write (“found”) else if x < middle item then Binary. Search (L, I k-1, x) else Binary. Search (L, k+1, j, x) end if end function Binary. Search
Recursion with logical formulas § Rule 1: Any statement letter is a wff § Rule 2: If P and Q are wffs, so are (P Λ Q), (P V Q), (P → Q), P’ , and (P ↔ Q) § Example – A, B, C are all wffs by rule 1 – (A Λ B) and (C’) are both wffs by rule 2 – ((A Λ B) → (C’)) is a wff, by rule 2 – (((A Λ B) → (C’))’) – ((A Λ B) → C’)’
Recursion in family relations § James’s parents are ancestors of James
System. out. println (); § A computer virus that spreads by way of e-mail messages is planted in 3 machines the first day. Each day, each infected machine from the day before infects 5 new machines. On the second day, a software solution is found to counteract the virus, and 1 machine is cleaned. Each day thereafter, 6 times as many machines are cleaned as were cleaned the day before. How many days will it be before the effects of the virus are completely gone?