Recursive Algorithms: Understanding the Approach and Its Applications - Prof. Nelson Padua, Study notes of Computer Science

An introduction to recursive algorithms, their definition, approaches, and formats. It includes examples of finding an element in an array, counting elements, calculating factorials, and solving the towers of hanoi problem. The document also covers the requirements for making recursion work and the concept of proof by induction. Recursive algorithms are compared to iterative algorithms, and the document concludes with a discussion of possible problems, such as infinite loops and inefficiency.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-uvx
koofers-user-uvx 🇺🇸

9 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Recursive Algorithms
Nelson Padua-Perez
Chau-Wen Tseng
Department of Computer Science
University of Maryland, College Park
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

Partial preview of the text

Download Recursive Algorithms: Understanding the Approach and Its Applications - Prof. Nelson Padua and more Study notes Computer Science in PDF only on Docsity!

Recursive Algorithms

Nelson Padua-Perez

Chau-Wen Tseng

Department of Computer Science

University of Maryland, College Park

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

  1. Solve small problem directly
  2. Simplify large problem into 1 or more smaller subproblem(s) & solve recursively
  3. Calculate solution from solution(s) for subproblem

Algorithm Format

1. Base case

Solve small problem directly

2. Recursive step

Simplify problem into smaller subproblem(s) Recursively apply algorithm to subproblem(s) Calculate overall solution

Example – Factorial

Factorial definition

n! = n × n-1 × n-2 × n-3 × … × 3 × 2 × 1 0! = 1

To calculate factorial of n

Base case If n = 0, return 1 Recursive step Calculate the factorial of n- Return n × (the factorial of n-1)

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 smaller subproblems Ability to calculate overall solution from solution(s) to subproblem(s)

Making Recursion Work

Designing a correct recursive algorithm

Verify

  1. Base case is Recognized correctly Solved correctly
  2. Recursive case Solves 1 or more simpler subproblems Can calculate solution from solution(s) to subproblems

Uses principle of proof by induction

Recursion vs. Iteration

Recursive algorithms

Higher overhead Time to perform function call Memory for activation records (call stack) May be simpler algorithm Easier to understand, debug, maintain Natural for backtracking searches Suited for recursive data structures Trees, graphs…

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

Example – Towers of Hanoi

To move a stack of n disks from peg X to Y

Base case If n = 1, move disk from X to Y Recursive step

  1. Move top n-1 disks from X to 3rd^ peg
  2. Move bottom disk from X to Y
  3. Move top n-1 disks from 3rd^ peg to Y

Recursive algorithm is simpler than iterative solution

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)

Possible Problems – Inefficiency

Recursive algorithm to calculate fibonacci(n)

If n is 0 or 1, return 1 Else compute fibonacci(n-1) and fibonacci(n-2) Return their sum

Simple algorithm ⇒ exponential time O(2 n^ )

Computes fibonacci(1) 2n^ times

Can solve efficiently using

Iteration Dynamic programming Will examine different algorithm strategies later…