Data Structures 605'202'92 Recursion - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Data Structures 605'202'92 Recursion

Description:

solves a problem by breaking it into smaller problem of ... both utilize divide and conquer. recursion divides problem into smaller problems of the same type ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 10
Provided by: HOLD2
Category:

less

Transcript and Presenter's Notes

Title: Data Structures 605'202'92 Recursion


1
Data Structures 605.202.92Recursion
  • Robert Holder
  • robert.holder_at_jhuapl.edu

2
Agenda
  • Recursion
  • Examples
  • Backtracking

3
Recursion
  • solves a problem by breaking it into smaller
    problem of the same type
  • calls itself

4
Recursion vs Iteration
  • both utilize divide and conquer
  • recursion divides problem into smaller problems
    of the same type
  • iteration divides problem into smaller problems
    of different types

.
5
Writing recursive solutions
  • define the problem in terms of smaller problems
    of the same type

    determine the base case
  • make sure each recursive call diminishes the size
    of the problem
  • make sure the base case is reached as the problem
    size diminishes

6
Examples
  • factorial
  • writing a string backwards
  • picking k things from n choices
  • Hanoi Towers

7
Recursion tradeoffs
  • benefits
  • simple solutions to complex problems
  • elegant solutions to complex problems
  • helpful in transitioning to iterative solutions
  • disadvantages
  • method call overhead
  • possible recalculation of sub-steps

n! ------------ k! (n-k)!
C (n,k)
see Multiplying Rabbits, p 69 and p 93
8
Backtracking
  • trial-and-error problem solving method
  • when a solution consists of a number of
    decisions, guess at the solution until a conflict
    is reached. At that point, go back to the last
    decision and guess differently.

9
Backtracking using recursion
  • recursion with no guarantee of success, but with
    a contingency plan.

Write a Comment
User Comments (0)
About PowerShow.com