Recursion: The Mirrors - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion: The Mirrors

Description:

For example, BinSearch(A, First, Mid -1, Value) ... True value of recursion is as a way to solve problems for which there is is no ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 25
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Recursion: The Mirrors


1
Recursion The Mirrors
  • B. Ramamurthy
  • CS114A,B

2
Introduction
  • Recursion is one of most powerful methods of
    solution available to computer scientists.
  • We will study application of recursion for
    computing, counting and searching.
  • We will also discuss the mechanics of recursion.

3
Topics to be Covered
  • Recursive Solutions Methodology
  • Computing using Recursion
  • Counting using Recursion
  • Searching using Recursion
  • Summary

4
Recursive Solutions
  • Recursion is an important problem solving
    approach that is an alternative to iteration.
  • These are questions to answer when using
    recursive solution
  • 1. How can you define the problem in terms of a
    smaller problem of the same type?
  • 2. How does each recursive call diminish the size
    of the problem?
  • 3. What instance of the problem can serve as the
    base case?
  • 4. As the problem size diminishes, will you reach
    this base case?

5
Example 1 Factorial of N
  • Iterative definition
  • Factorial(N) N (N-1) (N-2) 1 for any N gt
    0.
  • Factorial(0) 1
  • Recursive solution
  • 1. Factorial(N) N Factorial(N-1)
  • 2. Problem diminishing? yes.
  • 3. Base case Factorial(0) 1 base case does
    not have a recursive call.
  • 4. Can reach base case as problem diminishes? yes

6
Factorial of N C solution
  • int Fact(int N)
  • //Computes factorial of non-negative integer
  • //PRE N must be greater than or equal to 0
  • //POST Returns factorial of N N unchanged
  • if (N0) return 1
  • else return N Fact(N-1)

7
Factorial of N
cout ltlt Fact(3)
6
return 3Fact(2)
3 2
return 2Fact(1)
2 1
return 1Fact(0)
1 1
return 1
8
Invariants
  • An invariant is a condition that is always True
    at a particular point in an algorithm.
  • For example, an invariant for factorial function
    above can be specified after else as shown below
  • if (N 0) return 1
  • else //INV N gt 0, so N-1 gt 0
  • return N Fact(N-1)
  • Write invariants as documentation for your code.

9
Example 2 Writing String Backwards
  • 1. Write a string of length N backwards in terms
    of writing a string of length (N-1) backwards.
  • 2. Base case
  • Choice 1 Writing empty string.
  • Choice 2 Writing a string of length 1.

10
WriteBackward(S)
  • if string is empty
  • do nothing // this is the base case
  • else
  • write the last character of S
  • WriteBackward(S - minus its last character
  • Lets now look at the code for WriteBackward(S).

11
Variations of WriteBackward(S)
  • First character and the rest of the string
    backward.
  • Base case is printing one character.
  • Try these.

12
Computing Using Recursion
  • Raising integer to a power
  • int Pow2 (int X, int N)
  • if (N 0) return 1
  • else return X Pow2(X, N-1)

13
Power Function Using Recursion
  • A more efficient solution
  • int Pow3(int X, int N)
  • if (N0) return 1
  • else int HalfPower Pow3(X, N/2)
  • if ( N 2 0)
  • return HalfPower HalfPower
  • else
  • return X HalfPower HalfPower

14
Fibonacci Sequence
  • Fib (N) 1 if N lt 2 (two base cases)
  • Fib (N-1) Fib (N-2) if N gt 2
  • Recursive solution
  • int Fib( int N)
  • if (N lt 2) return 1
  • else Fib(N-1) Fib(N-2)

15
Counting Using Recursion
  • Choosing K out of N items. Need to write in terms
    of choosing from N-1 items
  • Choose 1 and then choose K-1 from rest (N-1)
    Eliminate 1 and choose K from N-1 items.
  • C(N,K) 1 if K 0
  • 1 if K N
  • 0 if K gt N
  • C(N-1,K-1) C(N-1,K) if 0 lt K lt
    N
  • Try it with a real-life example, say , marbles.
  • Reading Assignment Mad Scientist Problem(79-81)

16
Searching Using Recursion
  • BinSearch( A, Value)
  • if (A is of size 1) Determine if its element is
    equal to Value
  • else
  • Find Midpoint of A
  • Determine which half of A contains Value
  • If (Value is in first half of A)
  • BinSearch(first half of A, Value)
  • Else
  • BinSearch(second half of A, Value)

17
Implementation Considerations
  • How will you pass half of A to the recursive
    calls to BinSearch?
  • Pass entire array but send also the limits for
    search.
  • For example, BinSearch(A, First, Mid -1, Value)
  • How do you determine which half of the array
    contains Value?
  • Test of equality and comparison between MidValue
    and Search Value.
  • For example, if (Value lt Amid)
  • What should the base case(s) be?
  • First gt Last (termination without finding)
  • Value AMid (Found Search Value)

18
C implementation
  • int BinSearch(const int A, int First, int Last,
    int Value)
  • // POST if Value is in the array, returns the
  • // index of the array element that
  • // equals Value Otherwise returns -1.
  • if (First gt Last) return -1 // base case 1

19
C implementation (Contd.)
  • else
  • int Mid (First Last)/2
  • if (Value AMid)
  • return Mid // base case 2
  • else if (Value lt AMid)
  • return BinSearch (A, First, Mid -1,
    Value)
  • else return BinSearch(A, Mid1, Last,
    Value)
  • //else
  • //BinSearch

20
Find the kth smallest element of an array
  • This problem is different from the ones we
    examined You cannot predict in advance the size
    of either the smaller problems or the base case
    in the recursive solution to the kth
    smallest-element problem.
  • Recursive solution
  • 1. Decide a pivot element in the array P with
    index Pindex.
  • 2. Partition the array into three parts elements
    lt P(call it S1), P , and elements gtP (call it S2).

21
Find the Kth smallest (Contd.)
  • 3. If there are k or more elements in S1
    AFirstPindex-1 then S1 contains k smallest
    elements of array AFirstLats. kth smallest is
    in S1.
  • 4. If there k-1 elements in S1, the pivot element
    is the required element.
  • 5. If there are fewer than k-1 elements in S1,
    then kth element is S2 APindex1 -L. Since we
    have eliminates Pindex-First elements, now we are
    looking for (k-Pindex-First1)th element in S2.

22
Find the kth smallest.. algo
  • element_type Ksmall(k, A, First, Last)
  • 1. Choose a pivot element p from AFirstLast
  • 2. if (k lt Pindex - F 1) return Ksmall(k, A,
    First, Pindex-1)
  • 3. else if (k Pindex - First 1) return p
  • 4. else return Ksmall(k - (Pindex - First 1) ,
    A, Pindex1, L)

23
Recursion and Efficiency
  • Overhead associated with function calls and the
    inherent inefficiency of some recursive
    algorithms contribute to inefficiency in some
    recursive solutions.
  • Example for inherent inefficiency Rabbit(n)
    Rabbit(n-1) Rabbit(n-2)
  • Recursion is not always appropriate, particularly
    when a clear, efficient iterative solution
    exists.
  • True value of recursion is as a way to solve
    problems for which there is is no simple
    nonrecursive solutions.

24
Summary
  • Recursion is a powerful problem-solving technique
    that often produces very clean solutions to many
    complex problems.
  • As with most powerful tools, if it is not used
    properly, it may lead to inefficiency.
  • Go thru self-test exercises, and regular
    exercises at the end of chapter 2.
Write a Comment
User Comments (0)
About PowerShow.com