### Review: Recursion

• Recursion: a special divide and conquer problem solving technique

• I often to describe the recursive solution procedure as:

 the "lazy man's method to solve a problem....

• An un-scientific (hopefully more understandable) way to describe the recursive solution method:

• If you want to solve a problem of size n that has the recursive property, then:

 Hire (or delegate) someone else to solve the problem of size n − 1, size n − 2, etc... (You would then wait for that person to come back and give you the solution) When you receive the solution for the problem of size n − 1, size n − 2, etc..., you will then use these solutions to solve the original probem.

• The un-scientific way to look at recursion illustrated:

• Problem: compute 10!

• Solution plan: 10! = 10 × 9!

I will need 9!

• Delegate the problem: hire someone to solve the smaller problem

• Wait: in the mean time, I will wait for his answer....

• The hired hand finds the answer:

• I get back the answer from the hired hand:

• I can now find the answer to my problem:

• The general form of the recursive problem solving algorithm

• The recursive algorithm technique can be expressed in general as follows:

 ``` ReturnType solveProblem( n ) { if ( n is one of the base cases ) { return ( the readily available solution for that base case ); } else { /* ----------------------------------------------------------- Delegate the problem: hire someone to solve a (one or more) smaller problems ------------------------------------------------------------ */ sol1 = solveProblem ( n−1 ); // n−1 < n sol2 = solveProblem ( n−2 ); // n−2 < n ... /* ----------------------------------------------------------- Use the solutions of the smaller problems n−1, n−2, ... to solve my problem: ------------------------------------------------------------ */ mySol = Solve "problem(n)" using sol1, sol1, ... return ( mySol ); } } ```

• Where can you use the recursive algorithm technique ?

 The recursive algorithm technique can be used to solve problems that have the recursive property.

• Notes:

 I can only give you the general form of the recusive algorithm technique... because Each problem with the recursive property is solved differently...

• Recursive methods

• Consider the above algorithm:

 ``` ReturnType solveProblem( n ) { if ( n is one of the base cases ) { return ( the readily available solution for that base case ); } else { /* ----------------------------------------------------------- Delegate the problem: hire someone to solve a (one or more) smaller problems ------------------------------------------------------------ */ sol1 = solveProblem ( n−1 ); // n−1 < n sol2 = solveProblem ( n−2 ); // n−2 < n ... /* ----------------------------------------------------------- Use the solutions of the smaller problems n−1, n−2, ... to solve my problem: ------------------------------------------------------------ */ mySol = Solve "problem(n)" using sol1, sol1, ... return ( mySol ); } } ```

Notable fact:

 When you implement the algorithm as a Java method, the resulting method will invoke itself !!! We call these kind of methods: recursive methods

• Definition:

 Recursive method = a method that invokes itself (to solve a smaller version of the same problem and then use the solution to solve the original problem)

We will see some recursive methods in the next few webpages.

• Procedure to develop a recursive problem solving algorithm

• General procedure to develop a recursive problem solving algorithm :

Make sure that the problem has the recursive property (if the problem does not have the recursive property, then you cannot use recursion !!!)

1. Find out which smaller problems you need to use to solve the original problem

• Example:

 ``` factorial(n): smaller problem used = factorial(n-1) ```

2. Find out how to use the solutions of the smaller problems to solve the original problem.

 Depending on the problem, this step can be quite challenging....

3. Find the solutions for a sufficient number of the base cases.

Rule:

• If you used a smaller problem Problem(n−k) to solve the original problem Problem(n) , then you will need to find solutions for k base cases

Example:

 factorial(n) uses factorial(n-1) , so you will need the solution for 1 base case We usually use the base case: factorial(0) = 1

We will now look at some examples of the recursive algorithm technique.