CS187 Programming with Data Structures 8' Recursion - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

CS187 Programming with Data Structures 8' Recursion

Description:

For example, method m1 could invoke m2, which invokes m3, which invokes m1 again ... Can now define several different forms of decomposition ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 24
Provided by: robotics5
Category:

less

Transcript and Presenter's Notes

Title: CS187 Programming with Data Structures 8' Recursion


1
CS187 Programming with Data Structures8.
Recursion
  • Oliver Brock
  • UMass Amherst
  • Spring 2009

2
Overview (mostly for people who missed class)
  • In this class (or classes) we will do the
    following
  • See examples of recursion
  • Understand the ingredients of a recursive
    algorithm
  • Learn how to devise a recursive solution to a
    problem
  • Talk about implementation issues of recursion

3
Essence of recursion
  • Identify and exploit an identical but smaller
    instance of a sub-problem in order to solve a big
    problem

4
Examples of Recursion
  • Definition of trees and their traversal
  • Factorial
  • Greatest common divider
  • Pascals triangle
  • Binary search

5
Visualizing the Recursive Calls
public int sum (int N) int result if (N
1) result 1 else result N
sum(N-1) return result
main () sum(4)
Method invocations
Method returns
6
Example Adding squares of numbers
  • given two integers m and n, mltn find

public int sumOfSquares(int m, int n) int i,
sum sum0 for (im iltn i) sum
sum ii return sum
An Iterative Solution.
7
Ingredients
  • Base Case
  • Recursion
  • Merging solution of recursive call with the
    solution at the current level

8
Implementation Issues
  • Recursive versus Iterative
  • Tail recursion

9
Pascals Triangle
  • The binomial coefficients (a.k.a. Pascals
    Triangle) result from the expansion of a binomial
    expression of the form (x1)n.
  • (x1)3 x3 3x2 3x 1 1 3 3 1
  • (x1)6 x66x515x420x315x26x1 1 6 15 20
    15 6 1

pascal(n,k)
10
Recursion What you need
  • In a recursive program, there are three
    components
  • The Base Case This is the special case in the
    program. Either the solution is found or not
    found. But at this point the recursion always
    stops.
  • The Recursive Case This is the case where the
    problem is subdivided in smaller portions.
  • The Combination Putting together smaller
    problems such that the larger problem is solved
    correctly.

11
Recursive Pascals Triangle
k
pascal(n, k) ?
1 1 1 1 2
1 1 3 3 1 1 4 6 4
1 1 5 10 10 5 1 . . . . . . . . . .
. . . . . . .

pascal(n, 0) 1 for all n pascal(n, n) 1 for
all n
n
pascal(n, k) pascal(n-1,k-1)
pascal(n-1,k)
12
Recursive Solution
public int pascal(int n, int k) if (we're on
one of the edges) return 1 else
return the sum of the two parents
13
Recursive Solution
14
Recursion Complexity
  • 1
  • 1 1
  • 1 2 1
  • 1 3 3 1
  • 1 4 6 4 1
  • 1 5 10 10 5 1
  • 1 6 15 20 15 6 1
  • . . . . . . . . . . . . . . . . .

Binary Tree
O(2d)
d n / 2
Exponential!
15
Recursive Array Search
Design a recursive search function to return the
subscript of the target string
if the array is empty
Base Case
return -1
else if the first element matches the target
return the subscript of the first element
Base Case
else
Recursive Case
search the array excluding the first element and
return the result
16
Recursive Linear Search
private int linearSearch(Object items,
Object target, int posFirst) if
(posFirst items.length) return -1 else if
(target.equals(itemsposFirst)) return
posFirst else return linearSearch(items,t
arget, posFirst1)
Complexity?
O(n)
17
Recursive Binary Search
  • How do you find a word in the dictionary?
  • We ought to be able to do better than O(n)!
  • If we know the array is sorted, we can accelerate
    the search
  • How?

14
62
24
23?
18
Recursive Binary Search
Design a recursive search function to return the
subscript of the target in a sorted array
if the array is empty
return -1
else if the middle element matches the target
return the subscript of the middle element
else if the target is less than the middle element
search the array elements before the middle
element and return the result
else
search the array elements after the middle
element and return the result
19
Recursive Array Reversal using Center and Edges
A 53 9 22 45 0 87 13
  • reverse A0 and A6
  • reverse(A,1,5)
  • 13 9 22 45 0 87 53

A
1. Reverse the edges 2. Apply algorithm to
center.
20
The reverse method
  • public void reverse(int A,
  • int m, int n)
  • if (mltn)
  • int tempAm
  • AmAn
  • An temp
  • reverse(A, m1, n-1)

Hey! Where is the base case?
21
The Importance of the Base Case
  • The base case terminates the recursive calls and
    starts the reassembly process.
  • Recursive algorithms can get caught in an
    infinite loop just like iterative algorithms.
  • Two common reasons
  • there is no base case to stop the recursion
  • the base case never gets called or executed

public int sumOfSquares(int m, int n) if
(mn) return mm //solution is
the square of m else //solution is
obtained by adding the square of //m to the
sum of squares in the range (m1)n. return
mmsumOfSquares(m1,n)
What happens if m gt n on first call to this
method???
Exception Occurred java.lang.StackOverflowError
22
Types of Recursion
  • A method invoking itself is considered to be
    direct recursion
  • A method could invoke another method, which
    invokes another, etc., until eventually the
    original method is invoked again
  • For example, method m1 could invoke m2, which
    invokes m3, which invokes m1 again
  • This is called indirect recursion
  • It is often more difficult to trace and debug

23
Decomposition Methods
  • Can now define several different forms of
    decomposition
  • First and Rest split A into Am and Am1n
  • Last and All But Last Amn-1 and An
  • Halves let middle(nm)/2 and split A into the
    left half Ammiddle and the right half
    Amiddle1n (also called Divide and Conquer)
  • Edges and Center split Amn into its edges
    Am and An and its center Am1 n-1
Write a Comment
User Comments (0)
About PowerShow.com