1 of 17

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 of 17

2

BME 603/606: Computational Methods for Biological Modeling & Simulation

Announcements / Reflection

​

  • Assignment 1 has been released. Due Oct 5, 11:59 pm.
    • Everyone should complete Problems 1-3.
    • Problems 4-5 for grad students only!

​

  • Office hours tomorrow: 4 – 5 pm, ECB 2139

3 of 17

3

BME 603/606: Computational Methods for Biological Modeling & Simulation

What is recursion?

  • A function that calls itself to solve a smaller version of the same problem

​

  • Every recursive function needs:
    • Base case - when to stop
    • Recursive case - break the problem

4 of 17

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 of 17

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 of 17

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 of 17

7

BME 603/606: Computational Methods for Biological Modeling & Simulation

Recursion tree

8 of 17

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 of 17

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 of 17

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]

  • Use a dictionary as the memo
  • Each unique sub-problem is solved exactly once
  • fib(30) now takes just 30 calls

11 of 17

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]

  • No recursion - just a loop and a table
  • Build answers to small problems first
  • Use them to solve bigger ones
  • This is the most common DP pattern you will see

12 of 17

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 of 17

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 of 17

14

BME 603/606: Computational Methods for Biological Modeling & Simulation

Longest Common Subsequence (LCS) Problem

How similar are two sequences?

  • A subsequence preserves order but need not be contiguous
  • "ACGT" and "ACT" share subsequence "ACT"
  • LCS length = a measure of similarity
  • Applications: gene comparison, phylogenetics, diff tools

15 of 17

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!

​

  • Base case: dp[0][j] = dp[i][0] = 0 (empty sequence)
  • We fill the table row by row
  • Answer: dp[len(A)][len(B)]

16 of 17

16

BME 603/606: Computational Methods for Biological Modeling & Simulation

LCS: Filling in the table

LCS length = dp[4][3] = 3

  • Each cell uses ≤ 3 previously filled cells
  • Total work: proportional to len(A) × len(B)
  • Far better than checking all possible subsequences!

17 of 17

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))