Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

Recursion Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9 * 8 * 7 * 6 * 5 ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 14
Provided by: sidd95
Category:
Tags: recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
2
Definition
  • Recursion is a function calling on itself over
    and over until reaching an end state.
  • One such example is factorial.
  • 10! 10 9 8 7 6 5 4 3 2 1
  • n! n(n-1)(n-2)2 1

3



  • Factorial problem can be broken down into smaller
    problems.
  • Note 0! 1






4
Recursive Algorithm
  • An algorithm that calls itself with smaller and
    smaller subsets of the original input, doing a
    little bit or work each time, until it reaches
    the base case.
  • factorial(n) n factorial(n-1)
  • where factorial(0)1

5
Sample output for recursive factorial algorithm
  • fac(1) 1 fac(0)
  • 1 1
  • 1! 1
  • fac(2) 2 fac(1)
  • 2 (1 fac(0))
  • 2 (1 1)
  • 2! 2
  • fac(3) 3 fac(2)
  • 3 (2 fac(1))
  • 3 (2 (1 fac(0)))
  • 3 (2 (1 1))
  • 3! 6
  • fac(4) 4 fac(3)
  • 4 (3 fac(2))
  • 4 (3 (2 fac(1)))
  • 4 (3 (2 (1 fac(0))))
  • 4 (3 (2 (1 1)))
  • 4! 24

fac(n) n fac(n-1) fac(0) 1
6
Writing recursive functions
  • Recursive function keeps calling itself until it
    reaches the base case.
  • Base case - the part which makes recursion
    actually work by forcing it to stop at the
    necessary point. Leaving out the base case will
    almost certain result in a function which never
    terminates.
  • Usually a conditional statement
  • Recursive case - the part in which the function
    actually calls itself with diminished input. The
    function must not call itself with the exact same
    input again, otherwise it will continue doing so
    forever!
  • Recursive algorithm

7
Base Case and Recursive Case
  • Base case fac(0) 1
  • Recursive Case fac(n) n fac(n-1)
  • include ltiostreamgt
  • using namespace std
  • int fac(int n)
  • if (n 0)
  • return 1
  • else
  • return n fac(n-1)
  • int main()
  • int num
  • cout ltlt "Find factorial of gt "
  • cin gtgt num

Base case
Recursive Case
8
Maze Example
  • Neat example that uses recursion to traverse
    through a maze.
  • Base case
  • Leave if row or column not in maze
  • Leave if current spot is not the path
  • Announce success if you reached the finish point
  • Recursive case
  • mazeTraverse(north)
  • mazeTraverse(west)
  • mazeTraverse(south)
  • mazeTraverse(east)

9
  • void mazeTraverse(char maze1212,int
    start_row,int start_col)
  • if(start_rowgt0start_rowlt12start_colgt
    0start_collt12)
  • if(start_row4start_col11)
  • coutltlt"success"ltltendl
  • if(mazestart_rowstart_col'.'
    )
  • mazestart_rowstart_col
    'x'
  • print_array(maze,12,12)
  • mazeTraverse(maze,start_ro
    w-1,start_col)
  • mazeTraverse(maze,start_ro
    w,start_col-1)
  • mazeTraverse(maze,start_ro
    w1,start_col)
  • mazeTraverse(maze,start_ro
    w,start_col1)
  • mazestart_rowstart_col
    ''

Base case
Recursive case
10
Fibonacci numbers
  • Fibonacci numbers for n0,1, 2, 3,...
  • 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
  • fib(n) fib(n-2) fib(n-1)
  • Note
  • fib(0) 0
  • fib(1) 1

11
  • // File fibRecursive.cpp
  • include ltiostreamgt
  • using namespace std
  • long fib(int n)
  • int main()
  • int num
  • cout ltlt "Find fibonacci of gt"
  • cin gtgt num
  • cout ltlt "FIB(" ltlt num ltlt ")" ltlt fib(num) ltlt
    endl
  • long fib(int n)
  • if ((n0) (n1))
  • return n
  • else
  • return (fib(n-2) fib(n-1))

Base case
Recursive case
12
Recursion not always most efficient
  • Recursive solutions are usually more elegant but
    may have a higher cost because every function
    call pushes another instance of the function on
    the program stack.
  • Calling a function is more expensive
  • than iterating a loop

fib(6)
Recursive functions can always be converted
to iterative solutions.
fib(5)
fib(3)
fib(2)
fib(1)
fib(4)
fib(4)
fib(0)
fib(1)
1
fib(2)
0
1
fib(3)
fib(2)
fib(3)
fib(0)
fib(1)
fib(2)
fib(1)
fib(0)
fib(1)
fib(2)
fib(1)
0
1
1
0
1
fib(0)
fib(1)
1
fib(0)
fib(1)
0
1
0
1
13
  • // File fibIterative.cpp
  • include ltiostreamgt
  • using namespace std
  • long fib(int n)
  • int main()
  • int num
  • cout ltlt "Find fibonacci of gt"
  • cin gtgt num
  • cout ltlt "FIB(" ltlt num ltlt ")" ltlt fib(num) ltlt
    endl
  • long fib(int n)
  • if (nlt0)
  • return(-1)
  • if (nlt1)
Write a Comment
User Comments (0)
About PowerShow.com