Chapter 6 Recursion Data Structures Using C 1

  • Slides: 31
Download presentation
Chapter 6 Recursion Data Structures Using C++ 1

Chapter 6 Recursion Data Structures Using C++ 1

Chapter Objectives • Learn about recursive definitions • Explore the base case and the

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

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

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

Recursive Definitions • Recursive algorithm – Algorithm that finds the solution to a given

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

Recursive Definitions • General solution – Breaks problem into smaller versions of itself •

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

Tracing a Recursive Function Recursive function – Has unlimited copies of itself – Every

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

Tracing a Recursive Function After completing recursive call: • Control goes back to calling

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

Recursive Definitions • Directly recursive: a function that calls itself • Indirectly recursive: a

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

Designing Recursive Functions • Understand problem requirements • Determine limiting conditions • Identify base

Designing Recursive Functions • Understand problem requirements • Determine limiting conditions • Identify base cases Data Structures Using C++ 9

Designing Recursive Functions • Provide direct solution to each base case • Identify general

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

Recursive Factorial Function int fact(int { if(num == return else return } num) 0)

Recursive Factorial Function int fact(int { if(num == return else return } num) 0) 1; num * fact(num – 1); Data Structures Using C++ 11

Recursive Factorial Trace Data Structures Using C++ 12

Recursive Factorial Trace Data Structures Using C++ 12

Recursive Implementation: Largest Value in Array int largest(const int list[], int lower. Index, int

Recursive Implementation: Largest Value in Array int largest(const int list[], int lower. Index, int upper. Index) { int max; if(lower. Index == upper. Index) //the size of the sublist is 1 return list[lower. Index]; else { max = largest(list, lower. Index + 1, upper. Index); if(list[lower. Index] >= max) return list[lower. Index]; else return max; } } Data Structures Using C++ 13

Execution of largest(list, 0, 3) Data Structures Using C++ 14

Execution of largest(list, 0, 3) Data Structures Using C++ 14

Recursive Fibonacci int r. Fib. Num(int a, int b, int n) { if(n ==

Recursive Fibonacci int r. Fib. Num(int a, int b, int n) { if(n == 1) return a; else if(n == 2) return b; else return r. Fib. Num(a, b, n - 1) + r. Fib. Num(a, b, n - 2); } Data Structures Using C++ 15

Execution of r. Fibonacci(2, 3, 5) Data Structures Using C++ 16

Execution of r. Fibonacci(2, 3, 5) Data Structures Using C++ 16

Towers of Hanoi Problem with Three Disks Data Structures Using C++ 17

Towers of Hanoi Problem with Three Disks Data Structures Using C++ 17

Towers of Hanoi: Three Disk Solution Data Structures Using C++ 18

Towers of Hanoi: Three Disk Solution Data Structures Using C++ 18

Towers of Hanoi: Three Disk Solution Data Structures Using C++ 19

Towers of Hanoi: Three Disk Solution Data Structures Using C++ 19

Towers of Hanoi: Recursive Algorithm void move. Disks(int count, int needle 1, int needle

Towers of Hanoi: Recursive Algorithm void move. Disks(int count, int needle 1, int needle 3, int needle 2) { if(count > 0) { move. Disks(count - 1, needle 2, needle 3); cout<<"Move disk "<<count<<“ from "<<needle 1 <<“ to "<<needle 3<<". "<<endl; move. Disks(count - 1, needle 2, needle 3, needle 1); } } Data Structures Using C++ 20

Decimal to Binary: Recursive Algorithm void dec. To. Bin(int num, int base) { if(num

Decimal to Binary: Recursive Algorithm void dec. To. Bin(int num, int base) { if(num > 0) { dec. To. Bin(num/base, base); cout<<num % base; } } Data Structures Using C++ 21

Execution of dec. To. Bin(13, 2) Data Structures Using C++ 22

Execution of dec. To. Bin(13, 2) Data Structures Using C++ 22

Recursion or Iteration? • Two ways to solve particular problem – Iteration – Recursion

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

Backtracking Algorithm • Attempts to find solutions to a problem by constructing partial solutions

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

Backtracking Algorithm • If it is determined that partial solution would not lead to

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

Solution to 8 -Queens Puzzle Data Structures Using C++ 26

Solution to 8 -Queens Puzzle Data Structures Using C++ 26

4 -Queens Puzzle Data Structures Using C++ 27

4 -Queens Puzzle Data Structures Using C++ 27

4 -Queens Tree Data Structures Using C++ 28

4 -Queens Tree Data Structures Using C++ 28

8 X 8 Square Board Data Structures Using C++ 29

8 X 8 Square Board Data Structures Using C++ 29

Chapter Summary • • • Recursive definitions Recursive algorithms Recursive functions Base cases General

Chapter Summary • • • Recursive definitions Recursive algorithms Recursive functions Base cases General cases Data Structures Using C++ 30

Chapter Summary • • • Tracing recursive functions Designing recursive functions Varieties of recursive

Chapter Summary • • • Tracing recursive functions Designing recursive functions Varieties of recursive functions Recursion vs. Iteration Backtracking N-Queens puzzle Data Structures Using C++ 31