0% found this document useful (0 votes)
15 views56 pages

Recursion

The document covers the basics of recursion in programming, including its definition, use cases, and examples such as calculating factorials and checking for palindromes. It emphasizes the importance of base cases and recursive cases in designing recursive algorithms, as well as the efficiency of recursion compared to iterative solutions. Additionally, it includes announcements for a midterm exam and lab assignments related to the course.

Uploaded by

eddina2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views56 pages

Recursion

The document covers the basics of recursion in programming, including its definition, use cases, and examples such as calculating factorials and checking for palindromes. It emphasizes the importance of base cases and recursive cases in designing recursive algorithms, as well as the efficiency of recursion compared to iterative solutions. Additionally, it includes announcements for a midterm exam and lab assignments related to the course.

Uploaded by

eddina2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 56

Basics of Recursion

CSCI - 135
Spring 2023
Anietie Andy
Announcements

Midterm Exam this Wednesday


Linked List
Stacks and Queues
Object oriented programming
Recursion
Grades for Labs 1 and 2 are on canvas
No lab today. Use lab time to study for Midterm exam
Project description due by next week Wednesday (email me)
https://forms.gle/qey8oKEeQFSNiZEcA
Recursion
Reasons to use Recursion
Exercise
Exercise
Exercise
Recursion and Cases
What is Recursion programmatically?
“Recursions are functions that call themselves” -- informal definition

Function defined
here!

Function calls
itself!

https://repl.it/@DrSkary/RecursionIntro
But, how does this work?
We need to know how functions work first! Look at the a non-
recursive example below.
Function called with
num=3

Local variable y created and value


changed.

When function_b is
called and it returns,
how do we know the
value of y and num?

https://repl.it/@DrSkary/NonRecursion
Call Stack (Run-time Stack)
Function called with
num=3
Call Stack
Call Stack (Run-time Stack)

Call Stack

Local variable y
created and value
changed to 10.
Call Stack (Run-time Stack)

Call Stack

fucntion_b called with


a=3
Call Stack (Run-time Stack)

Call Stack

fucntion_b returns
value of
6.

This value will be


removed or
popped from the
stack when
function_b
returns.
Call Stack (Run-time Stack)

Call Stack

return 16.
function_a will
be popped from
stack too.
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack
Call Stack and Recursion

Call Stack

Output
6
Recursive Algorithm Design - How to think?
1. Which is the smallest sub-problem that can solved?
Base Case

2. For larger problems we have the function call itself


Recursive case, answer these questions:
How can problem be divided into smaller identical problems.
Decomposition

How are solutions of smaller problems combined to form answer to the larger
problem?
Composition
Recursion Design Example: Factorial
Factorial
Factorial is a math operation denoted with the !
operator.
The factorial of a number is equal to: multiplying
the number by every number between it and 1.
Factorial of a negative number is undefined

1! = 1
5! = 1 * 2 * 3 * 4 * 5 = 120
Factorial
Factorial is a math operation denoted with the !
operator.
The factorial of a number is equal to: multiplying
the number by every other number between it and
1.
Factorial of a negative number is undefined

What’s a base-case for factorial?


Hint: Smallest input for which the answer is known!
Base case:
Factorial of 0 or 1 = 1 (this is known, smallest, does not need to
calculated)
Recursive case:
Multiply by n (composition), recursive call on n - 1
(decomposition)
Factorial
Factorial

Base Case

Recursive
Case
In-Class Exercise

A = [1,3,5,6,7,10]

Using recursion, write a Python program to calculate the sum of a list of


numbers
Solution

def list_sum(num_List):
if len(num_List) == 1:
return num_List[0]
else:
return num_List[0] + list_sum(num_List[1:])

print(list(A))
Write python code to calculate the value of 'x' to the power of 'y'
def power(num, top):
if top == 0:
return 1
else:
return num * power(num, top - 1)
def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return a*power(a,b-1)
Palindrome
Given a string return true if it’s a palindrome, otherwise return false.
A string is a palindrome if it’s the same forwards as backwards

Provide 1 example of a palindrome

“a” -> True


“foo” -> False
“racecar” -> True
Palindrome
Given a string return true if it’s a palindrome, otherwise return false.
A string is a palindrome if it’s the same forwards as backwards

Chat Waterfall: What is a base case for this problem?

Base case:
1) empty string or string size 1

2) Mismatched characters at start/end of word

Recursive case:
Remove first & last characters recursive call with a shorter string
Palindrome

