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, … F n =F n-1 +F n-2
Iteration vs. Recursion � Solution using iteration: long Fibonacci (int n) { int i; long f0 = 1, f1 = 1, f2; if (n < 2) { return 1; } else { for (i = 2; i < n; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } } return f2; }
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 printSequence (int n) { if (n > 0) { printSequence(n – 1); System.out.println(n); } } void printSequence (int n) { if (n > 0) { System.out.println(n); printSequence(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 mergeSort (int start, int end) { if (start < end) { mergeSort(start, (start + end) / 2); mergeSort((start + end) / 2 + 1, end); merge(start, end); } } � Advanced topic: How to write the merge method?
Dictionary Data Structure (Tries) Dictionary Data Structure (Tries) Store this collection of words in the memory efficient way : {abacus, abode, dreaded, dust, dusty, planar, east} {abacus, abode, dreaded, dust, dusty, planar, east} How to pr oceed?
We know Arrays!! We know Arrays!! = 6 slo ts a b a c u s = 5 slo ts a b o d e Can we use fewer slo ts? L e t’s se e ho w many re dundant = 7 slo ts d r e a d e d slo ts we are using he re ! d u s t = 4 slo ts = 5 slo ts d u s t y
Idea? 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 Trie � Store characters in each node, not keys � Use characters of the key to guide the search process � From re retrie trieval val, but pronounced “ try ”
Applications 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
Recommend
More recommend