Recursion_Advanced Python
Recursion_Advanced Python
Larissa C. Shimomura
Outline
• Recursion
• Recursion vs. Iteration
• Search and Sorting
• Divide and Conquer algorithms
What is Recursion?
• Example 1: Fibonacci - Sequence in which each number is the sum of the two
preceeding ones
n=0,1
0, 1
0+1=1 n=2
n=6
1+1=2 n=3
3+ 5 = 8
1+2=3 2+3=5
n=4
n=5
Problem solving using recursion
• Fibonacci - Sequence in which each number is the sum of the two preceeding
ones
• Call sequence:
n=0,1
0, 1
0+1=1 n=2
n=6
1+1=2 n=3
3+ 5 = 8
1+2=3 2+3=5
n=4
n=5
Problem solving using recursion
• In Fibonacci:
1. Smaller problem: Sum of the two
previous numbers
Recursion!!!
In practice, when implementing
recursion it means that the program/
function will call itself!
Must haves for Recursion
Without the base case and a properly written recursive case, the function will be called in nitely!
fi
What is happening under the hood?
How does it move to simpler cases and eventually to the base case?
• It’s a function! Each previous call waits for the next call to nish.
• First, it makes all the calls until it reaches the base case
• Each function call is added to the call stack
bonacci(5)
bonacci(4) bonacci(3)
bonacci(1) bonacci(0)
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
What is happening under the hood?
How does it move to simpler cases and eventually to the base case?
• It’s a function! Each previous call waits for the next call to nish.
• Base case can be solved without extra function calls - Function nishes, answer is returned to previous call!
• Whenever a function nishes it gets removed from the call stack
bonacci(5)
bonacci(4) bonacci(3)
bonacci(1)
Base Case!
bonacci(0)
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
What is happening under the hood?
How does it move to simpler cases and eventually to the base case?
• It’s a function! Each previous call waits for the next call to nish.
• Previous call uses the answer of the nished function!
3+2=5
Recursive Cases
bonacci(5)
2+1=3 1+1=2
bonacci(4) bonacci(3)
1+1=2 0+1=1 0+1=1
bonacci(3) bonacci(2) bonacci(2) bonacci(1)
0+1=1
bonacci(2) bonacci(1) bonacci(1) bonacci(0) bonacci(1) bonacci(0)
bonacci(1)
Base Case!
bonacci(0)
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Examples - Factorial and Exponential
See Recursion-pt1.ipynb
Example 2 - Factorial Example 3 - Exponential
• Start from simpler cases and move eventually to more di cult ones
• Since the recursive steps always calls a previous step, it can help to think
about the problem backwards.
ffi
Recursion vs. Iteration
Can we rewrite recursive programs to iterative programs? - Yes!!
Recursive Version
Recursion vs. Iteration
Can we rewrite recursive programs to iterative programs? - Yes!!
Recursive Version
Recursion vs. Iteration
- Iteration can be simulated by recursion
Which one to use depends on your use case and problem you are dealing with!
fi
Recursion vs. Iteration
Performance - Execution Time
Recursive Version Iterative Version
Stack Overflow - Recursion depth in Python
• Tree traversals
• Graph searches: Depth- rst search and Path searches
• Sorting algorithms
• Merge Sort
• Quick Sort
Which one to use depends on your use case and the problem you are dealing with!
fi
Memoization
0+1=1
bonacci(2) bonacci(1) bonacci(1) bonacci(0) bonacci(1) bonacci(0)
bonacci(1)
Base Case!
bonacci(0)
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Memoization
• It is exponential - each call, makes 2 other calls
• Many calls with the same parameters 3+2=5
bonacci(5)
2+1=3 1+1=2
bonacci(4) bonacci(3)
1+1=2 0+1=1 0+1=1
bonacci(3) bonacci(2) bonacci(2) bonacci(1)
0+1=1
bonacci(2) bonacci(1) bonacci(1) bonacci(0) bonacci(1) bonacci(0)
bonacci(1) bonacci(0)
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Memoization
• Linear Search
• Binary Search
Guessing Game (1)
picks a secret number
between 1 and 1024 makes a guess
• An algorithm designed to test whether the rst element of an array is equal to the second
fi
Guessing Game (2)
picks a secret number
between 1 and 1024 makes a guess
0 512 1024
Algorithm
1. Let [x,y] be the interval containing the secret number (for example, [1,1024])
2. Let m be round((x+y) / 2) (for example, 512). Guess m.
3. If m is correct then stop
4. If m is smaller than the secret number, set x equal to m+1 and goto step 1
5. If m is larger than the secret number, set y equal to m-1 and goto step 1
Binary Search Implementation
def binary_search(data, key):
low = 0
high = len(data) - 1
middle = (low + high + 1) // 2
location = -1
return location
O(log(n))
Binary Search
0 512 1024
Low Middle High
• Selection sort
• Insertion sort
• Merge sort
Selection Sort
• First iteration selects the smallest element in the array and swaps it with the rst element
• Second iteration selects the second-smallest item (which is the smallest item of the remaining elements)
and swaps it with the second element
• The algorithm continues until the last iteration selects the second-largest element and swaps it with the
second-to last index, leaving the largest element in the last index
fi
Selection Sort
index1 smallest
smallest
index1
index1
…
Selection Sort
def selection_sort(data):
for index1 in range(len(data) - 1):
smallest = index1 # first index of remaining array
O(nˆ2)
Selection Sort
def insertion_sort(data):
for next in range(1, len(data)):
insert = data[next] # value to insert
move_item = next # location to place element
data[move_item] = insert
Divide and Conquer Algorithms
• Break up problem into several parts and solve each part recursively divide
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 12
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1 2 3
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1 2 3 4
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1 2 3 4 5
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1 2 3 4 5 6
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1 2 3 4 5 6 7
Merge sort
• Observation: linear time algorithm to merge to
sorted lists
list_a 1 3 4 7
list_b 2 5 6 8 9 10
output 1 2 3 4 5 6 7 8 9 10
Merge Sort Algorithm
merge_sort(list):
if len(list) > 1:
else
Conquer
return list
fi
Merge Sort Algorithm
log(n) Splitting: n
divide
conquer
merge
nlog(n)
log(n)
Summary
• Recursion
• Recursion vs. Iteration
• Search and Sorting
• Divide and Conquer