Transcript Lecture 7
CIT 590
Recursion
Agenda
• Style ‘rules’
• More on recursion
• Divide and conquer
• Some thoughts on running time
Style ‘rules’
• http://www.cis.upenn.edu/~matuszek/cit590-
2013/Pages/style-rules.html
• http://www.cis.upenn.edu/~matuszek/cit5902013/Pages/programming-hints.html
• Before submitting your assignments, go through a code
review process with your partner.
• Ideally, look at the same large screen
• Skype/Facetime/other communication
Recursion
• Ask yourself one question ‘Does solving a smaller
problem help me solve the bigger one’
• Lists are naturally recursive
• A list of n elements contains in it a list of n-1 elements
• Python makes extraction of portions of the list super easy
List recursion examples
• Sum of elements of a list
• Finding max of a list
• Replicate - I want to initialize a list with repeats of a
certain number
Divide and Conquer
• Divide the problem into smaller problem with (usually)
similar structure
• Solve the smaller problems
• Now aggregate/rollup the smaller solutions to get the
solution to the original problem
Classic divide and conquer = mergesort
• Sort a list
• If I divide the list into 2 halves and sort each half does that
help?
• 2 sorted lists can be merged
mergesort
1.#sorting example
2.def mergeSort(a):
3.
#base case/ simple case
4.
if len(a)== 0 or len(a) == 1:
5.
return a
6.
else:
7.
#divide
8.
firstHalf =mergeSort(a[:len(a)/2])
9.
secondHalf=mergeSort(a[len(a)/2:])
10.
#and conquer!
11.
return merge(firstHalf,secondHalf)
Merge
• Making a larger sorted array from 2 smaller sorted ones
Towers of Hanoi (and complexity of recursion)
• Classic recursion problem involving 3 towers and discs of
different sizes
• http://www.dynamicdrive.com/dynamicindex12/towerhanoi
.htm
• The recursive solution involves recursing on the largest
disc’s movement
• Show how this problem is exponential in nature
How to solve recurrences
• This is an informal method. Real proofs will be done by
induction or using some theorems.
• But, you can usually get a good ‘feel’ for the complexity
Sum of elements of a list
T(n) = T(n-1) + k, because we can assume it takes some
small constant unit of time to add 2 numbers
Solving the recurrence for Towers of
Hanoi
M(n) = Move n-1 disc to the extra tower, move the biggest disc to
the destination tower, move the n-1 discs from the extra tower to
the destination tower.
M(n) = M(n-1) + 1 + M(n-1) = 2M(n-1) + 1
= 2(2M(n-2) + 1) + 1
= 4M(n-2) + 2 + 1
= 4 (2M(n-3) + 1) + 2 + 1
= 8M(n-3) + 4 + 2 + 1
So the number of moves is following the series
1 + 2 + 4 + 8 + …+
which is bounded by 2n
Getting all permutations of a string
• Take out the first character of the string
• Make permutations from all the other characters
• Now put the first character in all possible places for each
of the permutations of the ‘n-1’ characters
Caution
• Recursive growth with things like Fibonacci, towers of
Hanoi, permutation printing
• Write base cases first
• In fact if you are doing TDD, why not write these as your first test
cases!