My interests:
Some questions I ask:
Image Not Showing
Possible Reasons
|
|
After this lesson we should be able to:
The optimal structure can be built by combining optimal solutions to smaller problems.
E.g. Fibonacci numbers
Recursive algorithm:
The same subproblem's solution is used multiple times.
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
Call tree fib(6)
:
Recursive solution is
M-fib(n)
:
Binary Search: Given a sorted array
def binary_search(A, left, right, query):
if low > high:
return None
mid = (left + right) // 2
if A[mid] == query:
return i
elif A[mid] > query:
return binary_search(A, left, mid-1, query)
else:
return binary_search(A, mid+1, right, query)
Call tree binary_search(A=[ 15, 22, 32, 36, 41, 63, 75], left=0, right=4, query=75)
:
Example:
Counterexample:
The weights force us to consider all possible subproblems (i.e. local choice is not enough).
Let us sort all intervals by increasing finish time and let
Consider
Also consider the weight of the best solution up to
There are two cases for the last request,
Case 1
Case 2
Let
We can now write an expression for computing
Ingredients
Now we can write down a recursive algorithm that gives us the maximum weight over
compute_OPT(j)
:
Let's build the execution tree for our recursive algorithm on this example:
compute_OPT(6)
:
Runtime:
Ingredients
New algo: M_compute_OPT(j)
Runtime:
Now our DP execution has two steps:
Fill
Image Not Showing
Possible Reasons
|
Ruth Nussinov, designed the first RNA folding algo in 1977. |
We use some fairly realistic constraints on admissible structures:
A-U
, U-A
and C-G
, G-U
form pairsThis lets us identify the problem structure needed for a DP solution:
Consider an optimal set of pairs
Case 1.
Note we introduce a new variable
Case 2.
Ingredients?
From this we can build our recurrence that fills the table up to
Bonus Questions: What is the runtime for filling the table?
If you want an exercise sheet to learn how to implement this send me an email carlos@ozeki.io
.