1
Instructor:
Welcome to BME 603/606�
Computational Methods for�Biological Modeling & Simulation
Assistant Professor of Biomedical Engineering�Affiliate, Data Science Institute�Affiliate, Biophysics Graduate Program�Affiliate, Center for Genomic Science Innovation�Affiliate, Wisconsin Institute for Translational Neuroengineering �University of Wisconsin-Madison
DHANANJAY ‘Jay’ BHASKAR
Course Website: https://bio-modeling-uw-madison-f26.github.io/
2
BME 603/606: Computational Methods for Biological Modeling & Simulation
Announcements / Reflection
3
BME 603/606: Computational Methods for Biological Modeling & Simulation
What is recursion?
4
BME 603/606: Computational Methods for Biological Modeling & Simulation
Example: factorial
Definition: n! = n × (n−1) × (n−2) × … × 1
0! = 1
n! = n × (n−1)!
def factorial(n):
if n == 0: # base case
return 1
return n * factorial(n - 1) # recursive case
print(factorial(5)) # → 120
Notice:
factorial(4)
= 4 × factorial(3)
= 3 × factorial(2)
= 2 × factorial(1)
= 1 × factorial(0)
= 1 ← base case
5
BME 603/606: Computational Methods for Biological Modeling & Simulation
Recursions can be expressed as iterative loops
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
⇌
Recursive
Iterative
6
BME 603/606: Computational Methods for Biological Modeling & Simulation
Example: Fibonacci sequence
Definition: F(0)=0, � F(1)=1 � F(n) = F(n−1) + F(n−2)
0, 1, 1, 2, 3, 5, 8, 13, 21, …
def fib(n):
if n <= 1: # base case
return n
return fib(n-1) + fib(n-2)
print(fib(6)) # 8
7
BME 603/606: Computational Methods for Biological Modeling & Simulation
Recursion tree
8
BME 603/606: Computational Methods for Biological Modeling & Simulation
Tracking function calls
fib(5)
15 calls
fib(10)
177 calls
fib(20)
21,891 calls
fib(30)
We need a smarter approach!
fib(30) makes over 1 million function calls!
2,692,537 calls
9
BME 603/606: Computational Methods for Biological Modeling & Simulation
Dynamic programming
Idea: Solve each sub-problem once. Store the answer. Look it up when needed.
Memoization
Top-down
(recursion + cache)
Tabulation
Bottom-up
(fill a table)
10
BME 603/606: Computational Methods for Biological Modeling & Simulation
Approach 1: Memoization
memo = {}
def fib(n):
if n <= 1: return n
if n in memo: return memo[n] # ← look up cached answer
memo[n] = fib(n-1) + fib(n-2) # ← compute & store
return memo[n]
11
BME 603/606: Computational Methods for Biological Modeling & Simulation
Approach 2: Tabulation
def fib(n):
table = [0] * (n + 1)
table[1] = 1
for i in range(2, n + 1):
table[i] = table[i-1] + table[i-2]
return table[n]
12
BME 603/606: Computational Methods for Biological Modeling & Simulation
How DP works
1) The optimal solution to the big problem contains the optimal solutions
to its sub-problems
e.g. fib(n) = fib(n−1) + fib(n−2)
2) The same sub-problems appear repeatedly when we recurse.
e.g. fib(3) is needed to compute fib(4) AND fib(5)
13
BME 603/606: Computational Methods for Biological Modeling & Simulation
Longest Common Subsequence (LCS) Problem
How similar are two sequences?
Sequence A:
Sequence B:
LCS = A G T A C G (length 6)
14
BME 603/606: Computational Methods for Biological Modeling & Simulation
Longest Common Subsequence (LCS) Problem
How similar are two sequences?
15
BME 603/606: Computational Methods for Biological Modeling & Simulation
LCS: Defining the subproblem
Let dp[i][j]= length of LCS of A[1..i]and B[1..j]
if A[i] == B[j]:
dp[i][j] = dp[i-1][j-1] + 1 # characters match → extend
else:
dp[i][j] = max(dp[i-1][j], # skip A[i]
dp[i][j-1]) # skip B[j]
We can break it up into smaller problems!
16
BME 603/606: Computational Methods for Biological Modeling & Simulation
LCS: Filling in the table
LCS length = dp[4][3] = 3
17
BME 603/606: Computational Methods for Biological Modeling & Simulation
LCS: Recovering the sequence
def traceback(dp, A, B):
i, j = len(A), len(B)
lcs = []
while i > 0 and j > 0:
if A[i-1] == B[j-1]:
lcs.append(A[i-1]) # match
i -= 1; j -= 1
elif dp[i-1][j] > dp[i][j-1]:
i -= 1 # came from above
else:
j -= 1 # came from left
return "".join(reversed(lcs))