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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background 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).
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background image of page 6
Tracing Factorial Execution Can also show values of local variables inside each box Normally show parameters at top of box
Background image of page 7

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

View Full DocumentRight 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
Background 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); }
Background image of page 9

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

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

This note was uploaded on 12/01/2011 for the course CSCE 2014 taught by Professor Gauch during the Spring '11 term at Arkansas.

Page1 / 64

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online