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

An introduction to recursive algorithms, a problem-solving strategy where a procedure calls itself. The approach involves simplifying complex problems into smaller instances and solving them recursively, with the solutions combined to solve the original problem. The recursive algorithm format, examples such as finding an element in an array, counting elements, and calculating factorials, and the properties and advantages of recursion over iteration. It also discusses the towers of hanoi problem and the importance of designing correct recursive algorithms.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-02a
koofers-user-02a 🇺🇸

10 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
Recursive Algorithms
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

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)