Upload
chaffey-college
View
4.038
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
An Introduction to Programming Using Alice
Recursion
An Introduction to Programming Using Alice
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.
An Introduction to Programming Using Alice
Recursion
The image shown here, a “Sierpinski gasket”, is a recursive structure. It is generated by a recursive algorithm.
A recursive algorithm isone that calls itself.
An Introduction to Programming Using Alice
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
An Introduction to Programming Using Alice
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
An Introduction to Programming Using Alice
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.
An Introduction to Programming Using Alice
The emerging fields of fractal geometry and chaos theory are largely based on recursion.These images were all generated with simple fractal algorithms.
An Introduction to Programming Using Alice
Recursive methodsRecursive methods are methods that call themselves. The sailboat.sailTo method shown here is a recursive Alice method.
An Introduction to Programming Using Alice
Recursive methodsIt is an example of a linear recursive method because it calls itself only once.
An Introduction to Programming Using Alice
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
An Introduction to Programming Using Alice
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.
Linear Recursion
0
20
40
60
80
100
120
140
1 2 3 4 5 6 7 8
Exponential Recursion
0
20
40
60
80
100
120
140
1 2 3 4 5 6 7 8
Number of copies of the method.
An Introduction to Programming Using Alice
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.
Linear Recursion
0
20
40
60
80
100
120
140
1 2 3 4 5 6 7 8
Exponential Recursion
0
20
40
60
80
100
120
140
1 2 3 4 5 6 7 8
Number of copies of the method.
An Introduction to Programming Using Alice
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
Operating System
Method A Method B
other dataMethod C Method D
An Introduction to Programming Using Alice
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
Operating System
Method A Method B
other dataMethod C Method D
Method A
An Introduction to Programming Using Alice
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.
Operating System
Method A Method B
other dataMethod C Method D
Method A Method A Method A Method A
Method A Method A Method A Method A
memory map
An Introduction to Programming Using Alice
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
Operating System
Method A Method B
other dataMethod C Method D
An Introduction to Programming Using Alice
These two methods do the same thing, but one is recursive and the other is iterative.
Recursive
Iterative
An Introduction to Programming Using Alice
For simple processes that can be done with a single loop, iteration is often better than recursion.
Recursive
Iterative
An Introduction to Programming Using Alice
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
An Introduction to Programming Using Alice
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.