04-Recursion01

# 04-Recursion01 - Ch Chapter 3 Recursion This material is...

This preview shows pages 1–8. Sign up to view the full content.

Chapter 3: Recursion This material is based on contents the book: “Data structures, Algorithms, and Software Principles in C”, from T.A. Standish. Parts also under permission from David McCaughan, University of Guelph, Canada. USF – COP4530 – Data Structures Summer 2009 1of±32 a d cCaug a , U e s ty o Gue p , Ca ada

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

View Full Document
What is recursion? An alternative to iteration General idea: – a subroutine, or collection of sub-routines, is defined in terms of itself Simple recursion – problem defined in terms of itself Mutual recursion cycle of problems involving one another a cycle of problems involving one another USF – COP4530 – Data Structures Summer 2009 2of±32
Think Recursively Let’s examine a sequence of solutions to simple problems Add up all squares of the numbers from m to n – Given 2 positive integers, m and n, where m n – We want to find SumSquares(m, n) = m 2 + (m+1) 2 + … + n 2 erative version Iterative version int SumSquares(nt m, in n){ int i, sum; sum = 0; for (i=m; i<=n; ++i) sum+= i*I; return sum; } USF – COP4530 – Data Structures Summer 2009 3of±32

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

View Full Document
Think Recursively How to compute SumSquares(m,n) recursively – Find a way of solving the overall problem by breaking it into smaller subproblems – Smaller subproblems can be solved using the same method as that used to solve the overall problem olutions of the subproblems are then ombined get the – Solutions of the subproblems are then combined to get the solution of the whole problem trategy Strategy int SumSquares(int m, int n){ (to compute the sum of the squares in the range m:n, where m<=n) f (there is more than one number in the range m:n){ if (there is more than one number in the range m:n){ (the solution is gotten by adding the square of m to) (the sum of aquares in the range m+1:n) }else { (there is only one number in the range m:n, so m == n, and) USF – COP4530 – Data Structures Summer 2009 (the solution is therefore just the square of m) }} 4of±32
Think Recursively Refining the strategy – A recursive call is one in which a function calls itself inside itself – The solution of the overall problem can be gotten by adding: • The solution of the smaller subproblem of SumSquares(m+1,n) he solution of the subproblem of finding the square of m The solution of the subproblem of finding the square of m – Base case: prevents recursive function from calling itself endlessly tS S (it it ) { / < int SumSquares(int m, int n){ //assume m <= n if (m<n){ return m*m + SumSquares(m+1, n); //the recursion } else { return m*m; //the base case } } USF – COP4530 – Data Structures Summer 2009 5of±32

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

View Full Document
Let’s generalize Different recursive approaches for same problem – Going-up recursions int SumSquares(int m, int n){ if (m<n){ return m*m + SumSquares(m+1, n); //the recursion } else { return m*m; //the base case } } – Going-down recursions int SumSquares(int m, int n){ if (m<n){ et n mSqaes m n )+n *n /the ec sion return SumSquares(m, n-1) + n*n; //the recursion } else { return n*n; //the base case } } USF – COP4530 – Data Structures Summer 2009 6of±32
Let’s generalize

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/28/2009 for the course COP COP 4350 taught by Professor Jimeno during the Summer '09 term at University of South Florida.

### Page1 / 32

04-Recursion01 - Ch Chapter 3 Recursion This material is...

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

View Full Document
Ask a homework question - tutors are online