recursion
play

Recursion Recitation 11/(6,7)/2008 CS 180 Department of Computer - PowerPoint PPT Presentation

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


  1. Recursion Recitation – 11/(6,7)/2008 CS 180 Department of Computer Science, Purdue University

  2. 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.

  3. 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.

  4. Iteration vs. Recursion � Sometimes, recursions are lovely. � Example: � Towers of Hanoi

  5. 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?

  6. 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

  7. 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; }

  8. 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?

  9. 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.

  10. 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?

  11. 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.

  12. 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.

  13. 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

  14. 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?

  15. 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?

  16. 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

  17. 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!

  18. 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 ”

  19. Applications Applications � Spell checkers � Data compression � Computational biology � Routing tables for IP addresses � Storing/Querying XML documents … and many other

  20. Tries using Array of Pointers • Nodes marked red indicate the end of the word. • Each node has 26 children corresponding to 26 alphabets

  21. Tries: Insertion

  22. 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

  23. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend