recursion - see recursion

31
Recursion - see Recursion

Upload: loe

Post on 12-Jan-2016

98 views

Category:

Documents


0 download

DESCRIPTION

Recursion - see Recursion. Recursion. We know that: We can define classes We can define methods on classes Mehtods can call other methods But…can a method call itself…?. Recursion. public void callMe() { System.out.println(”Hello”); callMe(); }. Recursion. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursion - see Recursion

Recursion- see Recursion

Page 2: Recursion - see Recursion

RHS – SWC 2

Recursion

• We know that:– We can define classes– We can define

methods on classes– Mehtods can call other

methods

• But…can a method call itself…?

Page 3: Recursion - see Recursion

RHS – SWC 3

Recursion

public void callMe()

{

System.out.println(”Hello”);

callMe();

}

Page 4: Recursion - see Recursion

RHS – SWC 4

Recursion

• Previous method definition was legal, but hardly useful…

• Just calling the same method will result in an infinite loop

• BUT what if we– Supply a parameter to the method– Change the parameter in each call– Stop calling ourselves for some specific value

Page 5: Recursion - see Recursion

RHS – SWC 5

Recursion

public void callMe(int calls)

{

if (calls > 0)

{

System.out.println(”Hello”);

int fewercalls = calls – 1;

callMe(fewercalls);

}

}

Page 6: Recursion - see Recursion

RHS – SWC 6

Recursion

• Calling methods like this is often called recursion

• The previous example could easily be rewritten as a ”traditional” loop, and would even be more efficient

• However, quite a lot of problems can be solved very elegantly by recursion

Page 7: Recursion - see Recursion

RHS – SWC 7

Recursion

• Example: the factorial function

• The factorial function F(n) is defined as:

F(n) = n × (n-1) × (n-2) × … × 2 × 1

• But you could also define F(n) as:

F(n) = n × F(n-1)

Page 8: Recursion - see Recursion

RHS – SWC 8

Recursion

public void factorial(int n)

{

int result = 1;

for (int val = 1; val <= n; val++)

{

result = result * val;

}

return result

}

Page 9: Recursion - see Recursion

RHS – SWC 9

Recursion

public void factorial(int n)

{

if (n <= 1)

return 1;

else

return (n * factorial(n-1));

}

Page 10: Recursion - see Recursion

RHS – SWC 10

Thinking recursively

• Thinking in terms of recursion may seem quite confusing at first

• However, one should try not to think about how it works in detail

• Think in terms of how a problem can be solved, by solving ”simpler” versions of the same problem

Page 11: Recursion - see Recursion

RHS – SWC 11

Thinking recursively

• Solving a problem by recursion:– Control step: Does the problem have a

simple solution?– Division step: Split the problem into a simpler

problem, plus a residual– Solution step: Solve the simpler problem– Combination step: Combine the solution to

the simpler problem with the residual, in order to solve the original problem

Page 12: Recursion - see Recursion

RHS – SWC 12

Thinking recursively

• Solving the factorial function by recursion:

• Control step: – Is n < 1? If so, the result is 1

• Division step: – Original problem: F(n)– Simpler problem: F(n-1)– Residual: n

Page 13: Recursion - see Recursion

RHS – SWC 13

Thinking recursively

• Solving the factorial function by recursion:

• Solution step: – Solve F(n-1) (go to top…)

• Combination step:– Combine by multiplying F(n-1) with n

Page 14: Recursion - see Recursion

RHS – SWC 14

Thinking recursively

public void factorial(int n)

{

if (n <= 1)

return 1;

else

return (n * factorial(n-1));

}

Control Step

Solution Step

Combination Step

Division Step

Page 15: Recursion - see Recursion

RHS – SWC 15

Thinking recursively

• A slightly harder problem is string permutations:– Given a string of text, find all possible

permutations of the characters in the string– A string of length n will have n! different

permutations

Page 16: Recursion - see Recursion

RHS – SWC 16

Thinking recursively

• The string ”cat” has 3! = 6 permutations:– ”cat”– ”cta”– ”act”– ”atc”– ”tac”– ”tca”