Base Case 1
def is_palindrome(test):
if not text or len(text) == 1:
return True
if (text[0] != text[-1]): Base Case 2
return False
return is_palindrome(text[1:-1])
Recursive Case 2
Recursion Design Review: Recursive sum of
all numbers < num
Base (or Termination)
Case

Recursive Case

Composition happens
here

Decomposition happens
here
Factorial

Call Stack

factorial(0)
Line: 5
factorial(1)
Line: 5
factorial(2)
Line: 5
factorial(3)
Line: 8
__main__
done
Factorial - Iterative vs Recursive
Recursive:

Same Stopping Condition


Same update for each step
Iterative:
Caveats while writing Recursively?
1. Make sure your algorithm terminates at the base case.
a. At least 1 base case is required! Can be more than 1!
b. You MUST return a value in the base case -> no recursive calls!
c. A program that runs forever does not help practically.
d. Exception: you are working on theoretical or philosophical problems.

2. Recursive case:
a. At least 1 recursive case is required! Can be more than 1!
b. Use recursive call for decomposition.
c. Use result from recursive calls for composition.
No Base Case Example
If the recursive case does not exist, it becomes a simple function.

What happens when the base case does not exist?


You expect behavior similar to an infinite while loop.

In python, the language sets a limit on recursion.


And, you can change it for advanced usage, but you shouldn’t.

https://replit.com/@DrSkary/InfiteRecursion
Google’s Easter Egg - The cult of Recursion

Recursion is really useful for certain problem. It has a history of

being loved and adored.

Google search for example, usually uses autocorrect suggestion

“Did you mean:....” to suggest for typos etc.

Try Googling the term “recursion”


Sample: The Fibonacci Sequence
0,1,1,2,3,5,8,13,21,34,55…..

Every number (but the first two) is the sum of the two
numbers before it.

These numbers exist in nature and explains natural


growth: https://www.youtube.com/watch?v=wTlw7fNcO-0&vl=en

Feel free to read about the “Golden ratio” too.


Here is the solution..

https://repl.it/@DrSkary/recurseFibo
Why isn’t recursion as commonly used?
Used for specific problems.

Inefficient for lot of problems.

Most recursion can be deconstructed as a loop and it is


usually faster.
Self-study: how to do deconstruct in most cases.
Let’s look at Fibonacci again
How many steps does it take to calculate fibonacci(5) recursively?

https://repl.it/@DrSkary/recurseFiboTraced

This takes about 2^4 = 15 steps, or 2n approx for any n > 0.

How many steps does it take to calculate fibonacci(5) iteratively i.e.


using a Loop?
https://repl.it/@DrSkary/iterativeFibo
This take about 5 steps or n steps for any n > 0.
Comparing 2n with N
Just plug in values...for every n >= 0,
2n > n

For example, n=0 => 20 > 0


For example, n=1 => 21 > 1
For example, n=100 => 2100 > 100 ……… It gets worse as value of N
increases

Don’t believe me? Let’s run this code here for n = 35.
https://repl.it/@DrSkary/compareFibos
To Recur or not to Recur?
Don’t prefer as the first solution for most problems. Prefer the
closed-loop variant when possible.

If you have limited memory (stack), do not use recursion.

Some problems, especially advanced data structures of graphs,


trees, traversing folder to find a file etc are inherently recursive.

When the function is working with immutable data i.e data does
not change. Usually in a for-loop, you loop through to change data.
Next Class is Midterm Review!
Review Exam Structure
Do some sample questions
Ask anything you need more clarification on.

Lab extension were posted for OOP Basics Lab


Palindrome
Given a string return true if it’s a palindrome, otherwise return false.
A string is a palindrome if it’s the same forwards as backwards

Chat Waterfall: Provide 1 example of a palindrome

“a” -> True


“foo” -> False
“racecar” -> True
Palindrome
Given a string return true if it’s a palindrome, otherwise return false.
A string is a palindrome if it’s the same forwards as backwards

Chat Waterfall: What is a base case for this problem?

Base case:
1) empty string or string size 1

2) Mismatched characters at start/end of word

Recursive case:
Remove first & last characters recursive call with a shorter string
Palindrome

Base Case 1
def is_palindrome(test):
if not text or len(text) == 1:
return True
if (text[0] != text[-1]): Base Case 2
return False
return is_palindrome(text[1:-1])
Recursive Case 2
Questions

You might also like