more recursion
DESCRIPTION
More Recursion. Intro to Computer Science CS1510 Dr. Sarah Diesburg. Recursion – one way. A class of methods exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases), and - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/1.jpg)
More Recursion
Intro to Computer Science
CS1510
Dr. Sarah Diesburg
![Page 2: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/2.jpg)
Recursion – one way
A class of methods exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases), and A set of rules which reduce all other cases toward
the base case. (recursive step)
2
![Page 3: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/3.jpg)
Recursion – another way
A recursive function is: One that calls itself
With “smaller” inputs (a recursive step) Until those inputs reach a base case
3
![Page 4: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/4.jpg)
A language based example… The following is a recursive definition of a
person's ancestors: Your parents are your ancestors (base case). The parents of your ancestors are also your
ancestors (recursion step).
4
![Page 5: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/5.jpg)
Recursion Humor
Recursion See "Recursion".
5
![Page 6: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/6.jpg)
Better Recursion Humor
Recursion If you still don't get it, see "Recursion".
6
![Page 7: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/7.jpg)
Recursion Humor
7
![Page 8: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/8.jpg)
Some Examples of things that can be done recursively Summation of numbers – sum(lower,upper) Exponents - power(base,exp) Reverse a string – reverse(string) Merge Sort – mergeSort(lyst)
8
![Page 9: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/9.jpg)
How Does the Computer Keep Track?
![Page 10: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/10.jpg)
The Stack
A Stack is a data structure, like a List or a Dictionary, but with a few different characteristics.
A Stack is a sequence. A Stack only allows access to one end of its
data, the top of the stack.
![Page 11: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/11.jpg)
![Page 12: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/12.jpg)
Operations
pop: remove top of stack. Stack is one element smaller.
push (val): add val to the stack. Val is now the top. Stack is one element larger.
top: Reveals the top of the stack. No modification to stack.
![Page 13: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/13.jpg)
![Page 14: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/14.jpg)
Stack of Function Calls
Python maintains a stack of function calls. If a function calls another function recursively,
the new function is pushed onto the calling stack and the previous function waits.
The top is always the active function. When a pop occurs, the function below
becomes active.
![Page 15: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/15.jpg)
Example: Fibonacci Sequence Start with two numbers in the sequence of
1 1 To get the next number in the sequence, add
the previous two numbers together 1+1=2
1 1 2 1+2=3
1 1 2 3
15
![Page 16: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/16.jpg)
How to make Fibonacci a Recursive Function Depends on the Fibo results for the two
previous values in the sequence. The base values are Fibo(0) == 1 and Fibo(1)
== 1.
fibo (x) = fibo(x-1) + fibo(x-2)
![Page 17: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/17.jpg)
Code Listing 16-3
def fibo(n):
"""Recursive Fibonacci sequence."""
if n == 0 or n == 1: # base case
return 1
else:
# divide and conquer
return fibonacci(n-1) + fibonacci(n-2)
![Page 18: More Recursion](https://reader035.vdocument.in/reader035/viewer/2022062518/56814b07550346895db81f5e/html5/thumbnails/18.jpg)
Trace
fibo(4) = fibo(3) + fibo(2) fibo(3) = fibo(2) + fibo(1) fibo(2) = fibo(1) + fibo(0) = 2 # base case fibo(3) = 2 + fibo(1) = 3 # base case fibo(4) = 3 + 2 = 5