Docsity

Recursive Algorithms: A Strategy for Solving Problems through Recursion - Prof. Nelson Pad, Study notes of Computer Science

Partial preview of the text

Download Recursive Algorithms: A Strategy for Solving Problems through Recursion - Prof. Nelson Pad and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Recursive Algorithms

Department of Computer Science

University of Maryland, College Park

Recursion

Recursion is a strategy for solving problems

A procedure that calls itself

ApproachIf ( problem instance is simple / trivial )

Solve it directly

Else

Simplify problem instance into smallerinstance(s) of the original problem

Solve smaller instance using same algorithm

Combine solution(s) to solve original problem

Example – Find

To find an element in an array

Base case

If array is empty, return false Recursive step

If 1

st

element of array is given value, return true

Skip 1

st

element and recur on remainder of array

Example – Count

To count # of elements in an array

Base case

If array is empty, return 0 Recursive step

Skip 1

st

element and recur on remainder of array

Add 1 to result

Example – Factorial

Codeint fact ( int n ) {

if ( n == 0 ) return 1;

// base case

return n * fact(n-1);

// recursive step

Properties

Recursion relies on the call stack

State of current procedure is saved whenprocedure is recursively invoked Every procedure invocation gets own stack space

Any problem solvable with recursion may besolved with iteration (and vice versa)

Use iteration with explicit stack to store state Algorithm may be simpler for one approach

Recursion vs. Iteration

Recursive algorithms

Higher overhead

Time to perform function call Memory for call stack May be simpler algorithm

Easier to understand, debug, maintain Natural for backtracking searches Suited for recursive data structures

Trees, graphs…

Example – Towers of Hanoi

Problem

Move stack of disks between pegs Can only move top disk(s) in stack Only allowed to place disk on top of larger disk

Recursion vs. Iteration

Iterative algorithms

May be more efficient

No additional function calls Run faster, use less memory

Making Recursion Work

Designing a correct recursive algorithm Verify1.^

Base case is

Recognized correctly Solved correctly

Recursive case

Solves 1 or more simpler subproblems Can calculate solution from solution(s) tosubproblems

Uses principle of proof by induction

Proof By Induction

Mathematical technique A theorem is true for all n

0 if

Base case

Prove theorem is true for n = 0, and

Inductive step

Assume theorem is true for n

(inductive hypothesis) Prove theorem must be true for n+

Types of Recursion

Tail recursion

Single recursive call at end of function Exampleint tail( int n ) {

… return function( tail(n-1) ); } Can easily transform to iteration (loop)

Possible Problems – Infinite Loop

Infinite recursion

If recursion not applied to simpler problemint bad ( int n ) {

if ( n == 0 ) return 1;return bad(n); } Will infinite loop Eventually halt when runs out of (stack) memory

Stack overflow

Possible Problems – Efficiency

May perform excessive computation

If recomputing solutions for subproblems

Example

Fibonacci numbers

fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)