# CMU 15-112: Fundamentals of Programming and Computer Science Class Notes: Recursion (Continued)

1. Using Multiple Recursive Calls
2. Using Recursive Results
3. Applications of Recursion
1. Recursive Sorting
2. File System Navigation
3. Fractals
4. Backtracking
5. Improving Efficiency with Memoization
6. Expanding the Stack Size and Recursion Limit

1. Using Multiple Recursive Calls
1. fibonacci
1. First attempt
# Note: as written, this function is very inefficient! # (We need to use "memoization" to speed it up! See below for details!) def fib(n): if (n < 2): # Base case: fib(0) and fib(1) are both 1 return 1 else: # Recursive case: fib(n) = fib(n-1) + fib(n-2) return fib(n-1) + fib(n-2) print([fib(n) for n in range(15)])

2. Once again, printing call stack using recursion depth:
def fib(n, depth=0): print(" "*depth, "fib(", n, " )") if (n < 2): # Base case: fib(0) and fib(1) are both 1 return 1 else: return fib(n-1, depth+1) + fib(n-2, depth+1) fib(4)

3. Even better (printing result, too):
def fib(n, depth=0): print(" "*depth, "fib(", n, " )") if (n < 2): result = 1 # Base case: fib(0) and fib(1) are both 1 print(" "*depth, "-->", result) return result else: result = fib(n-1, depth+1) + fib(n-2, depth+1) print(" "*depth, "-->", result) return result fib(4)

4. Finally, not duplicating code:
def fib(n, depth=0): print(" "*depth, "fib(", n, " )") if (n < 2): result = 1 else: result = fib(n-1, depth+1) + fib(n-2, depth+1) print(" "*depth, "-->", result) return result fib(4)

2. towersOfHanoi
def moveDiscs(pegs, startPeg, endPeg, tmpPeg, numDiscs): # If you have only one disc, just move it! if numDiscs == 1: assert(len(pegs[endPeg]) == 0 or pegs[startPeg][0] < pegs[endPeg][0]) disc = pegs[startPeg].pop(0) print("Moving", disc, "from", startPeg, "to", endPeg) pegs[endPeg].insert(0, disc) return 1 else: numMoves = 0 # If you want to move N discs, move the top N-1 discs to the tmp peg numMoves += moveDiscs(pegs, startPeg, tmpPeg, endPeg, numDiscs - 1) # Then move the bottom disc to the end peg numMoves += moveDiscs(pegs, startPeg, endPeg, tmpPeg, 1) # Then move the N-1 discs from the tmp to the end peg numMoves += moveDiscs(pegs, tmpPeg, endPeg, startPeg, numDiscs - 1) return numMoves # A wrapper function that sets up the other parameters based on pegs def towersOfHanoi(pegs): return moveDiscs(pegs, "left", "right", "middle", len(pegs["left"])) pegs = { "left" : [1, 2, 3], "middle" : [], "right" : [] } print("Number of discs moved:", towersOfHanoi(pegs)) print("End peg state:", pegs)

3. floodFill
Python code: notes-recursion-floodFill-grid-based.py
Key excerpt:
def floodFill(data, row, col, depth=0): if ((row < 0) or (row >= data.rows) or (col < 0) or (col >= data.cols)): return # off-board! cell = data.cells[row][col] if (cell.isWall == True): return # hit a wall if (cell.depth >= 0): return # already been here # "fill" this cell cell.depth = depth cell.ordinal = len(data.floodFillOrder) data.floodFillOrder.append(cell) # then recursively fill its neighbors floodFill(data, row-1, col, depth+1) floodFill(data, row+1, col, depth+1) floodFill(data, row, col-1, depth+1) floodFill(data, row, col+1, depth+1)

2. Using Recursive Results
1. powerset
def powerset(a): # returns a list of all subsets of the list a if (len(a) == 0): return [[]] else: allSubsets = [ ] for subset in powerset(a[1:]): allSubsets += [subset] allSubsets += [[a[0]] + subset] return allSubsets print(powerset([1,2,3]))

