Upcoming SlideShare
×

# Ch 7 recursion

5,221 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
5,221
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
54
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ch 7 recursion

1. 1. Recursion
2. 2. Recursion <ul><li>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. </li></ul><ul><li>The fern shown here is recursive because each of its leaves has a structure like that of the entire fern. </li></ul>
3. 3. Recursion <ul><li>The image shown here, a “Sierpinski gasket”, is a recursive structure. It is generated by a recursive algorithm. </li></ul><ul><li>A recursive algorithm is one that calls itself. </li></ul>
4. 4. <ul><li>Here is an algorithm to draw a Sierpinski Gasket. </li></ul><ul><li>It splits a triangle into four smaller triangles, and then calls itself for three of the four smaller triangles. </li></ul><ul><li>It is a recursive algorithm. </li></ul><ul><li>Sierpinski (triangle) </li></ul><ul><li>Start </li></ul><ul><li>Find the mid point of each side of the triangle </li></ul><ul><li>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. </li></ul><ul><li>Color in (or cut out) the center triangle </li></ul><ul><li>Do Sierpinski (triangle A) </li></ul><ul><li>Do Sierpinski (triangle B) </li></ul><ul><li>Do Sierpinski (triangle C) </li></ul><ul><li>Stop </li></ul>
5. 5. <ul><li>Recursive algorithms can generate and manipulate complex structures from a simple instruction set. </li></ul><ul><li>They are among the most powerful and useful of all algorithms. </li></ul><ul><li>Sierpinski (triangle) </li></ul><ul><li>Start </li></ul><ul><li>Find the mid point of each side of the triangle </li></ul><ul><li>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. </li></ul><ul><li>Color in (or cut out) the center triangle </li></ul><ul><li>Do Sierpinski (triangle A) </li></ul><ul><li>Do Sierpinski (triangle B) </li></ul><ul><li>Do Sierpinski (triangle C) </li></ul><ul><li>Stop </li></ul>
6. 6. <ul><li>Recursion is also important in the world around us. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
7. 7. <ul><li>The emerging fields of fractal geometry and chaos theory are largely based on recursion. </li></ul><ul><li>These images were all generated with simple fractal algorithms. </li></ul>
8. 8. Recursive methods <ul><li>Recursive methods are methods that call themselves. The sailboat.sailTo method shown here is a recursive Alice method. </li></ul>
9. 9. Recursive methods <ul><li>It is an example of a linear recursive method because it calls itself only once. </li></ul>
10. 10. <ul><li>The Sierpinski Gasket algorithm calls itself three times. </li></ul><ul><li>It is an example of exponential recursion. </li></ul><ul><li>Sierpinski (triangle) </li></ul><ul><li>Start </li></ul><ul><li>Find the mid point of each side of the triangle </li></ul><ul><li>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. </li></ul><ul><li>Color in (or cut out) the center triangle </li></ul><ul><li>Do Sierpinski (triangle A) </li></ul><ul><li>Do Sierpinski (triangle B) </li></ul><ul><li>Do Sierpinski (triangle C) </li></ul><ul><li>Stop </li></ul>
11. 11. Linear and Exponential Recursion <ul><li>Linear recursion occurs when a method calls itself once. Exponential recursion occurs when a method calls it self more than once. </li></ul><ul><li>Exponential recursion is very powerful, because an algorithm can quickly generate many copies of itself to work on smaller parts of a large problem. </li></ul>Number of copies of the method.
12. 12. Linear and Exponential Recursion <ul><li>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. </li></ul>Number of copies of the method.
13. 13. The Overhead of Recursion <ul><li>A computer needs space in its memory to keep track of each method that is currently running. </li></ul><ul><li>This space is called the method’s “ overhead ”. </li></ul>memory map Method D Method C other data Method B Method A Operating System
14. 14. The Overhead of Recursion <ul><li>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. </li></ul>memory map Method A Method D Method C other data Method B Method A Operating System
15. 15. The Overhead of Recursion <ul><li>As a recursive method repeatedly calls itself, it consumes more and more of the computer’s memory. </li></ul><ul><li>This “overhead” is part of the cost of recursion. </li></ul>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. 16. The Overhead of Recursion <ul><li>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. </li></ul><ul><li>Iteration is another name for looping. An iterative method contains a loop. </li></ul>memory map Method D Method C other data Method B Method A Operating System
17. 17. These two methods do the same thing, but one is recursive and the other is iterative.  Recursive Iterative 
18. 18. For simple processes that can be done with a single loop, iteration is often better than recursion.  Recursive Iterative 
19. 19. <ul><li>However, exponentially recursive methods are usually far more time efficient than iteration. </li></ul><ul><li>In such cases, recursion is better, even considering the overhead associated with recursion. </li></ul><ul><li>Sierpinski (triangle) </li></ul><ul><li>Start </li></ul><ul><li>Find the mid point of each side of the triangle </li></ul><ul><li>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. </li></ul><ul><li>Color in (or cut out) the center triangle </li></ul><ul><li>Do Sierpinski (triangle A) </li></ul><ul><li>Do Sierpinski (triangle B) </li></ul><ul><li>Do Sierpinski (triangle C) </li></ul><ul><li>Stop </li></ul>
20. 20. 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.