Chapter 6

31
Data Structures Using C++ 1 Chapter 6 Recursion

description

Chapter 6. Recursion. Chapter Objectives. 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 - PowerPoint PPT Presentation

Transcript of Chapter 6

Page 1: Chapter 6

Data Structures Using C++ 1

Chapter 6

Recursion

Page 2: Chapter 6

Data Structures Using C++ 2

Chapter Objectives

• 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: Chapter 6

Data Structures Using C++ 3

Recursive Definitions

• Recursion– Process of solving a problem by reducing it to

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: Chapter 6

Data Structures Using C++ 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– Implemented using recursive functions

• Recursive function– function that calls itself

• Base case– Case in recursive definition in which the solution is

obtained directly – Stops the recursion

Page 5: Chapter 6

Data Structures Using C++ 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

Page 6: Chapter 6

Data Structures Using C++ 6

Tracing a Recursive Function

Recursive function– Has unlimited copies of itself– Every recursive call has

• its own code• own set of parameters• own set of local variables

Page 7: Chapter 6

Data Structures Using C++ 7

Tracing a Recursive Function

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 8: Chapter 6

Data Structures Using C++ 8

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

Page 9: Chapter 6

Data Structures Using C++ 9

Designing Recursive Functions

• Understand problem requirements

• Determine limiting conditions

• Identify base cases

Page 10: Chapter 6

Data Structures Using C++ 10

Designing Recursive Functions

• Provide direct solution to each base case

• Identify general case(s)

• Provide solutions to general cases in terms of smaller versions of itself

Page 11: Chapter 6

Data Structures Using C++ 11

Recursive Factorial Function

int fact(int num)

{

if(num == 0)

return 1;

else

return num * fact(num – 1);

}

Page 12: Chapter 6

Data Structures Using C++ 12

Recursive Factorial Trace

Page 13: Chapter 6

Data Structures Using C++ 13

Recursive Implementation: Largest Value in Array

int largest(const int list[], int lowerIndex, int upperIndex){ int max; if(lowerIndex == upperIndex) //the size of the sublist is 1 return list[lowerIndex]; else { max = largest(list, lowerIndex + 1, upperIndex); if(list[lowerIndex] >= max) return list[lowerIndex]; else return max; }}

Page 14: Chapter 6

Data Structures Using C++ 14

Execution of largest(list, 0, 3)

Page 15: Chapter 6

Data Structures Using C++ 15

Recursive Fibonacci

int rFibNum(int a, int b, int n){ if(n == 1) return a; else if(n == 2) return b; else return rFibNum(a, b, n - 1) + rFibNum(a, b, n - 2);}

Page 16: Chapter 6

Data Structures Using C++ 16

Execution of rFibonacci(2,3,5)

Page 17: Chapter 6

Data Structures Using C++ 17

Towers of Hanoi Problem with Three Disks

Page 18: Chapter 6

Data Structures Using C++ 18

Towers of Hanoi: Three Disk Solution

Page 19: Chapter 6

Data Structures Using C++ 19

Towers of Hanoi: Three Disk Solution

Page 20: Chapter 6

Data Structures Using C++ 20

Towers of Hanoi: Recursive Algorithm

void moveDisks(int count, int needle1, int needle3, int needle2)

{ if(count > 0) { moveDisks(count - 1, needle1, needle2,

needle3); cout<<"Move disk "<<count<<“ from "<<needle1 <<“ to "<<needle3<<"."<<endl; moveDisks(count - 1, needle2, needle3,

needle1); }}

Page 21: Chapter 6

Data Structures Using C++ 21

Decimal to Binary: Recursive Algorithm

void decToBin(int num, int base)

{

if(num > 0)

{

decToBin(num/base, base);

cout<<num % base;

}

}

Page 22: Chapter 6

Data Structures Using C++ 22

Execution of decToBin(13,2)

Page 23: Chapter 6

Data Structures Using C++ 23

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

Page 24: Chapter 6

Data Structures Using C++ 24

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

Page 25: Chapter 6

Data Structures Using C++ 25

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

Page 26: Chapter 6

Data Structures Using C++ 26

Solution to 8-Queens Puzzle

Page 27: Chapter 6

Data Structures Using C++ 27

4-Queens Puzzle

Page 28: Chapter 6

Data Structures Using C++ 28

4-Queens Tree

Page 29: Chapter 6

Data Structures Using C++ 29

8 X 8 Square Board

Page 30: Chapter 6

Data Structures Using C++ 30

Chapter Summary

• Recursive definitions

• Recursive algorithms

• Recursive functions

• Base cases

• General cases

Page 31: Chapter 6

Data Structures Using C++ 31

Chapter Summary

• Tracing recursive functions

• Designing recursive functions

• Varieties of recursive functions

• Recursion vs. Iteration

• Backtracking

• N-Queens puzzle