1 / 24

CS102 – Recursion

CS102 – Recursion. David Davenport Bilkent University Ankara – Turkey email: david@bilkent.edu.tr Spring 2003. What is Recursion?. Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...

Télécharger la présentation

CS102 – Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS102 – Recursion David Davenport Bilkent University Ankara – Turkey email: david@bilkent.edu.tr Spring 2003

  2. What is Recursion? • Method of solving problems • Alternative to iteration • All recursive solutions can be implemented iteratively • Characteristic... • recursive methods call themselvesrecursive functions defined in terms of (simpler versions) themselves • General recursive case & stopping cases

  3. Factorial N • Iterative • N! = N * N-1 * N-2 … 2 * 1 • Recursive • 1! = 1 - stopping case • N! = N * (N-1)! - general case implement & trace in Java

  4. So, why use recursion? • Advantages... • Interesting conceptual framework • Intuitive solutions to difficult problems • But, disadvantages... • requires more memory & time • requires different way of thinking!

  5. Towers of Hanoi • The Problem… • Rules • only move one disk at a time • can’t put a disk on a smaller one • Monks believed world would end once solved for 64 disks! Takes 580 billion years! at one move per sec.

  6. 5 7 2 6 3 Print reverse • Print first N values in reverse • Simple iterative solution… • Recursive? • Hint - what is the simplest case? • Write out sequence of cases& look for previous case within each casegeneralise & note stopping case! Given... print... 3, 6, 2, 7, 5

  7. 5 7 2 6 3 N-1 N’th Print reverse - solution • Solution form Write Java & trace To print N values in reverse order if N > 0 Print the N’th value & then Print the preceding N-1 values in reverse order

  8. 5 7 2 6 3 N-1 N’th Print forward • Print set of values in normal order • Solution form Write Java & trace To print N values in normal order if N > 0 Print the first N-1 values in normal order & then Print the N’th value

  9. 5 7 2 6 3 first rest S E Print – alternative solution • Print set of values in order • Alternative solution Write Java & trace To print values btw S & E in order if S <= E Print the value at S & then Print the values btw S+1 & E in order

  10. Think about solution in terms of these two sub-problems 5 7 2 6 3 N-1 N’th Find Max • Find max of first N values To find max of N values if N = 1 return N’th value else return greater of N’th & max of preceding N-1 values

  11. Think about simplest cases of problem & more general solution in terms of these two sub-problems 5 7 2 6 3 N-1 N’th Sequential search Search( first N values of X for target) if N = 0 return not found else if N’th value is target return found at N’th else return search( first N-1 values of X for target) O(N)

  12. 5 7 2 6 3 N-1 N’th Summation • Find the sum of the first N values To sum first N values of X if N =0 return 0 else return N´th value + sum of preceding N-1 values

  13. 5 7 2 -1 3 rest first Summation 2 • Find sum of all values before sentinel (-1)

  14. 7 9 1 2 3 5 6 first half second half S E middle Binary Search O(log2N) • Think about using telephone directory • Values must be in order! • Have knowledge of data distribution • If unknown? (number guessing game)

  15. Binary Search (continued…) Search( X btw S & E for target) if S > E return not found else if ( middle value is target) return found at middle else if ( target < middle value) return search( first half of X for target) else return search( second half of X for target) First half isS, middle-1 Second half is middle+1, E

  16. 5 7 2 6 3 N-1 N’th locOfMax Selection sort O(N2) selectionSort( first N values of X) if N > 1 locOfMax = findmax( first N values of X) exchange N’th value with value at locOfMax selectionSort( first N-1 values of X)

  17. 9 1 6 6 pivot partition 5 2 7 1 3 3 9 5 7 2 Sort first part Sort second part pivot QuickSort O(Nlog2N) QuickSort( X btw S & E) if S < E pivot loc. = partition( X btw S & E) QuickSort( first part of X) QuickSort( second part of X) First part is S to pivot loc.-1 Second part is pivot loc.+1 to E

  18. Sort second half 7 3 9 6 Sort first half 1 1 5 2 3 7 5 9 2 6 merge MergeSort O(Nlog2N) Merge is the basis of sequential processing MergeSort( X btw S & E) if S < E MergeSort( first half of X) MergeSort( second half of X) Merge( first & second halves of X)

  19. 5 7 2 0 3 first rest S Counting • Count number of values before zero Zero is sentinel value& so is guaranteed to exist. This is a common form of representation for strings! count no. values before zero starting at S if value at S is zero return 0 else return 1 + count no. values starting at S+1

  20. Common mistakes! (1) sum = 0; public void count( int[] X, int S) { if ( X[S] != 0) { sum++; count( X, S+1); } } • Problems: • Need another method to getCount() • What happens if called again?

  21. Common mistakes! (2) public int count( int[] X, int S) { int sum; if ( X[S] == 0) return sum; else { sum++; return count( X, S+1); } } • Problem: • New instance of local variable for each instantiation!

  22. R A D A R middle first last Palindrome isPalindrome( word) if first >= last return true else if first char of word != last char of word return false else return isPalindrome( middle of word)

  23. The plan… • Print set of N values forward & backward • Repeat using S & E • find max value • search for target - sequential • binary search • sort - selection, quick, merge • Count using S only • palindrome • Maze & blob counting • Fractals? Recursive-descent-parsing? • …on to data structures(stacks, queues, lists, trees, …)

More Related