Recursive Algorithms: Understanding the Basics and Implementation - Prof. Nelson Padua-Per, Study notes of Computer Science

An introduction to recursive algorithms, their definition, approaches, format, and examples. Recursive algorithms are self-calling functions that solve a problem by breaking it down into smaller subproblems. The requirements for making recursion work, recursion vs. Iteration, and possible problems such as infinite loops and inefficiency.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-12s
koofers-user-12s ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm
Finite description of steps for solving problem
Problem types
Satisfying
โ‡’
โ‡’โ‡’
โ‡’
find any legal solution
Optimization
โ‡’
โ‡’โ‡’
โ‡’
find best solution (vs. cost metric)
Approaches
Iterative
โ‡’
โ‡’โ‡’
โ‡’
execute action in loop
Recursive
โ‡’
โ‡’โ‡’
โ‡’
reapply action to subproblem(s)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Recursive Algorithms: Understanding the Basics and Implementation - Prof. Nelson Padua-Per and more Study notes Computer Science in PDF only on Docsity!

Algorithm

Finite description of steps for solving problem Problem types

Satisfying

find any legal solution

Optimization

find

best

solution (vs. cost metric)

Approaches

Iterative

execute action in loop

Recursive

reapply action to subproblem(s)

Recursive Algorithm

Definition

An algorithm that calls itself

Approach

Solve small problem directly

Simplify large problem into 1 or more smallersubproblem(s) & solve recursively

Calculate solution from solution(s) for subproblem

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 (See ArrayExamples.java)

Example โ€“ Count

To

count

# of elements in an array

Base case

If array is empty, return

Recursive step

Skip 1

st

element and

recur

on remainder of array

Add

to result

Example โ€“ Factorial

Code

int

fact

( int n ) {

if ( n == 0 ) return 1;

// base case

return n *

fact

(n-1);

// recursive step

Requirements

Must have

Small version of problem solvable without recursion Strategy to simplify problem into 1 or more smallersubproblems Ability to calculate overall solution from solution(s)to subproblem(s)

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

Recursion vs. Iteration

Problem may usually be solved either way

Both have advantages

Iterative algorithms

May be more efficient

No additional function calls Run faster, use less memory

Example โ€“ Factorial

Recursive algorithm

int

fact

( int n ) {

if ( n == 0 ) return 1;return n *

fact

(n-1);

Recursive algorithm is closer to factorial definition

Iterative algorithm

int fact ( int n ) {

int i, res;res = 1;for (i=n; i>0; i--) {

res = res * i;

} return res;

Example โ€“ Towers of Hanoi

Problem

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

Possible Problems โ€“ Infinite Loop

Infinite recursion

If recursion not applied to simpler problem

int

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 โ€“ Inefficiency

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)