# Recursion Recitation 116 72008 CS 180 Department of

Title Recursion Recitation 116 72008 CS 180 Department of French PPT 23 540 KB 260

##### Description Recursion Recitation – 11/(6, 7)/2008 CS 180 Department of Computer Science, Purdue University Project 7 Now posted on the class webpage. Due Wed, Nov. 19 at 10 pm. Milestone Due Wed, Nov. 12 at 10 pm. Start early. All questions on the class newsgroup. There will be additional slides for project 7 today. Iteration vs. Recursion Iteration: We need to schedule everything in advance. The compiler can only follow our instructions. Since we are always clever, iterations are usually efficient. Recursion: We tell the compiler how to make the problem smaller and/or which problem can be solved directly. The compiler will generate the right process for us, but it is not so clever and may do some “meaningless” work. Iteration vs. Recursion Sometimes, recursions are lovely. Example: Towers of Hanoi Iteration vs. Recursion Solution using recursion: void Hanoi (int n, char from, char dest, char by) { if (n == 1) { System. out. println(“Move the disk from ” + from + “ to “ + dest + “. ”); } else { Hanoi(n – 1, from, by, dest); Hanoi(1, from, dest, by); Hanoi(n – 1, by, dest, from); } } Solution using iteration? Iteration vs. Recursion Sometimes, recursions are frustrating. Example: Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, … Fn=Fn-1+Fn-2 Iteration vs. Recursion Solution using iteration: long Fibonacci (int n) { int i; long f 0 = 1, f 1 = 1, f 2; if (n < 2) { return 1; } else { for (i = 2; i < n; i++) { f 2 = f 0 + f 1; f 0 = f 1; f 1 = f 2; } } return f 2; } Iteration vs. Recursion Solution using recursion: long Fibonacci (int n) { if (n < 2) { return 1; } else { return Fibonacci(n – 1) + Fibanacci(n – 2); } } It seems simple and intriguing. Why is it very inefficient? How to write recursions? Write the stop sign first. Think twice before you use a loop. Remember the differences between iterations and recursions. Take care of the order of your statements. Make good use of the parameters and notice their scopes. Try to simulate what your program may act like before letting it run. How to write recursions? The importance of the statement order: void print. Sequence (int n) { if (n > 0) { print. Sequence(n – 1); System. out. println(n); } } void print. Sequence (int n) { if (n > 0) { System. out. println(n); print. Sequence(n – 1); } } What’s the stop sign? How to write recursions? Practice the ability to cut a big problem into small pieces, which is essential to programming and helps solve all kinds of problems. Typically, cut a problem into two halves, solve them separately and then solve the whole problem. Programming with recursions can show this way of thinking explicitly. Examples of recursions Binary search What’s the idea? Sample code 11. 5 Use pen and paper to simulate the recursion. Data: 1 2 3 4 6 7 8 10 12 13 14 15 18 20 25 To find: 4, 5, 13, 19 You can also use only a loop to solve the problem. Examples of recursions Merge sort – A recursive sorting method A divide-and-conquer algorithm Array to be sorted is divided in half The two halves are sorted by recursive calls This produces two smaller, sorted arrays which are merged to a single sorted array Use an example to explain the idea Examples of recursions Brief code: void merge. Sort (int start, int end) { if (start < end) { merge. Sort(start, (start + end) / 2); merge. Sort((start + end) / 2 + 1, end); merge(start, end); } } Advanced topic: How to write the merge method? Dictionary Data Structure (Tries) Store this collection of words in the memory efficient way : {abacus, abode, dreaded, dusty, planar, east} How to proceed? We know Arrays!! a b a c u a b o d e d s = 6 slots = 5 slots Can we use fewer slots? Let’s see how many redundant = r e a d e d slots we are using here! d u s t 7 slots = 4 slots y = 5 slots Idea? Can we come up with a solution where we can take advantage of the common letters in the words? It would be great if we can reduce the redundancy, and use the same letter for more than one word! Trie Store characters in each node, not keys Use characters of the key to guide the search process From retrieval, val but pronounced “try” Applications Spell checkers Data compression Computational biology Routing tables for IP addresses Storing/Querying XML documents … and many other Tries using Array of Pointers • Nodes marked red indicate the end of the word. • Each node has 26 children corresponding to 26 alphabets Tries: Insertion Tries: Project 7 requirements Insert – Load all words from words. txt (already implemented, you just have to read words from file and call insert of Trie. java) Delete – Change the marker at the end of the word to green from red Search – Traverse down the Trie recursively with each letter of the search word. If the end node is marked red, return success Tries: Project 7 requirements Prefix Search - Find all words in the Trie which start with a given prefix (E. g. prefix : “ca” ; Output: print words car, cane, care) Jumble Search - Find the longest possible word present in the Trie that is formed from the given jumbled string (Depth-First-Search) E. g. , jumble string: “bcgylonaa”; Output: analogy

##### Similar Slides
• ##### CS 1101 X Programming Methodology Recitation 2 Classes
• 31 Pages
• Latin
• ##### CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9
• 24 Pages
• English
• ##### Java Swing th 4 April 2008 CS 180
• 45 Pages
• English
• ##### Recursion Examples Recursion review Recursion review Scenario You
• 29 Pages
• Spanish
• ##### Programming Languages and Compilers CS 421 5 Recursion
• 71 Pages
• English
• ##### CS 2110 Recitation Week 8 Hashing An implementation
• 18 Pages
• English
• ##### CS 179 GPU Computing Recitation 1 4116 Recap
• 18 Pages
• English
• ##### CS 314 Section 5 Recitation 10 Long Zhao
• 25 Pages
• Latin
• ##### CS 100 J February 23 Recursion Read pp
• 12 Pages
• English
• ##### University of Manchester Department of Computer Science CS
• 72 Pages
• French
• ##### Psalms 116 1 19 NIV Psalm 116 1
• 22 Pages
• Estonian
• ##### CS 112 Retrospective CS 112 Scientific Computation Department
• 10 Pages
• French
• ##### Bilkent University Department of Computer Engineering CS 342
• 68 Pages
• English
• ##### Bilkent University Department of Computer Engineering CS 342
• 74 Pages
• English
• ##### University of Tampere CS Department Jyrki Nummenmaa Internationalisation
• 24 Pages
• English
• • 25 Pages
• English