Recursion - UMD

Preview:

Citation preview

Recursion

CMSC132,Summer2016Object-OrientedProgrammingII

AnwarMamat

Recursion

• Whenonefunctioncallsitselfdirectlyorindirectly

• amethodwherethesolutiontoaproblemdependsonsolutionstosmallerinstancesofthesameproblem.

Factorial

5!=5*4!

4=4*3!

3!=3*2!

2!=2*1!

1!=1

2*1

3 *2

4 *65 *24=120

5!=?

Definition:

GreatestCommonDivisor

• gcd:FindlargestintegerdthatevenlydividesintopandqEuclid'salgorithm.[300BCE]

GreatestCommonDivisor

• gcd:FindlargestintegerdthatevenlydividesintopandqEuclid'salgorithm.[300BCE]

TowersofHanoi

TowersofHanoi• Moveallthediscsfromtheleftmostpegtotherightmostone.– Onlyonediscmaybemovedatatime.– Adisccanbeplacedeitheronemptypegorontopofalargerdisc

TowersofHanoiLegend

• Q.Isworldgoingtoend(accordingtolegend)?– 64goldendiscson3diamondpegs.–Worldendswhencertaingroupofmonksaccomplishtask.

• Q.Willcomputeralgorithmshelp

Let’smovedisks

A B C

?

Moveonedisk

A B C

Movetwodisks

A B C

Movethreedisks

A B C

Movefourdisks

A B C

Movendisks

1.Moven-1disksfromA->B

2.MovethelargestdisktoC

3.Moven-1disksfromB->C

A B C

TowersofHanoi:RecursiveSolutionpublic class TowersOfHanoi {

public static void solve(int n, String A, String B, String C) {if (n == 1) {

System.out.println(A + " -> " + C);} else {

solve(n - 1, A, C, B);System.out.println(A + " -> " + C);solve(n - 1, B, A, C);

}}public static void main(String[] args) {

int discs = 3;solve(discs, "A", "B", "C");

}}

A B C

Remarkablepropertiesofrecursivesolution

• Takes2n stepstosolvendiscproblem.• Takes585billionyearsforn=64(atrateof1discpersecond).

• Reassuringfact:anysolutiontakesatleastthislong

FibonacciNumberFibonaccinumbers:0,1,1,2,3,5,8,13,21,34

FibonacciNumberFibonaccinumbers:0,1,1,2,3,5,8,13,21,34

InefficientRecursionFibonaccinumbers:0,1,1,2,3,5,8,13,21,34

Memoized VersionoftheFibonaccipublic Map<Integer, Integer> fibo;

public int fib(int n){

int f1=0,f2=0;

if( (n == 1)|| (n == 2)) return 1;

else{

if(fibo.containsKey(n-1)){

f1 = fibo.get(n-1);

}else{

f1 = fib(n-1);

fibo.put(n-1,f1);

}

if(fibo.containsKey(n-2)){

f2 = fibo.get(n-2);

}else{

f2 = fib(n-2);

fibo.put(n-2,f2);

}

return f2+f1;

}

Recommended