{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch05 - Ch 5 Recursion Motivation Problem Solving Tips...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Ch 5 – Recursion Motivation Problem Solving Tips Factorial Array Reversal Sum Squares Calculates Fibonacci Numbers Ackerman’s Function Binary Search Linked List Traversal Towers of Hanoi Recursive Flood Fill Defining Languages Languages Palindrome Expressions 8 Queens p X
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Motivation Recursion is a powerful problem solving tool that is based on mathematical induction The idea is to express solution to problem X in term of smaller versions of X All recursive solutions can be implemented iteratively, but they often require less code when implemented recursively Recursion also gives an easy way to “backtrack” if a searching algorithm reaches a dead end
Image of page 2
Problem Solving Tip Think like a manager! Take large problem and break into parts to delegate to employees Tell employees to think like managers and subdivide their tasks Always need a termination condition so employees know when to stop dividing and delegating For speed, try to divide the problem in half (or even smaller).
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Factorial Example Hence, solution to N factorial can be written as a smaller factorial problem We need a terminating condition to stop this sequence of recursive replacements. N! = N.(N-1).(N-2)….3.2.1 Really (N-1)! N! = N.(N-1)! 1! = 1 0! = 1 Common stopping conditions
Image of page 4
Factorial Implementation int factorial (int num) { // check terminating condition if (num <= 1) return 1; // handle recursive case else return (num * factorial(num – 1)); } The code above has same number of multiplies as an iterative solution. Recursive answer slightly slower due to function call overhead. a smaller problem
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Tracing Factorial Execution Box method tracing is helpful for showing what recursive function do Pretend we have multiple copies of code and draw a box for each Draw arrows to show when a function calls itself (pointing to new box) and when it returns to calling function. Show return values on arrows back main fact(3) fact(2) fact(1) 1 2 6
Image of page 6
Tracing Factorial Execution Can also show values of local variables inside each box Normally show parameters at top of box
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Array Reversal Example Assume we are given array of N values to reverse N N-2 Problem gets smaller at each step Stop when asked to swap 0 or 1 elements Algorithm: swap first and last elements in array recursively reverse N-2 elements in between
Image of page 8
Array Reversal Implementation void reverse(int []data, int low, int high) { // check termination condition int size = high – low + 1; if (size <= 1) return; // handle recursive case else { int temp = data[low]; data[low] = data[high]; data[high] = temp; reverse (data, low + 1, high – 1); } } int main () { int d[9] = {3, 1, 4, 1, 5, 9, 2, 6, 5} reverse (d, 0, 8); }
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Tracing Array Reversal Tracing execution of reverse(d,0,8) using the box methods.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern