04-Recursion01

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

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

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Let’s generalize
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online