2. permutations
def permutations(a): # returns a list of all permutations of the list a if (len(a) == 0): return [[]] else: allPerms = [ ] for subPermutation in permutations(a[1:]): for i in range(len(subPermutation)+1): allPerms += [subPermutation[:i] + [a[0]] + subPermutation[i:]] return allPerms print(permutations([1,2,3]))

3. Applications of Recursion
1. Recursive Sorting
1. Mergesort
def merge(A, B): # beautiful, but impractical for large N if ((len(A) == 0) or (len(B) == 0)): return A+B else: if (A[0] < B[0]): return [A[0]] + merge(A[1:], B) else: return [B[0]] + merge(A, B[1:]) def merge(A, B): # iterative (ugh) and destructive (double ugh), but practical... C = [ ] i = j = 0 while ((i < len(A)) or (j < len(B))): if ((j == len(B)) or ((i < len(A)) and (A[i] <= B[j]))): C.append(A[i]) i += 1 else: C.append(B[j]) j += 1 return C def mergesort(L): if (len(L) < 2): return L else: mid = len(L)//2 left = mergesort(L[:mid]) right = mergesort(L[mid:]) return merge(left, right) print(mergesort([1,5,3,4,2,0]))

2. Quicksort
def quicksort(L): if (len(L) < 2): return L else: first = L[0] # pivot rest = L[1:] lo = [x for x in rest if x < first] hi = [x for x in rest if x >= first] return quicksort(lo) + [first] + quicksort(hi) print(quicksort([1,5,3,4,2,0]))

2. File System Navigation
1. printFiles
import os def printFiles(path): if (os.path.isdir(path) == False): # base case: not a folder, but a file, so print its path print(path) else: # recursive case: it's a folder for filename in os.listdir(path): printFiles(path + "/" + filename) # To test this, download and expand this zip file in the same directory # as the Python file you are running: hw10p2_sampleFiles.zip # Note: if you see .DS_Store files in the sampleFiles folders, or in the # output of your function (as often happens with Macs, in particular), # don't worry; this is just a metadata file and can be safely ignored. printFiles("sampleFiles")

2. listFiles
import os def listFiles(path): if (os.path.isdir(path) == False): # base case: not a folder, but a file, so return singleton list with its path return [path] else: # recursive case: it's a folder, return list of all paths files = [ ] for filename in os.listdir(path): files += listFiles(path + "/" + filename) return files # To test this, download and expand this zip file in the same directory # as the Python file you are running: hw10p2_sampleFiles.zip print(listFiles("sampleFiles"))

3. removeTmpFiles
# If you want to automatically remove all .DS_Store files, call the following # function on the directory you want to clear. # Be careful when using os.remove() on non-temporary files- it's permanent! import os def removeTmpFiles(path): if path.split("/")[-1] == '.DS_Store': os.remove(path) elif os.path.isdir(path): for filename in os.listdir(path): removeTmpFiles(path + "/" + filename)

3. Fractals
1. kochSnowflake
Python code: notes-recursion-koch-snowflake.py
Key excerpt:
def kochSide(length, n): if (n == 1): turtle.forward(length) else: kochSide(length/3.0, n-1) turtle.left(60) kochSide(length/3.0, n-1) turtle.right(120) kochSide(length/3.0, n-1) turtle.left(60) kochSide(length/3.0, n-1)

2. sierpinskiTriangle
Python code: notes-recursion-sierpinsky-triangle.py
Key excerpt:
def drawSierpinskyTriangle(canvas, x, y, size, level): # (x,y) is the lower-left corner of the triangle # size is the length of a side if (level == 0): canvas.create_polygon(x, y, x+size, y, x+size/2, y-size*(3**0.5)/2, fill="black") else: drawSierpinskyTriangle(canvas, x, y, size/2, level-1) drawSierpinskyTriangle(canvas, x+size/2, y, size/2, level-1) drawSierpinskyTriangle(canvas, x+size/4, y-size*(3**0.5)/4, size/2, level-1)

