### Introduction to Recursion

• Previously discussed: the divide and conquer problem solving technique

• Divide and conquer:

• Divide the original problem into a number of (smaller and easier) sub-problems

• Solve each sub-problem with an algorithm (i.e., write an algorithm for each sub-problem)

• Illustration:

• In fact, we have just applied this technique to the Text formatting problem:

 We divided the Text Formatting problem into 3 different sub-problems and We conquered each sub-problem with one (small) algorithm (written as a method)

• The recursive property

• Some problems (especially in Mathematics) are defined in terms of itself

 Recursive problem = a problem that is defined with one or more smaller problems of the same kind

• Example: the factorial of the number n

 ``` n! = n × (n − 1)! where: 0! = 1 ```

Notice that:

• The problem of computing n! contains a another smaller problem ((n−1)!) of the same nature:

 ``` n! = n × (n − 1)! ```

• It's like a russian doll:

• Illustration: compute factorial of 10:

 ``` 10! = 10 × 9! ```

To compute 10!, we need to value of 9!

 We must solve another (smaller) factorial problem

• Now, suppose that you know (e.g., someone told you) that:

 ``` 9! = 362880 ```

Then, we can solve the original problem (10!) very easily:

 ``` 10! = 10 × 362880 = 3628800 ```

• The recursive property:

• Let problem X(n) denote a certain problem of a given size n

• A problem X(n) has the recursive property if

 The problem X(n) can be solved using the solution of one or more smaller problems X(n−1), X(n−2), ... A sufficient number of base (simple) cases of the problem can be solved readily. (The number of base case solutions needed depends on how many smaller problems X(n−1), X(n−2), ... are used to solve the original problem X(n))

Example: the factorial(n) problem has the recursive property

• The problem factorial(n) can be solved using the solution of one smaller problem   factorial(n−1)

• Because one smaller problem is used to solve factorial(n), we need one base case

The base (simple) case n = 0 of the factorial problem can be solved readily:

 ``` factorial(0) = 1 ```

• The number of base cases (already solved) needed:

• In general, if we use the solution for Problem(n−k) to formulate the solution for the original problem Problem(n):

 ``` Problem(n) = ....... Problem(n − k) ```

then we need solutions for k base case.

• Recursion: a special divide and conquer problem solving technique

• Because there a smaller problem of the same kind embedded inside a recursion problem, there is special divide and conquer solution procedure for recursive problem

I like to describe this 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

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.