Download presentation
Presentation is loading. Please wait.
Published byRobert Harrell Modified over 9 years ago
1
Lecture 10 Recursion CSE225: Data Structures
2
2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; }
3
3 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } main
4
4 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistance (0,0,3,4)
5
5 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistancesqrt (0,0,3,4) (25)
6
6 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistancesqrt (0,0,3,4) (25) (5)
7
7 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistance (0,0,3,4) (5)
8
Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. Recursion can be implemented using a recursive function (a function invoking itself, either directly or indirectly). Recursion can be used as an alternative to iteration. It is an important and powerful tool in problem solving and programming. It is a programming technique that naturally implements the divide- and-conquer problem solving methodology.
9
n! = n * (n-1) * (n-2) * … * 2 * 1for any integer n>0 0! = 1 Iterative Definition in C: fval = 1; for (i = n; i >= 1; i--) fval = fval * i; Factorial – Iterative Definition
10
To define n! recursively, n! must be defined in terms of the factorial of a smaller number. Observation (problem size is reduced): n! = n * (n-1)! Base case:0! = 1 We can reach the base case, by subtracting 1 from n if n is a positive integer. Recursive Definition: n! = 1 if n = 0 n! = n*(n-1)! if n > 0 Factorial – Recursive Definition
11
1.One or more simple cases of the problem (called the stopping cases or base case) have a simple non-recursive solution. 2.The other cases (general cases) of the problem can be reduced (using recursion) to problems that are closer to stopping cases. 3.Eventually the problem can be reduced to base cases only, which are relatively easy to solve. In general: if (base case) solve it else reduce the problem using recursion // general case The Nature of Recursion
12
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __
13
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __
14
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __
15
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __ Calculate 1! 1! = 1 * (1-1)! 1! = 1 * 0! 1! = 1 * __
16
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __ Calculate 1! 1! = 1 * (1-1)! 1! = 1 * 0! 1! = 1 * __ Calculate 0! 0! = 1
17
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __ Calculate 1! 1! = 1 * (1-1)! 1! = 1 * 0! 1! = 1 * 1 1! = 1
18
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * 1 2! = 2
19
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * 2 3! = 6
20
Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * 6 4! = 24
21
Recursive Factorial Function // Computes the factorial of a nonnegative integer. // Precondition: n must be greater than or equal to 0. // Postcondition: Returns the factorial of n; n is unchanged. int Factorial(int n) { if (n ==0) return (1); else return (n * Factorial(n-1)); }
22
Tracing Factorial(4) Factorial(4)
23
Tracing Factorial(4) Factorial (4) call Factorial(4)
24
Tracing Factorial(4) Factorial (4) Factorial (3) call Factorial(4)= 4 * Factorial(3)
25
Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2))
26
Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1)))
27
Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0))))
28
Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1)))
29
Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1))
30
Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 2*1=2 call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2)
31
Tracing Factorial(4) Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 2*1=2 3*2=6 call
32
Tracing Factorial(4) Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24 Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 2*1=2 3*2=6 4*6=24 final answer call
33
A stack is used to keep track of function calls. Whenever a new function is called, all its parameters and local variables are pushed onto the stack along with the memory address of the calling statement (this gives the computer the return point after execution of the function) A Couple of Things You Should Know
34
If the recursion never reaches the base case, the recursive calls will continue until the computer runs out of memory and the program crashes. Experienced programmers try to examine the remains of a crash. The message “stack overflow error” or “heap storage exhaustion” indicates a possible runaway recursion. When programming recursively, you need to make sure that the algorithm is moving toward the base case. Each successive call of the algorithm must be solving a simpler version of the problem. Any recursive algorithm can be implemented iteratively, but sometimes only with great difficulty. However, a recursive solution will always run more slowly than an iterative one because of the overhead of opening and closing the recursive calls. Pitfalls of Recursion
35
Fibonacci’s Problem 35 “Fibonacci” ( Leonardo de Pisa) 1170-1240
36
Fibonacci’s Problem
37
Rabbit Rules 1.All pairs of rabbits consist of a male and female 2.One pair of newborn rabbits is placed in hutch on January 1 3.When this pair is 2 months old they produce a pair of baby rabbits 4.Every month afterwards they produce another pair 5.All rabbits produce pairs in the same manner 6.Rabbits don’t die 37
38
Fibonacci’s Problem How many pairs of rabbits will there be 12 months later? 38
39
Jan 1 Feb 1 Mar 1 2 0 1 0
40
40 Apr 1 May 1 Mar 1 2 3 4 0 1 2 1 0 0 0
41
Apr 1 May 1 June 1 310 4210 0 5321 1
42
Pairs this month In General, Pairs last monthPairs of newborns =+ Pairs this monthPairs last month Pairs 2 months ago =+
43
Fibonacci Numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive definition: F(0) = 0; F(1) = 1; F(number) = F(number-1)+ F(number-2); 43
44
Fibonacci Numbers int Fibonacci(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); }
45
Fibonacci Numbers int Fibonacci(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } Base case
46
Fibonacci Numbers int Fibonacci(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } General case
47
F(5) Return F(4)+F(3) F(4) Return F(3)+F(2) F(3) Return F(2)+F(1) F(2) Return F(1)+F(0) F(1) Return 1 F(0) Return 0 F(2) Return F(1)+F(0) F(0) Return 0 F(1) Return 1 F(0) Return 0 F(2) Return F(1)+F(0) F(1) Return 1 F(3) Return F(2)+F(1) F(1) Return 1 F(1) Return 1 5 2 10 111010 1121 3 Tracing Fibonacci(5)
48
Given n things, how many different sets of size k can be chosen? with base cases: Another Example: n choose r (combinations)
49
int Combinations(int n, int r) { if(r == 1) // base case 1 return n; else if (n == r) // base case 2 return 1; else //general case return(Combinations(n-1, r-1) + Combinations(n-1, r)); } n choose r (Combinations)
50
Tracing Combinations(4,3)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.