Building blocks problems cont.d
You are given N wooden blocks, each with a weight Wi and a strength Si for each item i
Please find a way to stack the blocks such that the strength of a block should be larger or equal to the sum of the weights above this block and we want to maximize the number of blocks that are put into the stack
Solution: first sort by S+W.
Now let P[U, K ] be the problem that uses 1 to U items to stack and pick K items, and P[U,K] stores the minimum weight of picking K items.
The best solution is largest K such that P[N, K] has a valid value
P[X, Y] initialized to infinity
P[J, 0] = 0 for any J >= 0
Solution:
Let’s assume the items are sorted. Now let **P[U, K] ** be the problem that using 1 to U items to stack and pick K items, and P[U, K] stores the min weight of picking K items
The best solution is largest K such that P[N, K] has a valid value
P[X, Y] initialized to infinite
P[J, 0] = 0 for any J >= 0
$P[U+1, K] = \min { P[U,K], \min { P[i, K-1] \text{ If it’s less than } S[U+1] } + W[U+1] } $
Why we cannot use P[U] as subproblem of uses 1 to U items (sorted) to form a maximum stack?
Because, P[U+1] need to have S[U+1] > the sum of weights of the solution of P[U] or P[U-1] or P[U-2] … but P[U] could have an optimal solution exceeding the S[U+1], but there might be another solution that could have less weight. So we are facing the puzzle of multiple solutions in one entry, hence we need to add more variables/constraints to the subproblems.
What if the problem is changed to: maximize the total weights?
Claim: it cannot be solved in polynomial time! Knapsack can be reduced to this problem!
DP on tree structures
Claim: ALMOST all problems on Tree can be solved by DP
Simplest:
Given a tree with N nodes and N-1 edges, calculate the max sum of the nodes from root to any of the leaves.
Problem 2:
suppose we are given a tree with N nodes and a weight assigned to each node, along with a number K. The aim is to delete enough nodes from the tree so that the tree is left with precisely K leaves. The cost of such a deletion is the sum of the weights of the nodes deleted.What is the minimum cost to reduce to tree to a tree with K leaves?
Define S[a, m] be the minimium cost such that removing nodes of tree rooted at a a a with precisely m m m leave.
Recursion?
Naive recursion: S[r, K] = min { S{v1, K1} + S[v2, K2] +… } where v’s are the children of r and K1+K2+… =K
BUT THIS IS EXPONENTIAL ALGORITHM!
This is similar to backpacking problem
C1: K1 -> W(K1)
C2: K2 -> W(K2)
C_D(V) -> K_D(V) -> W(K_D(V))
Let C[u, M] be the subproblem of using children from 1 to u and having leaves exactly equal to M, the min cost of the deletion
C[u+1, M] = min{C[u, M- K*] + S[u+1, K*]}
Running time O(|E|^2 |K|). Input size: log K + O(N) = O(N)
[This is a DOUBLE DP problem]
Maximum weighted independent set on trees
Each vertex on tree is associated with a weight
Find an independent set on tree such that the weight is maximized
Subproblems of max weighted ind set (MWIS)
P(v, O) = the sub problem of occupying v and then find the MWIS in the subtrees with roots unoccupied
P(v, UO) = the sub problem of NOT occupying v and then find the MWIS in the subtrees with not constraints on roots
O P T ( v , O ) = w e i g h t ( v ) + ∑ c ∈ c h i l d r e n ( v ) O P T ( c , U O ) OPT(v, O) = weight(v) + \sum_{c\in children(v)} OPT(c, UO) OPT(v,O)=weight(v)+∑c∈children(v)OPT(c,UO)
$OPT(v, UO) = \sum_{c\in children(v)} \max{OPT(c,UO), OPT(c, O) } $
initial condition: OPT(L, UO) = 0, OPT(L, O) = weight(L)
All-Pairs shortest paths via Floyd-Warshall
Given an undirected graph with positive weight on edges, find the length of shortest paths between all pairs of vertices.
S[k, i, j] is the subproblem of shortest path from i to j using the vertices in 1 to k
Recursion: $S[k, i,j] = \min {S[k-1, i, j], S[k-1, i, k] + S[k-1, k, j] } $
Editing Distance
There are two strings A and B and they are similar. There is an editing cost of inserting, deleting and replacing a letter in A or B. What’s the minimum cost of editing A such that A becomes B, or what’s the min cost of editing both A and B such that they are the same?
e.g. A = helle, B = hllo, then we need to insert e into B and change the last e in A to o
Note that:
- Deleting a letter in A is the same as inserting a _ in B at the same position and vise versa
- Inserting a letter in A is the same as deleting the letter at the same position in B and vise versa
Lots of application in biology
One formulation of problem
Given 2 strings of letters A and B, and costs for inserting space and replacing letters in A, what’s the minimum editing distance from A to B.
Here we assume cost of all operations are 1.
Let dp[i, j] be the min cost of editing such that A[:i], B[:j] match.
recursion: $dp[i,j] = dp[i-1,j-1] + int(A[i]!=B[j]) , dp[i-1, j] + 1, dp[i, j-1] + 1 $