Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

Notice no function calls inside this version of factorial ... best move in a game. searching through a tree data structure (next week) Finding a Path ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 21
Provided by: matt51
Category:
Tags: deal | game | no | or | recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • CS 367 Introduction to Data Structures

2
Definition
  • Mathematically
  • a recursive function is one that is defined in
    terms of an absolute case and itself
  • example

1 n 0 n(n-1)! n gt 0
n
Consider n 3 n result 0! 1 1! 1 0!
1 2! 2 1! 2 3! 3 2! 6
3
Definition
  • Programming
  • a recursive function is one that knows the answer
    to a specific case in all other cases, the
    function calls itself again
  • example
  • int factorial(int n)
  • if(n 0)
  • return 1
  • else
  • return n factorial(n 1)

4
Function Call
  • To understand recursion, we need a better idea of
    what happens on a function call
  • remember the program stack?
  • on a function call, the compiler creates an
    activation record
  • parameters passed in
  • local variables
  • return address
  • return value (where applicable)
  • activation record gets pushed on the program stack

5
Activation Records
parameters
local variables
f3()
return address
return value
parameters
local variables
f2()
return address
return value
parameters
local variables
f1()
return address
return value
main()
6
Calling a Function
  • The program compiler takes care of building the
    activation record for each function
  • this means you dont have to worry about writing
    it
  • it also means that a single function call is
    really a lot more code than you think
  • this means it takes awhile to do

7
Calling a Function
  • High level code
  • void main()
  • int z double(5)
  • int double(int x)
  • int y x 2
  • return y
  • Compiler code
  • main
  • ld r1, 5
  • push addr
  • push r1
  • jmp double
  • double
  • pop r2
  • mult r3, r2, 2
  • pop r2
  • push r3
  • jmp r2

8
Calling a Function
  • The previous compiler code is very incomplete
    (and not quite correct either ?)
  • Previous code does prove a point
  • calling a function requires more memory
  • must create and store the activation record
  • calling a function takes more time
  • must run extra code to build the activation
    record
  • Fastest program would be one that only had a main
    function
  • this is very unrealistic why?
  • if it were easy to do, still would be a bad idea
    why?

9
Anatomy of a Recursive Call
  • Consider the factorial example used earlier
  • int factorial(int n)
  • if(n 0) return 1
  • else return n factorial(n-1)
  • Now consider the following code for main()
  • void main()
  • int f factorial(3)
  • print(f)

10
Anatomy of a Recursive Call
factorial(3)
factorial(2)
factorial(1)
n 1 ret 1 f4
f3()
n 2 ret 2 f3
n 2 ret 2 f3
f2()
f2()
n 3 ret 3 f2
n 3 ret 3 f2
n 3 ret 3 f2
f1()
f1()
f1()
main()
main()
f
main()
main()
f
f
f
factorial(0)
return
return
return
n 0 ret 1
f3()
n 1 ret 1
n 1 ret 1 1
f3()
f3()
n 2 ret 2
n 2 ret 2 f3
n 2 ret 2 1
f2()
f2()
f2()
n 3 ret 3 2
n 3 ret 3
n 3 ret 3 f2
n 3 ret 3 f2
f1()
f1()
f1()
f1()
main()
f
main()
main()
main()
f
f
f
11
Anatomy of a Recursive Call
  • Obviously, the last step in the previous example
    is missing (not enough room)
  • f1 will return 6 and the value (f) in main will
    be equal to 6
  • Major point
  • using recursion can get very expensive
  • consume lots of memory
  • execute a lot of extra instructions
  • what is the alternative?

12
Loops
  • Almost any recursive function can be re-written
    as a loop
  • a loop will be much less time consuming
  • no extra activation records to construct and
    store
  • consider the factorial example one last time
  • int factorial(int n)
  • int sum 1
  • for(int i1 iltn i)
  • sum i
  • return sum

13
Loops
  • Notice no function calls inside this version of
    factorial()
  • will run much faster than the recursive version
  • Why use recursion?
  • in some cases it is easier to understand and
    write a recursive function

14
Reverse()
  • Consider a function that prints a string of
    characters in the reverse order
  • ABC will be printed out as CBA
  • void reverse()
  • char ch getChar()
  • if(ch ! \n)
  • reverse()
  • System.out.println(ch)

15
Reverse()
  • Note that this recursive function doesnt return
    anything
  • simply reads a character and waits to print it
    until the one after it is printed
  • very easy to understand
  • very quick to program
  • how would you convert this to an iterative
    solution?

16
Reverse()
  • Soln in Java
  • void reverse()
  • String stack new String()
  • stack buffer.readLine()
  • for(topstack.length()-1 topgt0 top--)
  • System.out.print(stack.charAt(top))
  • Soln in C/C
  • void reverse()
  • char stack80
  • int top0
  • stacktop getch()
  • while(stacktop ! \n)
  • stacktop getch()
  • for(top - 1 top gt 0 top--)
  • System.out.print(stacktop)

17
Reverse()
  • Iterative soln must first read whole string into
    memory
  • then it can use a loop to print the string out
  • Java soln looks so simple because of the
    String.length() operator
  • it tells us exactly how long the string is
  • many languages dont have this feature
  • There are fewer steps and less code in the
    recursive soln
  • because were interacting with I/O, recursive
    nature isnt such a big deal
  • why?

18
Backtracing
  • One of the best uses of recursion is in
    navigating a large, directed search space
  • in other words, if you are at a certain point, P,
    in the search space, you need to pick the next
    spot to search
  • if you go in one direction, you can only go back
    the other direction by first returning to point P
  • examples
  • finding a path from one city to another
  • 8 queens example
  • best move in a game
  • searching through a tree data structure (next
    week)

19
Finding a Path
  • Consider the graph of flights from the lecture on
    stacks

Key city (represented as C) flight
from city C1 to city C2
C1 C2
W
Example
flight goes from W to S
W
20
Finding a Path
  • We showed how to use a stack to find a route from
    P to Y
  • We can also do it using recursion
  • public boolean findPath(City origin, City
    destination)
  • // pick a possible city to go to make that
    city the origin
  • if(findPath(nextCity, destination))
  • // found the city
  • else
  • // that path didnt work, try another one
  • The above recursive soln is still going to need
    some kind of loop inside it
  • have to check all the possible routes from a city
Write a Comment
User Comments (0)
About PowerShow.com