Comp 110 Recursion
description
Transcript of Comp 110 Recursion
![Page 1: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/1.jpg)
COMP 110RECURSION
Instructor: Jason Carter
![Page 2: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/2.jpg)
2
RECURSION English
Return (Oxford/Webster) procedure repeating itself indefinitely or until condition
met, such as grammar rule (Webster) adequate: satisfactory satisfactory: adequate
recursion: recursion Mathematics
expression giving successive terms of a series (Oxford) Programming
Method calling itself. On a smaller problem. Alternative to loops.
![Page 3: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/3.jpg)
3
RECURSION Recursive Functions Recursive Procedures Number-based Recursion List-based Recursion
![Page 4: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/4.jpg)
4
FACTORIAL(N)
public static int factorial(int n) {int product = 1;while (n > 0) {
product *= n;n -= 1;
}return product;
}
public static void main (String[] args) {while (true) { // loop condition never false
int n = Console.readInt();if (n < 0)
break;System.out.println("factorial = " +
factorial(n));}
}
1*2*3*…*n
![Page 5: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/5.jpg)
5
DEFINING FACTORIAL(N)
Product of the first n numbers
1*2*3*…*n
factorial(0) = 1
factorial(1) = 1 = 1*factorial(0)
factorial(2) = 2*1 = 2*factorial(1)
factorial(3) = 3*2*1 = 3*factorial(2)
factorial(4) = 4*3*2*1 = 4*factorial(3)
factorial(n) = n*n-1*…*1 = n*factorial(n-1)
![Page 6: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/6.jpg)
6
DEFINING FACTORIAL(N)
factorial(n) = 1 if n == 0factorial(n) = n*factorial(n-1) if n > 0
![Page 7: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/7.jpg)
7
IMPLEMENTING FACTORIAL(N) (EDIT)
factorial(n) = 1 if n == 0factorial(n) = n*factorial(n-1) if n > 0
public static int factorial(int n) {…
}
![Page 8: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/8.jpg)
8
IMPLEMENTING FACTORIAL(N)
factorial(n) = 1 if n == 0factorial(n) = n*factorial(n-1) if n > 0
public static int factorial (int n) {if (n == 0)
return 1;if (n > 0)
return n*factorial(n-1); }
n < 0 ?
Function must return something
for all cases
Multiple return
Early return
![Page 9: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/9.jpg)
9
IMPLEMENTING FACTORIAL(N)
factorial(n) = 1 if n == 0factorial(n) = n*factorial(n-1) if n > 0
public static int factorial(int n) {if (n == 0)
return 1;else if (n < 0)
return factorial(-n);else
return n*factorial(n-1); }
Base case
Recursive Reduction
Steps
factorial(n) = - factorial(n) if n < 0
![Page 10: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/10.jpg)
10
RECURSIVE METHODS Should have base case(s) Recurse on smaller problem(s)
recursive calls should converge to base case(s)
![Page 11: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/11.jpg)
11
GENERAL FORM OF A RECURSIVE METHOD
if (base case 1 ) return solution for base case 1else if (base case 2) return solution for base case 2….else if (base case n) return solution for base case nelse if (recursive case 1) do some preprocessing recurse on reduced problem do some postprocessing…else if (recursive case m) do some preprocessing recurse on reduced problem do some postprocessing
![Page 12: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/12.jpg)
12
RECURSION VS. LOOPS (ITERATION)
public static int factorial(int n) {int product = 1;while (n > 0) {
product *= n;n -= 1;
}return product;
}public static int factorial(int n) {
if (n == 0) return 1;
else if (n < 0)return factorial(-n);
elsereturn n*factorial(n-1);
}
Implementation follows from definition
![Page 13: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/13.jpg)
13
TRACING RECURSIVE CALLS
Call Stack Locals
![Page 14: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/14.jpg)
14
TRACING RECURSIVE CALLS
Invocation n return valuefactorial(1) 1 ?factorial(2) 2 ?
Invocation n return valuefactorial(0) 0 ?factorial(1) 1 ?factorial(2) 2 ?
![Page 15: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/15.jpg)
15
TRACING RECURSIVE CALLS
Call StackLocals
![Page 16: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/16.jpg)
16
TRACING RECURSIVE CALLS
Invocation n return valuefactorial(0) 0 1factorial(1) 1 ?factorial(2) 2 ?
Invocation n return valuefactorial(0) 0 1factorial(1) 1 1factorial(2) 2 ?
Invocation n return valuefactorial(0) 0 1factorial(1) 1 1factorial(2) 2 2
![Page 17: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/17.jpg)
17
RECURSION PITFALLS
public static int factorial (int n) { return n*factorial(n-1); }
factorial(2)
2*factorial(1)
1*factorial(0)
0*factorial(-1)
-1*factorial(-2)
…
Infinite recursion! (stack overflow)
No base case
![Page 18: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/18.jpg)
18
RECURSION PITFALLSpublic static int factorial (int n) {if (n == 0)
return 1;else if (n < 0)
return factorial(-n);else
return n*factorial(n-1); }
factorial(2)
factorial(3)/3
factorial(4)/4
factorial(5)/5
factorial(6)/6
…
Infinite recursion!
Recurses on bigger problem
![Page 19: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/19.jpg)
19
RECURSIVE FUNCTIONS WITH MULTIPLE PARAMETERS
power(base, exponent)
baseexponent
base*base*base*…*base
Exponent # of timespower(0,
exponent) = 0power(1,
exponent) = 1power(2,
exponent)= 2*2*…*2 (exponent times)power(3,
exponent)= 3*3*…*3 (exponent times)
No pattern!
![Page 20: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/20.jpg)
20
RECURSIVE FUNCTIONS WITH MULTIPLE PARAMETERS (EDIT)
power(base, exponent)
baseexponent
base*base*base*…*base
Exponent # of times
![Page 21: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/21.jpg)
21
RECURSIVE FUNCTIONS WITH MULTIPLE PARAMETERS (EDIT)
power(base, exponent)
baseexponent
base*base*base*…*base
Exponent # of times
power(base, 0) = 1power(base, 1) = base*1 = base*power(base, 0)power(base, 2) =
base*base*1 = base*power(base, 1)power(base, exponent)
= base*power(base, exponent-1)
![Page 22: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/22.jpg)
22
DEFINING POWER(BASE, EXPONENT)power(base, exponent) = 1 if exponent <=
0if exponent > 0power(base,
exponent)= base*power(base, exponent-1)
public static int power(int base, int exponent) {if (n <= 0)
return 1;else
return base*power(base, exponent-1); }
![Page 23: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/23.jpg)
23
RECURSIVE PROCEDURES: GREET(N)
greet(0) greet(1)
print “hello”
greet(2)
print “hello”
print “hello”
greet(n)
print “hello”
print “hello”
print “hello”
n tim
es
![Page 24: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/24.jpg)
24
DEFINING GREET(N) (EDIT)
greet(0) greet(1)
print “hello”
greet(2)
print “hello”
print “hello”
greet(n)
print “hello”
print “hello”
print “hello”
n tim
es
![Page 25: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/25.jpg)
25
DEFINING GREET(N)
greet(0) greet(1)
print “hello”
greet(2)
print “hello”
print “hello”
greet(n)
print “hello”
print “hello”
print “hello”
n tim
es
do nothing;greet(0);
print “hello”;greet(1);
print “hello”;
greet(n-1);print “hello”;
![Page 26: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/26.jpg)
26
DEFINING GREET(N)greet(n) do nothing; if n <= 0
greet(n)
greet(n-1);print “hello”; if n > 0
![Page 27: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/27.jpg)
27
IMPLEMENTING GREET(N) (EDIT)greet(n) do nothing; if n <= 0
greet(n)
greet(n-1);print “hello”; if n > 0
![Page 28: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/28.jpg)
28
IMPLEMENTING GREET(N)greet(n) do nothing; if n <= 0
greet(n)
greet(n-1);print “hello”; if n > 0
public static void greet (int n) {if (n > 0) {
greet(n-1);System.out.println(“hello”);
}}
![Page 29: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/29.jpg)
29
LIST-BASED RECURSION: MULTIPLYLIST()
multiplyList() = 1 if remaining input is: -1multiplyList() = 2 if remaining input is: 2, -1multiplyList() = 12 if remaining input is: 2, 6, -1
multiplyList() = readNextVal() * multiplyList() if nextVal > 0
![Page 30: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/30.jpg)
30
LIST-BASED RECURSION: MULTIPLYLIST()
multiplyList() = 1 if nextVal < 0
multiplyList() = readNextVal() * multiplyList() if nextVal > 0
public static int multiplyList () {int nextVal = Console.readInt();if (nextVal < 0)
return 1;else
return nextVal*multiplyList();}
![Page 31: Comp 110 Recursion](https://reader035.fdocuments.us/reader035/viewer/2022062310/56816765550346895ddc4585/html5/thumbnails/31.jpg)
31
TRACING MULTIPLYLIST()
public static int multiplyList () {int nextVal = Console.readInt();if (nextVal < 0)
return 1;else
return nextVal*multiplyList();}
Invocation Remaining input Return valuemultiplyList() 2, 30, -1 ?Invocation Remaining input Return valuemultiplyList() 30, -1 ?multiplyList() 2, 30, -1 ?
Invocation Remaining input Return valuemultiplyList() -1 ?multiplyList() 30, -1 ?multiplyList() 2, 30, -1 ?
Invocation Remaining input Return valuemultiplyList() -1 1multiplyList() 30, -1 ?multiplyList() 2, 30, -1 ?
Invocation Remaining input Return valuemultiplyList() -1 1multiplyList() 30, -1 30multiplyList() 2, 30, -1 ?
Invocation Remaining input Return valuemultiplyList() -1 1multiplyList() 30, -1 30multiplyList() 2, 30, -1 60