If you can't read please download the document
Upload
gurpal-singh
View
277
Download
0
Embed Size (px)
Citation preview
Intro to Recursion in Programming with Fractals
@gurpalsingh204Presentation version of blog post onSinghcodes.wordpress.com
What is recursion?
Recursion simply is applying a procedure, breaking it into smaller parts, and applying the same to the smaaller parts.
Example: Fibonacci series: F(n) = F(n-1)+F(n-2)
Usually one has to get some experience before getting a good feel of the concept.
But if you could see what happens under the hood you could get a better understanding more quickly.
Fractals are perfect for this purpose besides being beautiful.
What are fractals?
A Fractal, basically, is a repeating pattern.
Each smaller part of the fractal is identical to the bigger part.
If you zoom into an ideal fractal, you couldnt tell how much you have zoomed in.
An Example of fractals
The mandelbrot set is a famous fractal
Let's make some fractals!
The Koch Line
We will begin with the simplest fractal : The Koch Line
The rule to make Koch line:
Draw (an imaginary) straight line.
Split it into three equal parts.
Replace the middle part with an equilateral triangle without a base
Apply the same procedure to all the new straight lines(recursion).
Finally draw the lines.
Defining iterations
Usually it is not feasible Step 4, in the last slide, a lot of times.
We need to stop after a finite number of times.
Each execution of step 1-3 is an iteration.
The number of times we apply the rule is called the number of iterations.
The Koch Line
Next we see the result of executing the steps.
I used simple C graphics to make the fractals.
The code is available on GitHub.
The Koch Line
One Iteration:
The Koch Line
Two Iterations:
The Koch Line
Three Iterations:
The Koch Line
Four Iterations:
The Koch Snowflake
The Koch Line procedure could be used to make a Koch Snowflake
To make a Koch Snowflake:
Make an equlateral trinangle
Apply Koch Line procedure to all the sides of the triangle
Let's see what the resulting fractal looks like.
The Koch Snowflake
The Koch Snowflake
In different colours
One More Example:
The Sierpinski Arrowhead Curve
The Sierpinski Arrowhead Curve
Following are the steps to make a sierpinski arrowhead curve
Draw an imaginary line. Think of it as a base of an equilateral triangle.
Move up (while drawing) one side. Stop halfway.
Move parallel to base till you reach the other side.
Move down the side to the base line.
Apply same procedure to all lines( Recursion ). The triangles on slanting lines must point inwards. The triangle on the horizontal line must point outwards.
The Sierpinski Arrowhead Curve
One Iteraton:
The Sierpinski Arrowhead Curve
Two Iteratons:
The Sierpinski Arrowhead Curve
Three Iteratons:
The Sierpinski Arrowhead Curve
Four and Five iterations skipped..
The Sierpinski Arrowhead Curve
Six iterations:
The Sierpinski Arrowhead Curve
In different colours:
Conclusions
Things to Note About Fractals
The final structure is completely different form what we started with.
Each smaller part is completely identical to the bigger part.
Recursion in Simple Language
Recursion mainly consists of following two steps:
Apply some procedure to a thing.
Start from Step 1 for all smaller parts.
In practice, we stop when we meet a base case.
Fractals are everywhere!
Romanesco Broccoli
Copper Crystals
Applications of recursion in Programming
Quick Sort
algorithm quicksort(A, lo, hi) is
if lo < hi then
p := partition(A, lo, hi)
quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi)
RecursiveCalls
Merge Sort
function merge_sort(list m)
// Base case. A list of zero or one elements is sorted, by definition.
if m is empty then
return m
// Recursive case. First, divide the list into equal-sized sublists
// consisting of the even and odd-indexed elements.
//omitted
// RECURSIVELY sort both sublists.
left := merge_sort(left)
right := merge_sort(right)
// Then merge the now-sorted sublists.
return merge(left, right)
Tower of Hanoi
function hanoi is:
input: integer n, such that n >= 1
1. if n is 1 then return 1
2. return [2 * [call hanoi(n-1)] + 1]
end hanoi
Interestingly, graphical representation of tower of hanoi is similar to that of sierpinski arrowhead curve
Recursive Call
Useful Resources
Original blog post
Code Used to make fractals
Snowflakes
Fractals in Nature
Wolfram Fractals Reference App
Think Python book
Thank You
Check out my blog for interesting new posts: singhcodes.wordpress.com
Follow me on twitter:
@gurpalsingh204