4. Backtracking
Sometimes, when problem solving, you'll encounter a problem that has several parts which each have several possible solutions. One way to find a set of solution-parts that all work together is to use backtracking. This process follows the following template:
# General backtracking template def solveWithBacktracking(problemState): if isComplete(problemState): return problemState nextStep = getNextStep(problemState) for move in getPossibleMoves(problemState, nextStep): # Sometimes it's easier to make a move, then check if it's valid. # Sometimes it's easier to check if a move is valid first. # Just make sure that you always undo a move properly! if isValid(problemState, nextStep, move): problemState = makeMove(problemState, nextStep, move) tmpSolution = solveWithBacktracking(problemState) if tmpSolution != None: return tmpSolution problemState = undoMove(problemState, nextStep, move) return None

1. maze solving
Python code: notes-recursion-maze-solver.py
Key excerpt:
def isValid(data, row,col,direction): maze = data.maze rows,cols = len(maze),len(maze[0]) if not (0<=row<rows and 0<=col<cols): return False if direction==EAST: return maze[row][col].east if direction==SOUTH: return maze[row][col].south if direction==WEST: return maze[row][col-1].east if direction==NORTH: return maze[row-1][col].south assert False def solve(data, row, col, visited): # base cases if row == len(data.maze)-1 and col == len(data.maze[0])-1: return visited # recursive case for direction in [NORTH,SOUTH,EAST,WEST]: drow, dcol = direction if (row+drow, col+dcol) not in visited and \ isValid(data, row, col, direction): visited.add((row+drow,col+dcol)) tmpSolution = solve(data, row+drow, col+dcol, visited) if tmpSolution != None: return tmpSolution visited.remove((row+drow,col+dcol)) return None def solveMaze(data): visited = set() visited.add((0, 0)) return solve(data, 0, 0, visited)

2. nQueens
def isLegal(board, queenRow, queenCol): # A board is legal if no two queens can attack each other # We only need to check the most recently placed queen for row in range(len(board)): for col in range(len(board[0])): if queenRow == row and queenCol == col: continue elif board[row][col] == "Q": if ((queenRow == row) or (queenCol == col) or (queenRow + queenRow == row + col) or (queenRow - queenCol == row - col)): return False return True def solve(board, row): if (row == len(board)): return board else: for col in range(len(board[row])): board[row][col] = "Q" if isLegal(board, row, col): solution = solve(board, row + 1) if (solution != None): return solution board[row][col] = " " return None def printBoard(board): for row in range(len(board)): print("".join(board[row])) def nQueens(n): board = [ [" "] * n for row in range(n) ] solution = solve(board, 0) if solution != None: printBoard(solution)

5. Improving Efficiency with Memoization
1. The problem:
def fib(n): if (n < 2): return 1 else: return fib(n-1) + fib(n-2) import time def testFib(maxN=40): for n in range(maxN+1): start = time.time() fibOfN = fib(n) ms = 1000*(time.time() - start) print("fib(%2d) = %8d, time =%5dms" % (n, fibOfN, ms)) testFib() # gets really slow!

2. A solution:
fibResults = dict() def fib(n): if (n in fibResults): return fibResults[n] if (n < 2): result = 1 else: result = fib(n-1) + fib(n-2) fibResults[n] = result return result import time def testFib(maxN=40): for n in range(maxN+1): start = time.time() fibOfN = fib(n) ms = 1000*(time.time() - start) print("fib(%2d) = %8d, time =%5dms" % (n, fibOfN, ms)) testFib() # ahhh, much better!

3. A more elegant solution:
def memoized(f): # You are not responsible for how this decorator works # on the inside, just how to use it! import functools cachedResults = dict() @functools.wraps(f) def wrapper(*args): if args not in cachedResults: cachedResults[args] = f(*args) return cachedResults[args] return wrapper @memoized def fib(n): if (n < 2): return 1 else: return fib(n-1) + fib(n-2) import time def testFib(maxN=40): for n in range(maxN+1): start = time.time() fibOfN = fib(n) ms = 1000*(time.time() - start) print("fib(%2d) = %8d, time =%5dms" % (n, fibOfN, ms)) testFib() # ahhh, much better!

6. Expanding the Stack Size and Recursion Limit
1. The problem:
def rangeSum(lo, hi): if (lo > hi): return 0 else: return lo + rangeSum(lo+1, hi) print(rangeSum(1,1234)) # RuntimeError: maximum recursion depth exceeded

2. The solution (on most platforms):