Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

Recursion B.Ramamurthy Chapter 4 Introduction Recursion is one of most powerful methods of solution available to computer scientists. We will study application of ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 23
Provided by: KumarM1
Learn more at: https://cse.buffalo.edu
Category:
Tags: prefix | recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • B.Ramamurthy
  • Chapter 4

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
    such as divide and conquer, spilt into first
    (/last) and the rest.

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
sumSquares problem
  • int sumSquares(int m, int n)
  • if (m lt n) return sumSquares(m, n-1) nn
  • else return nn

7
sumSquares (split)
  • int sumSquares( int m, int n)
  • int middle
  • if (m n) return nn
  • else
  • middle (mn)/2
  • sumSquares(m,middle) sumSquares(middle1
    , n)

8
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.

9
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

10
Write List Backwards
  • Try actual code with 1.2 LinkedList class.

11
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).

12
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.

13
Tower of Hanoi
  • Pole A with heavy metal disks of various sizes
    stacked on top of each other.
  • Objective Transfer disks from Pole A to Pole B.
  • Constraint Disks are so heavy that stacking to
    be done with largest at the bottom and in order.
  • Support You may use a spare pole during transfer.

14
Towers(Count, Source, Dest, Spare)
  • if (Count is 1)
  • Move disk from Source to Dest pole.
  • else
  • Solve Towers( Count -1, Source, Spare, Dest)
  • Solve Towers(1, Source, Dest, Spare)
  • Solve Towers(Count -1, Spare, Dest, Source)

15
Recursive call structure for Towers
Towers(3,A,B,C)
Towers(1,A,B,C)
Towers(2,C,B,A)
Towers(2,A,C,B)
Towers(1,A,B,C)
Towers(1,B,C,A)
Towers(1,A,C,B)
16
How can we use this?
  • In a real problem, model the problem as Tower of
    Hanoi.
  • Then a ready solution is available.
  • Example Sheep, Hay and Wolf problem.
  • Example Any transportation problem.
  • Recognize a general class of problems that can be
    solved using this model.

17
Defining Languages
  • A grammar states the rules of a language.
  • A recursive algorithm can be written based on
    this grammar that determines whether a given
    string is a member of the language gt
    recognition algorithm.
  • For expressing a grammar we use special symbols
  • X Y means X or Y
  • X.Y or simply X Y means X followed by Y
  • ltxyzgt an instance of entity xyz

18
A grammar for Java identifiers
  • ltidentifiergt ltlettergt ltidentifiergt ltlettergt
    ltidentifiergtltdigitgt
  • ltlettergt a b z A B CZ_
  • ltdigitgt 0 1 9

19
Recognition algorithm for identifiers
  • boolean IsId (w)
  • // Returns TRUE if w is a legal Java
    identifier,
  • // Otherwise returns FALSE
  • if (w is of length 1)
  • if (w is a letter)
  • return true
  • else
  • return false
  • else if (last char of w is a letter or a digit)
  • return IsId(w minus its last char)
  • else return false

20
Algebraic expressions
  • Infix The term infix indicates that every
    binary operator appears between its operands a
    b
  • Prefix An operator precedes its operands a b
  • Postfix An operator follows its operands a b
  • Advantage of prefix and postfix expressions
    they dont need precedence rules, association
    rules or parenthesis for the order of evaluation.
    Order of evaluation is embedded in the notation
    itself.
  • One more example (a b) c (infix)
  • a b c (prefix)
  • a b c (postfix)

21
Processing Prefix Expression
  • double prefixValue(Expression e)
  • //base case
  • if (e.length 1) return value of
    firstElement
  • else
  • if (secondElement is operand)
  • return (firstElementOperate
    (secondElement,
  • prefixValue(e -
    firstTwoElements)
  • else return (firstElementOperate
    (prefixValue
  • (e -
    firstElement , lastElement), lastElement)

22
Summary
  • Recursion allows you to solve problems whose
    iterative solutions are difficult to
    conceptualize. Example Tower of Hanoi.
  • Linked list (or Collection ADT) traversal,
    backtracking, grammar recognition are some
    example where recursion can be applied.
Write a Comment
User Comments (0)
About PowerShow.com