recursion. recursive definitions a recursive definition is one which uses the word being defined in...
Post on 19-Dec-2015
225 views
TRANSCRIPT
![Page 1: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/1.jpg)
Recursion
![Page 2: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/2.jpg)
Recursive Definitions
A recursive definition is one which uses the word being defined in the definition
Not always useful: for example, in dictionary definitions
In programming it can be very useful…
![Page 3: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/3.jpg)
A Recursive Definition
How do we define a LIST of numbers? 2, 65, -34, 4 1, 2, 4, 8, 16, 32
A LIST is either: <number> <number>, LIST
The concept of a LIST is used to define itself
![Page 4: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/4.jpg)
Breaking Down the Definition The recursive part is used several times:
number comma LIST 2 , 65, -34, 4
number comma LIST 65 , -34, 4
number comma LIST -34 , 4
number 4
![Page 5: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/5.jpg)
Form of a Recursive Definition The base case:
LIST ::= number The recursive case:
LIST ::= number, LIST Without the base case, we have infinite
recursion … usually a problem in programming
![Page 6: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/6.jpg)
A Recursive Function
Compute n!, pronounced “n factorial” This is the product of all numbers up to and
including n 5! = 5 * 4 * 3 * 2 * 1 = 120 n! = n * (n-1) * (n-2) * … * 2 * 1
![Page 7: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/7.jpg)
A Recursive Function (cont’d) Think recursively…what is the base case?
what is ‘1!’ ? How can we write n! in terms by using (n-1)! The definition:
1! = 1 n! = n * (n-1)!
![Page 8: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/8.jpg)
A Recursive Function (cont’d) Eventually, the base case is reached:
5! = 5 * 4!4 * 3!
3 * 2!
2 * 1!
1
= 2
= 6
= 24
= 120
![Page 9: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/9.jpg)
Recursive Programming
Code in the body of a method can call other methods e.g. In Quadratic, root1 calls discrim
There is no reason why a method can not call itself e.g. In the body of MyFunc(), we can call
MyFunc()
![Page 10: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/10.jpg)
Recursive Method Declarations A recursive method declaration must define
both the base case and the recursive case Each method call has it’s own variables and
parameters Flow of control is unchanged:
Method executes, then returns control to the calling method
![Page 11: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/11.jpg)
When Do We Want Recursion? There are many problems that are easier to
solve with recursive functions A problem that can be solved in pieces is a
candidate for a recursive algorithm Chop the function into one or more smaller parts Solve each part recursively Combine the parts to a whole solution
e.g. reversing a string
![Page 12: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/12.jpg)
Reversing a String (informal)
Start with “CMPT” Return reverse(“MPT”) + ‘C’ Base case:
reverse(“”) = “” Recursive case:
reverse(char + STRING)
= reverse(STRING) + char
![Page 13: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/13.jpg)
Reversing a String (code)class StringRev { public static String reverse(String s)
{ if ( s.length() == 0 ) return s; else return reverse(s.substring(1)) + s.charAt(0); }
public static void main(String[] args){
System.out.println( reverse(“CMPT") ); }}
![Page 14: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/14.jpg)
Analyzing The Method
reverse(“CMPT”) returns reverse(“MPT”) + ‘C’
reverse(“MPT”) returns reverse (“PT”) + ‘M’
reverse(“PT”) returns reverse(“T”) + ‘P’
reverse(“T”) returns reverse(“”) + ‘T’
So:
reverse(“CMPT”) = reverse(“MPT”) + ‘C’= (reverse(“PT”) + ‘M’) + ‘C’= ((reverse(“T”) + ‘P’) + ‘M’) + ‘C’
= (((reverse(“”) + ’T’) + ‘P’) + ‘M’) + ‘C’
![Page 15: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/15.jpg)
Understanding Recursion
… but you probably don’t need to worry too much about those details
When trying to understand a recursive algorithm assume the recursive calls return the right thing look at how that result is used to build the whole
result
![Page 16: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/16.jpg)
Defining Recursive Functions
The idea: Take the original problem (reverse “CMPT”) Find a smaller subproblem (reverse “MPT”) Define solution from subproblem (append ‘C’) Combine for general solution
If you can do this, you are pretty much done Key words “smaller subproblem”
![Page 17: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/17.jpg)
“Smaller”
You can’t keep calling reverse(“CMPT”) in the recursive part infinite recursion reverse(“CMPT”)-> reverse(“CMPT”)->
reverse(“CMPT”)-> reverse(“CMPT”)->….. The recursive step MUST reduce the problem
size
![Page 18: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/18.jpg)
“Subproblem”
You must be able to split the problem to make a recursive call subproblems getting smaller is good e.g. iteratively taking characters from “CMPT”
When the subproblem gets “obvious” then you have the base case reverse(“”) = “”
Every input MUST end with the base case
![Page 19: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/19.jpg)
Recursive Programming
Just because we “can” find a recursive solution, doesn’t mean that we should
Consider summing the numbers 1+…+n What is the base case?
sum(1) = 1 What is the recursive case?
sum(n) = n + sum (n-1)
![Page 20: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/20.jpg)
Recursive Programming
// This method returns the sum of 1 to numpublic int sum (int num){ int result;
if (num == 1) result = 1; else result = num + sum (n-1);
return result;}
![Page 21: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/21.jpg)
Recursive Programming
The iterative version is “easier to understand” Think about the way you usually compute a sum
Sometimes you also want to consider “running time” Is the iterative version faster or slower than the
recursive version? You need to decide on a case-by-case basis
if recursion provides the best solution
![Page 22: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/22.jpg)
Indirect Recursion
So far we have looked at direct recursion – when a method calls itself
A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again
This is indirect recursion, and it requires the same care
![Page 23: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/23.jpg)
Indirect Recursion
m1 m2 m3
m1 m2 m3
m1 m2 m3
![Page 24: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/24.jpg)
More Examples
Maze traversal (in text) Towers of Hanoi (in text) Calculate xy for positive integer y
This example has several recursive solutions…. There are several ways to break it into
subproblems The choice you make can have an impact on
efficiency
![Page 25: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/25.jpg)
Powers (version 1)
xy = x * xy-1
base case, x0 =1public static long pow(long x, long y)
{
if(y==0)
return 1;
else
return (x * pow(x,y-1));
}
![Page 26: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/26.jpg)
Powers (version 1)
xy = xy/2 * xy/2 , base case, x0 =1public static long pow(long x, long y){
long half;if(y==0)
return 1;else if(y%2==0) // y even{
half = pow(x,y/2);return half * half;
}else{
half = pow(x,(y-1)/2);return (half * half * x);
}}
![Page 27: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/27.jpg)
Which one is better?
Version 1 requires less code, but… Version 1 requires y steps to run Version 2 requires log2(y) steps
This is much faster when y gets big These differences can matter…
Computing 108 with Version 1 runs out of memory on a reasonable machine
Computing 108 with Version 2 takes 0.2 sec on same machine
![Page 28: Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful: for example, in](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649d2b5503460f94a00c93/html5/thumbnails/28.jpg)
Running Time
We consider this kind of argument in detail later… when we discuss “running time”
For now, it is sufficient to remark that you want to balance elegance and efficiency