Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case...
-
Upload
emory-morton -
Category
Documents
-
view
225 -
download
0
description
Transcript of Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case...
![Page 1: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/1.jpg)
Recursion
![Page 2: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/2.jpg)
2Overview
Learn about recursive definitions Explore the base case and the general case of a recursive
definition Discover recursive algorithms Learn about recursive functions Explore how to use recursive functions to implement
recursive algorithms Learn about recursion and backtracking
![Page 3: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/3.jpg)
3Recursive Definitions
Recursion The process of solving a problem by reducing it to a
smaller versions of itself Recursive definition
Definition in which a problem is expressed in terms of a smaller version of itself
Has one or more base cases
![Page 4: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/4.jpg)
4Recursive Definitions
Recursive algorithm Algorithm that finds the solution to a given problem by reducing
the problem to smaller versions of itself Has one or more base cases Can be implemented using recursive functions
Recursive function function that calls itself
Base case Case in recursive definition (or function) in which the solution is
obtained directly (without making a recursive function call) Halts the recursive function calling Without a base case, recursion would continue endlessly
![Page 5: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/5.jpg)
5Recursive Definitions
General solution Breaks problem into smaller versions of itself
General case Case in recursive definition in which a smaller version
of itself is called Must eventually be reduced to a base case
![Page 6: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/6.jpg)
6Tracing a Recursive Function
Every recursive call has its own code own set of parameters own set of local variables
After completing recursive call: Control goes back to calling environment Recursive call must execute completely before control goes back
to previous call Execution in previous call begins from point immediately
following recursive call
![Page 7: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/7.jpg)
7Recursive Definitions
Directly recursive: a function that calls itself Indirectly recursive: a function that calls another function
and eventually results in the original function call Tail recursive function: recursive function in which the
last statement executed is the recursive call Infinite recursion: the case where every recursive call
results in another recursive call
![Page 8: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/8.jpg)
8Designing Recursive Functions
Understand problem requirements Determine limiting conditions Identify base cases Provide direct solution to each base case Identify general case(s) Provide solutions to general cases in terms of
smaller versions of itself
![Page 9: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/9.jpg)
9Recursive Factorial Function
int fact(int num){ if(num <= 0) return 1; else return num * fact(num – 1);}
![Page 10: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/10.jpg)
10Largest Value in Array
int largest(const int list[], //in int lowerIndex, //in int upperIndex //in){ int max; if(lowerIndex == upperIndex) return list[lowerIndex]; else{ max = largest(list, lowerIndex + 1, upperIndex); if(list[lowerIndex] >= max) return list[lowerIndex]; else return max; }}
![Page 11: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/11.jpg)
11Towers of Hanoi Problem with 3 Disks
![Page 12: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/12.jpg)
12Towers of Hanoi: Three Disk Solution
![Page 13: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/13.jpg)
13Towers of Hanoi: Three Disk Solution
![Page 14: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/14.jpg)
14Towers of Hanoi: Recursive Algorithmvoid moveDisks(int count, int source, int target,
int spare){ if(count > 0) { moveDisks(count - 1, source, spare, target); cout<<"Move disk "<<count<<“ from "<<source <<“ to "<<target<<"."<<endl; moveDisks(count - 1, spare, target, source); }}
![Page 15: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/15.jpg)
15Decimal to Binary
void decToBin(int num, int base){ if(num > 0) { decToBin(num/base, base); cout<<num % base; }}
![Page 16: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/16.jpg)
16Recursion or Iteration?
Two ways to solve particular problem Iteration Recursion
Iterative control structures: uses looping to repeat a set of statements
Tradeoffs between two options Sometimes recursive solution is easier Recursive solution is often slower
![Page 17: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/17.jpg)
17Backtracking Algorithm
Attempts to find solutions to a problem by constructing partial solutions
Makes sure that any partial solution does not violate the problem requirements
Tries to extend partial solution towards completion
![Page 18: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/18.jpg)
18Backtracking Algorithm
If it is determined that partial solution would not lead to solution partial solution would end in dead end algorithm backs up by removing the most recently
added part and then tries other possibilities
![Page 19: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/19.jpg)
19Solution to 8-Queens Puzzle
![Page 20: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/20.jpg)
204-Queens Puzzle
![Page 21: Recursion. 2 Overview Learn about recursive definitions Explore the base case and the general case of a recursive definition Discover recursive.](https://reader036.fdocuments.in/reader036/viewer/2022062311/5a4d1af27f8b9ab05997f20f/html5/thumbnails/21.jpg)
214-Queens Tree