Advanced Algorithms Homework Help
Advanced Algorithms Homework Help
Solution:
1. Subproblems
• x(i, j) : maximum possible profit by selling j barrels to the
suffix of buyers i through n
• for i ∈ {1,...,n + 1},j ∈ {0,...,m}
2. Relate
• Ron can either sell or not sell to buyer i
– If he sells, can’t sell to them again but must sell
remaining barrels
– If not sells, no change to barrels to be sold
• x(i, j) = max x
3. Topo. Order
• x(i, j) only depends on subproblems with strictly larger i, so
acyclic
4. Base
• If no more buyers remain, Ron must pay s dollars per unsold
barrel
• x(n +1,j) = −sj for all j ∈ {0,...,m}
5. Original
• Solve subproblems via recursive top down or iterative bottom
up
• x(1,m) is the maximum profit allowing sales of m barrels to all
buyers
• Store parent pointers to reconstruct which sales fulfill an
optimal order
6. Time
• # subproblems: (n + 1)(m + 1) = O(nm)
• Work per subproblem: O(1)
• O(nm) running time, which is pseudopolynomial in the size of
the input
Solution:
1. Subproblems
• Assume pins are zero-indexed in array V = (v0,...,vn−1)
• x(i, j, c) : maximum possible score playing Split Bowling
on substring of pins V [i : j], where all pins in range
must be knocked down if c = 1, but unconstrained if c
=0
• for i ∈ {0,...,n},j ∈ {i,... , n},c ∈ {0, 1}
2. Relate
• Guess what to do with pin i
– If c = 0, can choose not to knock down pin i
– Otherwise, can knock down pin i in one of three ways:
∗ knock down by itself getting vi points,
∗ knock down with pin i + 1 for vi · vi+1 points, or
∗ knock down with pin k for vi · vk points for some k
∈ {i + 2,...,j − 1}
– In last case, need max value to knock down all pins
between and pins after
3. Topo. Order
• x(i, j) only depends on subproblems with strictly smaller j −
i, so acyclic
4. Base
• x(i, i, k) = 0 (no more pins gives zero value)
• for i ∈ {0,...,n},k ∈ {0, 1}
5. Original
• Solve subproblems via recursive top down or iterative
bottom up
• x(0, n, 0) : maximum score possible playing on all pins,
unconstrained
6. Time
• # subproblems: O(n2)
• Work per subproblem: O(n)
• O(n3) running time, which is polynomial in the size of the
input
Solution:
1. Subproblems,
• x(k, s1,s2,s3) : True if it is possible to partition suffix of
items A[k :] into four subsets A1,A2,A3,A4, where sj =
P ai∈Aj ai for all j ∈ {1, 2, 3}, and false otherwise
• for k ∈ {0,...,n} and s1,s2,s3 ∈ {0,...,m}
2. Relate
• Integer ak must be placed in some partition. Guess!
• x(k, s1,s2,s3) = OR
3. Topo. Order
• x(k, s1,s2,s3) only depends on subproblems with strictly
larger k, so acyclic
4. Base
• x(n, 0, 0, 0) = True (can partition zero integers into zero sum
subsets)
• x(n, s1,s2,s3) = False for any s1,s2,s3 > 0
• (cannot partition zero integers into any subset with positive
sum)
• for i ∈ {0,...,n},k ∈ {0, 1}
5. Original
• Solve subproblems via recursive top down or iterative bottom
up
• Let m(0,s1,s2,s3) =
programminghomeworkhelp.com
• Work to compute original: O(m3)
• O(m3n) running time, which is pseudopolynomial in the
size of the input
programminghomeworkhelp.com
Solution: Observe that if we can process Lu in O(m2n) time, and
then compute the restoration the n corrupted logs, each in O(m3)
time, then we will be within the desired bound.
First, scan through each of the O(m2n) at most length-m
uncorrupted logs, and insert each word w into a hash table H
mapping to frequency f(w). Specifically, if w does not appear in
H, add it to H mapping to 1, and if w does appear in H, increase
the mapped value by 1. This process computes all f(w) for words
appearing in any log in Lu directly in expected O(m3n) time,
since the time to hash each word is linear in its length. Second,
we compute a restoration for each log ` i in Lc via dynamic
programming in expected O(m3) time, leading to an expected
O(m3n) running time in total, which is polynomial in the size of
the input (there are at least Ω(nm) characters in the input).
1. Subproblems
• x(j) : maximum P w∈Ri,j f(w) for any restoration Ri,j of
suffix ` i[j :]
• for j ∈ {0,..., |` i| ≤ m}
2. Relate
• Guess the first word in ` i[j :] and recurse on the
remainder
• x(j) = max{f(` i[j : k]) + x(k) | k ∈ {j +1,..., |` i|}} H(w) if
w∈H
• Where f(w) = 0
3. Topo. Order
• x(j) only depends on strictly larger j, so acyclic
4. Base
• x(|` i|) = 0 (no log left to restore)
5. Original
• Solve subproblems via recursive top down or iterative
bottom up
• Solution to original problem is given by x(0)
• Store parent pointers to reconstruct a restoration
achieving value x(0)
6. Time
• # subproblems: O(m)
• Work per subproblem: O(m2)
• (O(m) choices, and each hash table lookup costs
expected O(m) time)
• Expected O(m3) running time per restoration
programminghomeworkhelp.com