recursion colin capham recursion – an introduction to the concept
TRANSCRIPT
RecursionColin Capham
Recursion – an introductionto the concept
RecursionColin Capham
Aim: • To understand the use of a simple recursion technique
Learning Objectives:
• To be able to explain simple recursion principles• Describe the process of “Divide and Conquer”• To understand basic “sub-problems”• Be able to solve an example of the “Towers of Hanoi”
RecursionColin Capham
What is Recursion?
Recursion is a common form of the general-purpose problem-solving technique called “divide and conquer”'.
1. divide P into several sub-problems, P1,P2,...Pn.
The principle of divide-and-conquer, is that you solve agiven problem P in 3 steps:
2. solve, however you can, each of the subproblems to get solutions S1...Sn.
3. Use S1...Sn to construct a solution to the original problem, P.
RecursionColin Capham
Lets look at an everyday example:
Suppose you want to carry a large pile of books to your car!
P = carry a pile of 50 books from your office to your car
Well, you can't even lift such a big pile, never mind carry it.
RecursionColin Capham
P1 contains 10 books;
So you split the original pile into 3 piles:
P2 contains 15 books;
P3 contains 25 books.
Solution…
RecursionColin Capham
Your plan, of course, is to carry each pile to the car separately,and then put them back together into one big pile once theyare all there.
This is a recursive “divide-and-conquer” strategy;you have divided the original problem P into 3 problemsof the same kind.
Now suppose you can directly solve P1 and P2 - they are small enough that you can carry them to the car.
So, P1 and P2 are now sitting out there by the car. Once you get P3 there, you will pile them up again!!
RecursionColin Capham
But P3 is too big to be carried.
You apply the same strategy recursively:divide P3 into smaller problems,solve them, and combine the resultsto make the solution for P3.
You divide P3 into two piles,P3a has 11 books, P3b has 14,and carry these separately to the car…
RecursionColin Capham
Then you put P3a back onto P3b to form the pile P3 there at the car.
P1 P2 P3
P3a
P3b
Now you have solved all the subproblems: P1, P2, P3 are sitting at the car.
RecursionColin Capham
You stack them into one big pile, and presto,you have solved the original problem.
RecursionColin Capham
Quick recap:
The principle of divide-and-conquer, is that you solve agiven problem P in 3 steps:
1. divide P into several sub-problems, P1,P2,...Pn.
2. solve, however you can, each of the subproblems to get solutions S1...Sn.
3. Use S1...Sn to construct a solution to the original problem, P.
Recursion is a common form of the general-purpose problem-solving technique called “divide and conquer”'.
RecursionColin Capham
Lets now look at another example of using recursion to solve a problem…
The Towers of Hanoi
An old legend tells of a Hindu templewhere the pyramid puzzle might havebeen used for the mental disciplineof young priests using recursion tosolve a problem.
RecursionColin Capham
The Towers of Hanoi is a puzzle, consisting of a collection of ringsof different sizes, and three posts mounted on a base.
At the beginning all the rings are on the left most post as shown,and the goal is to move them all to the rightmost post, by movingone ring at a time from one post to another.
The Towers of Hanoi
But, at no time may a larger ring be placed on top of a smaller one!
RecursionColin Capham
Student Activity:
Solve the Towers of Hanoi puzzle by breaking down the problem intosub-problems, and transfer your 4 disks from point A to point C.
Time limit: 5 mins
Who can solve the problem in the minimum amount of moves?
The objective of the puzzle is to move the entire stack at A to C,
obeying the following rules:
1. Only one disk may be moved at a time.
2. Each move consists of taking the upper disk from one of the pegs and placing it onto another peg, on top of the other disks that may already be present on that peg.
3. No disk may be placed on top of a smaller disk
RecursionColin Capham
Solution
A B C
15 Moves
RecursionColin Capham
Questions?
RecursionColin Capham