Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.

Similar presentations


Presentation on theme: "Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive."— Presentation transcript:

1 Recursion

2 2 Overview  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

3 3 Recursive 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

4 4 Recursive 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

5 5 Recursive 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

6 6 Tracing 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

7 7 Recursive 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

8 8 Designing 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

9 9 Recursive Factorial Function int fact(int num) { if(num <= 0) return 1; else return num * fact(num – 1); }

10 10 Largest 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; }

11 11 Towers of Hanoi Problem with 3 Disks

12 12 Towers of Hanoi: Three Disk Solution

13 13 Towers of Hanoi: Three Disk Solution

14 14 Towers of Hanoi: Recursive Algorithm void 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); }

15 15 Decimal to Binary void decToBin(int num, int base) { if(num > 0) { decToBin(num/base, base); cout<<num % base; }

16 16 Recursion 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

17 17 Backtracking 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

18 18 Backtracking 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

19 19 Solution to 8-Queens Puzzle

20 20 4-Queens Puzzle

21 21 4-Queens Tree


Download ppt "Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive."

Similar presentations


Ads by Google