12 - Dynamic Programming
ucla | CS 180 | 2023-11-14 08:02
Table of Contents
- Dynamic Programming (DP)
- Weighted Interval Scheduling
- Knapsack Problem
- Sequence Alignment (Longest Common Subsequence)
- RNA Secondary Structure
Dynamic Programming (DP)
- used when divide and conquer crates overlapping divisions
- create many subproblems for which answers are known (or cached) and use as needed
Weighted Interval Scheduling
Problem
- given a set of weighted intervals, find a subset of non-overlapping intervals that maximizes the total weight of intervals

Binary Selection
- Sort by end time



iff timesteps are integers and uniqueCase 1

Case 2

Memoization (Caching)
- must cache the results to prevent redundant branches in the recursive solution using binary selection
- Now finding previous is O(1)

Time Complexity

Proof
- basically an exhaustive search of case by case consideration of interval
Knapsack Problem
Problem


Dynamic Programming

- if there are multiple of each item, you an select the same value multiple times

- The optimal value of the knapsack is in the bottom right corner of the table
- BUT, this value either came from the value directly above it, or some value in its row
- If the value is somewhere in the row (same value), include that marginal item of that row (e.g. last row adds item 5), then continue (ur going to end up going up a row)

Time Complexity
- Polynomial in inputs but linear in implementation:

Sequence Alignment (Longest Common Subsequence)
Problem
- Given 2 sequences,
L,Rof sizem,nfind the largest common contiguous subsequences 
Intuition / Class Algo
- Sps we look at seqs
L,Rup to lengthi,j 
- 2 Cases:
-
L[i]==R[j] => OPT(i,j) = OPT(i-1,j-1) + 1 -
L[i] != R[j] => OPT(i,j) =3 sub cases- Case 2.1:
OPT(i,j) = OPT(i-1,j)- take all of R and skip the value at
iofL
- take all of R and skip the value at
- Case 2.2:
OPT(i,j) = OPT(i,j-1)- take all of L and skip the value at
jofR
- take all of L and skip the value at
- Case 2.3:
OPT(i,j) = OPT(i-1,j-1)
- Case 2.1:
-
Time Complexity
in run timeSpace Complexity
in input size: polynomial- For finding length:
- we only ever need the row above the row we are computing
- the length is the last cell we compute in the matrix
- For finding the sequence:

- find max # of matching base pairs in the secondary structure
Intuition / Class Algo
- Controlled exhaustive search, sps we look from
1toj - The value at
jcan match with up tojpossible values between1tojfor which we’ll call a possible matcht, e.g. if atjthe value isu, we match to all possibleA(values att) before positionj 
- if there is a matching
-
- Generalized:
Implementation pseudocode
func Solution(sequence S of length n): OPT # nxn matrix for memoization for k = 5,6,...,n-1: # because of sharp turns for i=1,2,...,n-k: j=i+k for j, find all t if_match = 1 + max{over all t} (OPT(i,t-1) + OPT(t+1,j-1)) no_match = OPT(i,j-1) OPT(i,j) = max(if_match, no_match)Time Complexity
possiblet intervals from(1,j)



