1 / 23

Recursion

Recursion. Recursion. What is recursion? When one function calls itself directly or indirectly. Gcd. Find largest integer d that evenly divides into p and q. base case. reduction step, converges to base case. public static int gcd ( int p , int q ) { if ( q == 0 ) return p ;

irmagarcia
Download Presentation

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. Recursion What is recursion? When one function calls itself directly or indirectly. Gcd. Find largest integer d that evenly divides into p and q. base case reduction step,converges to base case public staticint gcd(int p,int q) { if(q ==0)return p; elsereturn gcd(q, p % q); } base case reduction step

  3. H-tree Used in integrated circuits to distribute the clock signal. H-tree of order n. • Draw an H. • Recursively draw 4 H-trees of order n-1, one connected to each tip. tip and half the size size order 3 order 1 order 2

  4. Htree in Java public class Htree { public static void draw(int n, double sz, double x, double y) { if (n == 0) return; double x0 = x - sz/2, x1 = x + sz/2; double y0 = y - sz/2, y1 = y + sz/2; StdDraw.line(x0, y, x1, y); StdDraw.line(x0, y0, x0, y1); StdDraw.line(x1, y0, x1, y1); draw(n-1, sz/2, x0, y0); draw(n-1, sz/2, x0, y1); draw(n-1, sz/2, x1, y0); draw(n-1, sz/2, x1, y1); } public static void main(String[] args) { int n = Integer.parseInt(args[0]); draw(n, .5, .5, .5); } } draw the H, centered on (x, y) recursively draw 4 half-size Hs

  5. Computational Cost of the H-Tree Program • H-tree of Order 1 • 5 calls to draw() = 41 + 40 • H-tree of Order 2 • 21 calls to draw() = 42 + 41 + 40 • H-tree of Order 3 • 85 calls to draw() = 43 + 42 + 41 + 40 • Execution time grows exponentially

  6. Towers of Hanoi http://en.wikipedia.org/wiki/Image:Hanoiklein.jpg

  7. Towers of Hanoi Move all the discs from the leftmost peg to the rightmost one. • Only one disc may be moved at a time. • A disc can be placed either on empty peg or on top of a larger disc. start finish Towers of Hanoi demo Edouard Lucas (1883)

  8. Move largest disc left. cyclic wrap-around Towers of Hanoi: Recursive Solution Move n-1 smallest discs right. Move n-1 smallest discs right.

  9. Implementing the Recursive Solution • Discs 1…n: • 1 is the smallest disc, n is the largest • moves(int n, boolean left): • moves(n, true): Moves discs 1…n one pole to the left • moves(n, false): Move discs 1…n one pole to the right

  10. Towers of Hanoi: Recursive SolutionDr. Java Demo publicclass TowersOfHanoi { public staticvoidmoves(int n,boolean left){if(n ==0)return; //base case moves(n-1,!left); //move n-1 discs to right if(left) System.out.println(n + " left"); else System.out.println(n + " right"); moves(n-1,!left); //move n-1 discs to right } publicstaticvoidmain(String[] args){ int N = Integer.parseInt(args[0]); moves(N,true); } } moves(n, true) : move discs 1 to n one pole to the left moves(n, false): move discs 1 to n one pole to the right smallest disc

  11. Recursive Function Call TreeOn the Board

  12. Computational Cost of Towers of Hanoi • T(n) = Number of moves required for moving n disks • T(1) = 1 • T(n) = 2*T(n-1) + 1 • Recurrence Relation

  13. Computational Cost of Towers of HanoiT(n) = 2*T(n-1)+1; T(1) = 1 • 2 Discs; T(2) = 3 moves • 3 Discs; T(3) = 7 moves • 4 Discs; T(4) = 15 moves • What is a closed-form expression for n discs? • n Discs: T(n) = 2n -1 • Execution time grows exponentially

  14. So What if Time Grows Exponentially? • Assume that it takes 1 second to make a single move: • 1 disc: 21-1 = 1 second • 10 discs: 210-1 seconds ~ 17 minutes • 20 discs: 220-1 seconds ~ 12 days • 30 discs: 230-1 seconds ~ 34 years! • Algorithms with exponential running times are no match even for the fastest computers for large values of n • Can we implement a non-recursive solution?

  15. every other move is smallest disc Outputs of the Recursive Solution % java TowersOfHanoi 4 1 right 2 left 1 right 3 right 1 right 2 left 1 right 4 left 1 right 2 left 1 right 3 right 1 right 2 left 1 right % java TowersOfHanoi 3 1 left 2 right 1 left 3 left 1 left 2 right 1 left Need to make a total of 2n -1 moves

  16. Divide-and-Conquer Divide-and-conquer paradigm. • Break up problem into smaller subproblems of same structure. • Solve subproblems recursively using same method. • Combine results to produce solution to original problem. Many important problems succumb to divide-and-conquer. • FFT for signal processing. • Parsers for programming languages. • Multigrid methods for solving PDEs. • Quicksort and mergesort for sorting. • Hilbert curve for domain decomposition. • Quad-tree for efficient N-body simulation. • Midpoint displacement method for fractional Brownian motion. Divide et impera. Veni, vidi, vici. - Julius Caesar

  17. Summary How to write simple recursive programs? • Base case, reduction step. • Trace the execution of a recursive program. • Use pictures. Why learn recursion? • New mode of thinking. • Powerful programming tool. Divide-and-conquer. Elegant solution to many important problems.

  18. Homework 4

  19. Eureka!

  20. Two approaches • Random (“generate-and-test”) • Recursive

More Related