Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Colin Capham Recursion – an introduction to the concept.

Similar presentations


Presentation on theme: "Recursion Colin Capham Recursion – an introduction to the concept."— Presentation transcript:

1 Recursion Colin Capham Recursion – an introduction to the concept

2 Recursion Colin 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”

3 Recursion Colin 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 a given 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.

4 Recursion Colin 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.

5 Recursion Colin Capham P1 contains 10 books; So you split the original pile into 3 piles: P2 contains 15 books; P3 contains 25 books. Solution…

6 Recursion Colin 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 they are all there. This is a recursive “divide-and-conquer” strategy; you have divided the original problem P into 3 problems of 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!!

7 Recursion Colin 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 results to 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…

8 Recursion Colin Capham Then you put P3a back onto P3b to form the pile P3 there at the car. P1P2 P3 P3a P3b Now you have solved all the subproblems: P1, P2, P3 are sitting at the car.

9 Recursion Colin Capham You stack them into one big pile, and presto, you have solved the original problem.

10 Recursion Colin Capham Quick recap: The principle of divide-and-conquer, is that you solve a given 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” '.

11 Recursion Colin 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 temple where the pyramid puzzle might have been used for the mental discipline of young priests using recursion to solve a problem.

12 Recursion Colin Capham The Towers of Hanoi is a puzzle, consisting of a collection of rings of 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 moving one 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!

13 Recursion Colin Capham Student Activity: Solve the Towers of Hanoi puzzle by breaking down the problem into sub-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

14 Recursion Colin Capham Solution A BC 15 Moves

15 Recursion Colin Capham Questions?

16 Recursion Colin Capham


Download ppt "Recursion Colin Capham Recursion – an introduction to the concept."

Similar presentations


Ads by Google