Recursion : Chapter 8 Saurav Karmakar - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Recursion : Chapter 8 Saurav Karmakar

Description:

Recursive Trace for Fibonacci Numbers. 20. Too much Recursion Can ... That is, a connected graph without loop. 22. Some more examples: Factorials. Binary Search ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 26
Provided by: phi762
Learn more at: http://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Recursion : Chapter 8 Saurav Karmakar


1
Recursion Chapter 8 Saurav
Karmakar
2
Recursive Function Call
  • a recursion function is a function that either
    directly or indirectly makes a call to itself.
  • but we need to avoid making an infinite sequence
    of function calls (infinite recursion)

3
Finding a Recursive Solution
  • A recursive solution to a problem must be written
    carefully
  • The idea is for each successive recursive call to
    bring you one step closer to a situation in which
    the problem can easily be solved
  • The easily solved situation is called the base
    case
  • Each recursive algorithm must have at least one
    base case, as well as a general (recursive) case

4
Mathemetical Induction
  • To prove
  • Let p(n) denote the statement involving the
    integer variable n. The Principle of Mathematical
    Induction states
  • If p(1) is true and, for some integer K gt1 ,
    p(k1) is true whenever p(k) is true then p(n) is
    true for all ngt1 .
  • 4 steps in using Induction
  • Base cases --- p(1), p(2),
  • Induction hypothesis (IH) --- assume p(k) is
    true
  • Statement to be proved in induction --- it is
    true for p(k1)
  • Induction step. --- prove p(k1) is true based
    on IH

5
A recursive defination
  • int s (int n)
  • if (n 1)
  • return 1
  • else
  • return s(n-1) n
  • A few of problems
  • n ? 0 at the beginning
  • return value might be too large to fit in an int.

6
Printing number in Any Base
  • const string DIGIT_TABLE "0123456789abcdef"
  • const int MAX_BASE DIGIT_TABLE.length( )
  • void printIntRec( int n, int base )
  • if( n gt base )
  • printIntRec( n / base, base )
  • cout ltlt DIGIT_TABLE n base
  • Potential problems in this code
  • if basegt16 --- out of bound
  • if base 0 --- division by 0
  • if base 1 --- infinite loop.

7
General format forMany Recursive Functions
  • if (some easily-solved condition) // base
    case
  • solution statement
  • else // general case
  • recursive function call

8
When a function is called...
  • A transfer of control occurs from the calling
    block to the code of the function--it is
    necessary that there be a return to the correct
    place in the calling block after the function
    code is executed this correct place is called
    the return address
  • When any function is called, the run-time stack
    is used--on this stack is placed an activation
    record for the function call

9
Stack Activation Frames
  • The activation record contains the return address
    for this function call, and also the parameters,
    and local variables, and space for the functions
    return value, if non-void etc.
  • The activation record for a particular function
    call is popped off the run-time stack when the
    final closing brace in the function code is
    reached, or when a return statement is reached in
    the function code.
  • At that time the functions return value, if
    non-void, is brought back to the calling block
    return address for use there

10
A Stake of Activation Records
S(1)
S(2)
S(3)
S(4)
Main()
11
A recursive function
  • int Func ( / in / int a, / in / int
    b )
  • int result
  • if ( b 0 ) // base
    case
  • result 0
  • else if ( b gt 0 ) // first
    general case
  • result a Func ( a , b - 1 ) ) //
    instruction 50
  • return result

12
Run-Time Stack Activation Records
x Func(5, 2)//
original call at instruction 100
FCTVAL
? result
? b 2
a 5 Return
Address 100
original call at instruction 100 pushes on this
record for Func(5,2)
13
Run-Time Stack Activation Records
x Func(5, 2)//
original call at instruction 100
FCTVAL ?
result ?
b 1
a 5 Return Address 50
FCTVAL ?
result 5Func(5,1) ?
b 2 a
5 Return Address 100
call in Func(5,2) code at instruction 50 pushes
on this record for Func(5,1)
14
Run-Time Stack Activation Records
x Func(5, 2)//
original call at instruction 100
call in Func(5,1) code at instruction 50 pushes
on this record for Func(5,0)
FCTVAL ?
result ?
b 0 a
5 Return Address 50
FCTVAL ?
result 5Func(5,0) ?
b 1 a
5 Return Address 50
FCTVAL ?
result 5Func(5,1) ?
b 2 a
5 Return Address 100
15
Run-Time Stack Activation Records
x Func(5, 2)//
original call at instruction 100
FCTVAL 0
result 0
b 0 a
5 Return Address 50
FCTVAL ?
result 5Func(5,0) ?
b 1 a
5 Return Address 50
FCTVAL ?
result 5Func(5,1) ?
b 2 a
5 Return Address 100
record for Func(5,0) is popped first with its
FCTVAL
record for Func(5,1)
record for Func(5,2)
16
Run-Time Stack Activation Records
x Func(5, 2)//
original call at instruction 100
FCTVAL 5
result 5Func(5,0) 5 0
b 1
a 5 Return Address
50 FCTVAL
? result 5Func(5,1) ?
b 2
a 5 Return Address
100
record for Func(5,1) is popped next with its
FCTVAL
record for Func(5,2)
17
Run-Time Stack Activation Records
x Func(5, 2)//
original call at instruction 100
FCTVAL
10 result 5Func(5,1)
55 b 2
a 5 Return
Address 100
record for Func(5,2) is popped last with its
FCTVAL
18
Too much recursion Can Be Dangerous
Fibonacci numbers. Long fib (int n) If
(n lt1) return n Else return
fib(n-1) fib(n-2)
19
Recursive Trace for Fibonacci Numbers
20
Too much Recursion Can be Dangerous
  • This definition will lead to exponential running
    time.
  • Reason
  • -- too much redundant work.
  • Not necessary to use recursion.

21
Tree
  • Tree is a fundamental structure in computer
    science.
  • Recursive definition A tree is a root and zero
    or more nonempty subtrees.
  • Nonrecursive definition A tree consists of s set
    of nodes and a set of directed edges that connect
    pairs of nodes. That is, a connected graph
    without loop.

22
Some more examples
  • Factorials
  • Binary Search
  • template ltclass Comparablegt int binarySearch(
    const vectorltComparablegt a, const Comparable
    x, int low, int high )
  • if( low gt high )
  • return NOT_FOUND
  • int mid ( low high ) / 2
  • if( a mid lt x )
  • return binarySearch( a, x, mid 1,
    high )
  • else if( x lt a mid )
  • return binarySearch( a, x, low, mid
    - 1 )
  • else return mid

23
Divide and Conquer
  • Given an instance of the problem to be solved,
    split that into several, smaller, sub-instances
    (of the same problem).
  • Independently solve each of the sub-instances
    and then combine the sub-instance solutions so as
    to yield a solution for the original instance.

24
Finding Greatest Common Divisor
  • gcd(A,B) gcd(A-B, B)
  • template ltclass HugeIntgt
  • HugeInt gcd (const HugeInt a, Const HugeInt
    b )
  • if( b0 )
  • return a
  • else
  • return gcd(b, ab)

25
Recursion or Iteration?
EFFICIENCY
CLARITY
Write a Comment
User Comments (0)
About PowerShow.com