Recursion
Recursion Something is said to be recursive if each of the parts that make up the thing have a structure — in other words, a design or pattern — that repeats the structure of the whole thing itself.  The fern shown here is recursive because each of its leaves has a structure like that of the entire fern.
Recursion The image shown here, a “Sierpinski gasket”, is a recursive structure.  It is generated by a recursive algorithm. A recursive algorithm is one that calls itself.
Here is an algorithm to draw a Sierpinski Gasket. It splits a triangle into four smaller triangles, and then calls itself for three of the four smaller triangles. It is a recursive algorithm. Sierpinski (triangle) Start Find the mid point of each side of the  triangle Draw lines connecting the midpoints, which will form four  smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle  Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C)  Stop
Recursive algorithms can generate and manipulate complex structures from a simple instruction set.  They are among the most powerful and useful of all algorithms. Sierpinski (triangle) Start Find the mid point of each side of the  triangle Draw lines connecting the midpoints, which will form four  smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle  Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C)  Stop
Recursion is also important in the world around us. Blood vessels in the human body have a structure that can be best described by recursion, in which they continue to divide into smaller and smaller vessels until they are tiny capillaries only a few blood cells wide.  The pattern of seeds on the head of a sunflower, the geometry of a snail’s shell, and even DNA gene sequencing all appear to be recursive structures.
The emerging fields of fractal geometry and chaos theory are largely based on recursion. These images were all generated with simple fractal algorithms.
Recursive methods Recursive methods are methods that call themselves. The  sailboat.sailTo  method shown here is a recursive Alice method.
Recursive methods It is an example of a linear recursive method because it calls itself only once.
The Sierpinski Gasket algorithm calls itself three times. It is an example of exponential recursion. Sierpinski (triangle) Start Find the mid point of each side of the  triangle Draw lines connecting the midpoints, which will form four  smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle  Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C)  Stop
Linear and Exponential Recursion Linear recursion occurs when a method calls itself once.  Exponential recursion occurs when a method calls it self more than once. Exponential recursion is very powerful, because an algorithm can quickly generate many copies of itself to work on smaller parts of a large problem.  Number of copies of the method.
Linear and Exponential Recursion Many of the most important algorithms in computing, such as those used to search large databases quickly, to perform matrix algebra, and to play games such as chess, depend on the use of exponential recursion. Number of copies of the method.
The Overhead of Recursion A computer needs space in its memory to keep track of each method that is currently running. This space is called the method’s “ overhead ”. memory map Method D Method C other data Method B Method A Operating System
The Overhead of Recursion Each copy of a method consumes some space in the computer’s memory – even if it is  a copy of another method that is already running. memory map Method A Method D Method C other data Method B Method A Operating System
The Overhead of Recursion As a recursive method repeatedly calls itself, it consumes more and more of the computer’s memory. This “overhead” is part of the cost of recursion. memory map Method A Method A Method A Method A Method A Method A Method A Method A Method D Method C other data Method B Method A Operating System
The Overhead of Recursion Often a linear recursive method can be replaced by an iterative method that is just as time efficient and that does not have such overhead. Iteration  is another name for looping. An  iterative  method contains a loop. memory map Method D Method C other data Method B Method A Operating System
These two methods do the same thing, but one is recursive and the other is iterative.    Recursive Iterative  
For simple processes that can be done with a single loop, iteration is often better than recursion.    Recursive Iterative  
However, exponentially recursive methods are usually far more time efficient than iteration.  In such cases, recursion  is better, even considering the overhead associated with recursion. Sierpinski (triangle) Start Find the mid point of each side of the  triangle Draw lines connecting the midpoints, which will form four  smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle  Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C)  Stop
Exponentially recursive methods are usually part of complex algorithms beyond what is covered in introductory computer programming.  In this chapter, we will create simple linear recursive methods, like the  sail to  method shown here, to gain some experience with recursion.

