1 / 24

Recursion

Recursion. Russian nesting dolls. A Russian nesting doll is a sequence of similar dolls inside each other that can be opened Each time you open a doll a smaller version of the doll will be inside, until you reach the innermost doll which cannot be opened. Sierpinski tetrahedron.

cana
Télécharger la présentation

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

  2. Russian nesting dolls • A Russian nesting doll is a sequence of similar dolls inside each other that can be opened • Each time you open a doll a smaller version of the doll will be inside, until you reach the innermost doll which cannot be opened.

  3. Sierpinski tetrahedron

  4. Recursive definition • A statement in which something is defined in terms of smaller versions of itself   • A recursive definition consists of: • a base case: the part of the definition that cannot be expressed in terms of smaller versions of itself • a recursive case: the part of the definition that can be expressed in terms of smaller versions of itself

  5. Binary trees • Tree where all nodes have at most two children • Recursive definition: A binary tree is a structure defined on a finite set of nodes that either • contains no nodes, or • is composed of a root node, a right subtree which is a binary tree, and a left subtree which is a binary tree Recursive case: • root • binary tree as right subtree • binary tree as left subtree Base case: • zero nodes

  6. Mergesort • Divide and conquer sorting algorithm • Defined recursively • Divide: Divide n element array to be sorted into two subarrays with n/2 items each • Conquer: Sort the two subarrays recursively using mergesort • Combine: Merge the two sorted subarrays to produce the sorted solution

  7. Fundamentals of recursion • Base case • at least one simple case that can be solved without recursion • Reduction step • reduce problem to a smaller one of same structure • problem must have some measure that moves towards the base case in each recursive call • ensures that sequence of calls eventually reaches the base case • The Leap of Faith! • always assume the recursive case works!

  8. Classic example - factorial • n! is the factorial function: for a positive integer n, n! = n(n –1)(n -2)…321 • We know how to compute factorial using a for-loop • We can also compute n! recursively: int factorial(int n) { if(n==1) return 1; else return n*factorial(n-1); }

  9. Recursion trace factorial(6) = 6*factorial(5) factorial(5) = 5*factorial(4) factorial(4) = 4*factorial(3) factorial(3) = 3*factorial(2) factorial(2) = 2*factorial(1) factorial(1) = 1 factorial(2)= 2*factorial(1) = 2*1 = 2 factorial(3) = 3*factorial(2) = 3*2 = 6 factorial(4) = 4*factorial(3) = 4*6 = 24 factorial(5) = 5*factorial(4) = 5*24 = 120 factorial(6) = 6*factorial(5) = 6*120 = 720

  10. Recursion  iteration • Every recursive algorithm can be written non-recursively • If recursion does not reduce the amount of computation, use a loop instead • why? … recursion can be inefficient at runtime • Just as efficient to compute n! by iteration (loop) int factorial(int n) { int fact = 1; for(int k=2; k<=n; k++) fact = fact*k; return fact; }

  11. 1 if n=1 or n=2 F(n) = F(n-1)+F(n-2) if n>2 Classic bad example: Fibonacci numbers • Fibonacci numbers: sequence of integers such that • the first and second terms are 1 • each successive term is given by the sum of the two preceding terms 1 1 2 3 5 8 13 21 34 55 ... • Recursive definition: • let F(n) be the nth Fibonacci number, then

  12. Recursive subroutine for fibonacci int fibonacci(int n) { if( n==1 || n==2 ) return 1; else return fibonacci(n-1)+fibonacci(n-2); }

  13. fib(1) fib(3) fib(2) fib(5) fib(2) fib(4) fib(1) fib(3) fib(2) fib(7) fib(2) fib(4) fib(1) fib(3) fib(2) fib(6) fib(1) fib(3) fib(2) fib(5) fib(2) fib(4) fib(1) fib(3) fib(2) Fibonacci recursion

  14. Redundant computation int fibonacci(int n) { if( n==1 || n==2 ) return 1; else return fibonacci(n-1)+fibonacci(n-2); } • Inefficient!!! Why? • look at the recursion trace -- redundant computation • Exercise: • design an efficient subroutine to compute the nth Fibonacci number

  15. Number sequences • The ancient Greeks were very interested in sequences resulting from geometric shapes such as the square numbers and the triangular numbers • Each set of shapes represents a number sequence • The first three terms in each sequence are given • What are the next three terms in each sequence? • How can you determine in general the successive terms in each sequence?

  16. Square numbers The 4th square number is 9+2*4-1 =16 The 5th square number is 16+2*5-1 =25 The 6th square number is 25+2*6-1 =36 • Write a recursive Java subroutine to compute the nthsquare number 1, 4, 9, 16, 25, 36, …

  17. Square numbers • Base case • the first square number is 1 • Recursive case • the nth square number is equal to 2n-1 + the (n-1)st square number

  18. Recursive solution • int SquareNumber( int n ) • { • if( n==1 ) • return 1; • else • return 2*n-1 + SquareNumber( n-1 ); • }

  19. Triangular numbers The 4th triangular number is 6+4 =10 The 5th triangular number is 10+5 =15 The 6th triangular number is 15+6=21 • Write a recursive Java subroutine to compute the nthtriangular number 1, 3, 6, 10, 15, 21, …

  20. Triangular numbers • Base case • the first triangular number is 1 • Recursive case • the nth triangular number is equal to n + the (n-1)st triangular number

  21. Recursive solution • int TriNumber( int n ) • { • if( n==1 ) • return 1; • else • return n + TriNumber( n-1 ); • }

  22. int Something( int a, int b ) { if( a < b ) return a+b; else return 1 + Something( a-b, b ); } Something • What is the value of x after executing the following statement?x = Something(2,7); • What is the value of y after executing the following statement?y = Something(8,2);

  23. Strange What is the value of the expression Strange(5)? • 5 • 9 • 11 • 15 • 20 What is the value of the expression Strange(6)? • 6 • 10 • 12 • 15 • 21 int Strange( int x ) { if( x <= 0 ) return 0; else if( x%2 == 0 ) return x + Strange(x-1); else return x + Strange(x-2); }

  24. Weirdo int Weirdo(int n) { if(n == 1) return 1; else return 2 * Weirdo(n-1) * Weirdo(n-1); } If n is a positive integer, how many times will Weirdo be called to evaluate Weirdo(n) (including the initial call)? • 2n • 2n-1 • 2n • 2n-1 • 2n-1 What is the value of the expression Weirdo(4)? • 16 • 32 • 64 • 128 • 256

More Related