Procedural Recursion: Eric Roberts CS 106B April 15, 2009
Procedural Recursion: Eric Roberts CS 106B April 15, 2009
Recursion
One of the most important Great Ideas in CS 106B is the concept of recursion, which is the process of solving a problem by dividing it into smaller subproblems of the same form. The italicized phrase is the essential characteristic of recursion; without it, all you have is a description of stepwise refinement of the sort we teach in CS 106A. The fact that recursive decomposition generates subproblems that have the same form as the original problem means that recursive programs will use the same function or method to solve subproblems at different levels of the solution. In terms of the structure of the code, the defining characteristic of recursion is having functions that call themselves, directly or indirectly, as the decomposition process proceeds.
Goal: Goal: Goal: Goal: Goal: Goal: Goal: Goal: Goal: Goal: $100,000 $100,000 $100,000 $100,000 $100,000 $100,000 $100,000 $100,000 $100,000 $100,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $10,000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $1000
Goal: $100
Goal: $100
Goal: $100
Goal: $100
Goal: $100
Goal: $100
Goal: $100
Goal: $100
Goal: $100
Goal: $100
skip simulation
Finding a recursive solution is mostly a matter of figuring out how to break it down so that it fits the paradigm. When you do so, you must do two things:
1. Identify simple cases that can be solved without recursion. 2. Find a recursive decomposition that breaks each instance of the problem into simpler subproblems of the same type, which you can then solve by applying the method recursively.
Graphical Recursion
Recursion comes up in certain graphical applications, most notably in the creation of fractals, which are mathematical structures that consist of similar figures repeated at various different scales. Fractals were popularized in a 1982 book by Benoit Mandelbrot entitled The Fractal Geometry of Nature.
One of the simplest fractal patterns to draw is the Koch fractal, named after its inventor, the Swedish mathematician Helge von Koch (1870-1924). The Koch fractal is sometimes called a snowflake fractal because of the beautiful, six-sided symmetries it displays as the figure becomes more detailed. as illustrated in the following diagram:
MovePen(x, y)
Moves the drawing pen to the point (x, y) without drawing a line.
DrawLine(dx, dy)
Draws a line from the current point using the displacements dx and dy.
GetWindowWidth()
Returns the width of the graphics window.
GetWindowHeight()
Returns the height of the graphics window.
SetCoordinateSystem(system)
Sets the coordinate system to "screen" or "cartesian".
DrawPolarLine
The Koch snowflake fractal is much easier to write if you use polar coordinates, in which each line segment is specified in terms of its length (r) and its angle from the origin (), as illustrated in the following diagram:
r
This thought-experiment serves to illustrate the fact that coastlines are fractal in that they exhibit the same structure at every level of detail.
200km 100km 50km ruler
The End