Ch 7 recursion

  • 1.
  • 2.
    Recursion Something issaid to be recursive if each of the parts that make up the thing have a structure — in other words, a design or pattern — that repeats the structure of the whole thing itself. The fern shown here is recursive because each of its leaves has a structure like that of the entire fern.
  • 3.
    Recursion The imageshown here, a “Sierpinski gasket”, is a recursive structure. It is generated by a recursive algorithm. A recursive algorithm is one that calls itself.
  • 4.
    Here is analgorithm to draw a Sierpinski Gasket. It splits a triangle into four smaller triangles, and then calls itself for three of the four smaller triangles. It is a recursive algorithm. Sierpinski (triangle) Start Find the mid point of each side of the triangle Draw lines connecting the midpoints, which will form four smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C) Stop
  • 5.
    Recursive algorithms cangenerate and manipulate complex structures from a simple instruction set. They are among the most powerful and useful of all algorithms. Sierpinski (triangle) Start Find the mid point of each side of the triangle Draw lines connecting the midpoints, which will form four smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C) Stop
  • 6.
    Recursion is alsoimportant in the world around us. Blood vessels in the human body have a structure that can be best described by recursion, in which they continue to divide into smaller and smaller vessels until they are tiny capillaries only a few blood cells wide. The pattern of seeds on the head of a sunflower, the geometry of a snail’s shell, and even DNA gene sequencing all appear to be recursive structures.
  • 7.
    The emerging fieldsof fractal geometry and chaos theory are largely based on recursion. These images were all generated with simple fractal algorithms.
  • 8.
    Recursive methods Recursivemethods are methods that call themselves. The sailboat.sailTo method shown here is a recursive Alice method.
  • 9.
    Recursive methods Itis an example of a linear recursive method because it calls itself only once.
  • 10.
    The Sierpinski Gasketalgorithm calls itself three times. It is an example of exponential recursion. Sierpinski (triangle) Start Find the mid point of each side of the triangle Draw lines connecting the midpoints, which will form four smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C) Stop
  • 11.
    Linear and ExponentialRecursion Linear recursion occurs when a method calls itself once. Exponential recursion occurs when a method calls it self more than once. Exponential recursion is very powerful, because an algorithm can quickly generate many copies of itself to work on smaller parts of a large problem. Number of copies of the method.
  • 12.
    Linear and ExponentialRecursion Many of the most important algorithms in computing, such as those used to search large databases quickly, to perform matrix algebra, and to play games such as chess, depend on the use of exponential recursion. Number of copies of the method.
  • 13.
    The Overhead ofRecursion A computer needs space in its memory to keep track of each method that is currently running. This space is called the method’s “ overhead ”. memory map Method D Method C other data Method B Method A Operating System
  • 14.
    The Overhead ofRecursion Each copy of a method consumes some space in the computer’s memory – even if it is a copy of another method that is already running. memory map Method A Method D Method C other data Method B Method A Operating System
  • 15.
    The Overhead ofRecursion As a recursive method repeatedly calls itself, it consumes more and more of the computer’s memory. This “overhead” is part of the cost of recursion. memory map Method A Method A Method A Method A Method A Method A Method A Method A Method D Method C other data Method B Method A Operating System
  • 16.
    The Overhead ofRecursion Often a linear recursive method can be replaced by an iterative method that is just as time efficient and that does not have such overhead. Iteration is another name for looping. An iterative method contains a loop. memory map Method D Method C other data Method B Method A Operating System
  • 17.
    These two methodsdo the same thing, but one is recursive and the other is iterative.  Recursive Iterative 
  • 18.
    For simple processesthat can be done with a single loop, iteration is often better than recursion.  Recursive Iterative 
  • 19.
    However, exponentially recursivemethods are usually far more time efficient than iteration. In such cases, recursion is better, even considering the overhead associated with recursion. Sierpinski (triangle) Start Find the mid point of each side of the triangle Draw lines connecting the midpoints, which will form four smaller triangles that can be called triangles A, B, C, and D, with D in the center and the others around it. Color in (or cut out) the center triangle Do Sierpinski (triangle A) Do Sierpinski (triangle B) Do Sierpinski (triangle C) Stop
  • 20.
    Exponentially recursive methodsare usually part of complex algorithms beyond what is covered in introductory computer programming. In this chapter, we will create simple linear recursive methods, like the sail to method shown here, to gain some experience with recursion.