Page 17: Recursion - see Recursion

RHS – SWC 17

Thinking recursively

• Solving string permutations by recursion:

• Control step: – Does the string have length 1? If so, the result

is the string itself

Page 18: Recursion - see Recursion

RHS – SWC 18

Thinking recursively

• Solving string permutations by recursion:

• Division step: – For each character c in the string:

• Let c be the residual• Remove c from the original string. • The resulting string is then the simpler problem

Page 19: Recursion - see Recursion

RHS – SWC 19

Thinking recursively

• Solving string permutations by recursion:

• Solution step: – For each string S generated during the

division step:• Find all permutations of S

Page 20: Recursion - see Recursion

RHS – SWC 20

Thinking recursively

• Solving string permutations by recursion:

• Combination step: – For each string S generated during the

solution step:• Combine S with the residual character c, by adding

c to the front of S (permutation = c + S)• Add the permutation to the result set

Page 21: Recursion - see Recursion

RHS – SWC 21

Thinking recursively

• Solving string permutation by recursion is not trivial…

• …but try to write an algorithm for string permutation without using recursion!

• Trust that it works!

• When there is a simple solution to simple inputs, and the problem can be solved by solving it for simpler inputs, it will work!

Page 22: Recursion - see Recursion

RHS – SWC 22

Recursive helper methods

• It is sometimes easier to solve a more general problem by recursion

• Example: How to find a palindrome (a string which is equal to its own reverse)

• Possible interface to a Sentence class:– Sentence(String theSentence)– boolean isPalindrome()

Page 23: Recursion - see Recursion

RHS – SWC 23

Recursive helper methodspublic boolean isPalindrome()

{

int len = text.length();

if (len < 2)

return true;

if ((text.substring(0,1).equals(text.substring(len-1,len)))

{

Sentence newSen = new Sentence(text.substring(1,len-1));

return newSen.isPalindrome();

}

else

return false;

}

Page 24: Recursion - see Recursion

RHS – SWC 24

Recursive helper methods

• Previous implementation works, but is somewhat inefficient

• We create a lot of Sentence objects

• Let us include a helper method, that checks if a substring is a palindrome:

boolean isPalindrome(int start, int end)

Page 25: Recursion - see Recursion

RHS – SWC 25

Recursive helper methodspublic boolean isPalindrome(int start, int end)

{

int len = end – start;

if (len < 2)

return true;

if ((text.substring(start,start+1).equals(

text.substring(end-1,end)))

return isPalindrome(start+1,end-1);

else

return false;

}

Page 26: Recursion - see Recursion

RHS – SWC 26

Recursive helper methods

• Finally, implement original method by using the helper method:

public boolean isPalindrome()

{

return text.isPalindrome(0, lext.length() – 1);

}

Page 27: Recursion - see Recursion

RHS – SWC 27

Efficiency of recursion

• Recursion is a very elegant principle, but not always the correct strategy…

• Can result in very inefficient algorithms, if care is not taken

• Common pitfall is to calculate identical values over and over

Page 28: Recursion - see Recursion

RHS – SWC 28

Efficiency of recursion

• Example: Calculating Fibonacci numbers– Fib(1) = 1– Fib(2) = 1– Fib(n) = Fib(n-1) + Fib(n-2)

• Looks like a recursive solution is a no-brainer…

Page 29: Recursion - see Recursion

RHS – SWC 29

Efficiency of recursion

public long fib(int n)

{

if (n <= 2)

return 1;

else

return (fib(n-1) + fib(n-2));

}

Page 30: Recursion - see Recursion

RHS – SWC 30

Efficiency of recursion

• Method does work, but is very inefficient

• We calculate same values over and over

Fib(n)

Fib(n-1) Fib(n-2)

Fib(n-2) Fib(n-3)

Page 31: Recursion - see Recursion

RHS – SWC 31

Recursion summary

• Recursion is a powerful and elegant tool for algorithm development

• Many algoritms are very easy to under-stand, if you understand recursion

• No silver bullet – recursive algorithms can be very slow

• Always consider (iterative) alternatives