Programming Exam Help
Programming Exam Help
1. Subproblem definition
2. Relate recursively
3. Topological order
• Possibly use parent pointers to recover actual solution, not just objective
function
6. Time analysis P
A wealthy family, Alice, Bob, and their young son Charlie are sailing around
the world when they encounter a massive storm. Charlie is thrown
overboard, presumed drowned. Twenty years later, a man comes to Alice
and Bob claiming to be Charlie. Alice and Bob are excited, but skeptical.
Alice and Bob order a DNA matching test from the genetic testing company
46AndThee. Given three length-n DNA sequences from Alice, Bob, and
Charlie, the testing center will determine ancestry as follows: if Charlie’s
DNA can be partitioned into two (not necessarily contiguous)
subsequences of equal length, where one is a subsequence of Alice’s DNA,
and the other is a subsequence of Bob’s DNA, the Charlie is their son. For
example, suppose Alice’s DNA is AATT and Bob’s DNA is CCGG. If Charlie’s
DNA were CATG, he would be matched as their son, since CATG can be
partitioned into disjoint subsequences CG and AT which are subsequences
of Alice and Bob’s DNA respectively. However, Charlie would be found to be
an imposter if his DNA were AGTC. Describe an O(n4)-time algorithm to
determine whether Charlie is a fraud.
Obert Ratkins is having dinner at an upscale tapas bar, where he will order
many small plates. There are n plates of food on the menu, where
information for plate i is given by a triple of nonnegative integers (vi, ci, si):
the plate’s volume vi, calories ci, and sweetness si ∈ {0, 1} (the plate is
sweet if si = 1 and not sweet if si = 0). Obert is on a diet: he wants to eat no
more than k calories during his meal, but wants to fill his stomach as much
as possible. He also wants to order exactly s < n sweet plates, without
purchasing the same dish twice. Describe an O(nks)-time algorithm to find
the maximum volume of food Obert can eat given his diet.
Subproblems
• Let L(i) be the cash payout of playing the lottery on day i ∈ {1, . . . , n}
• Need to keep track of most recent two plays (or equivalently, restrictions
on future plays)
Base
• # subproblems: 6n
1. Subproblems
• Let A, B, and C be the relevant length-n DNA sequences from Alice, Bob,
and Charlie.
• x(i, j, ki, kj ): true if can match a length-ki subsequence of suffix A[i :] and
a length-kj characters from prefix B[j :] to all characters in suffix C[(n ki
kj ) :] (the suffix containing the last ki + kj characters), and false otherwise.
• Must match character C[i]; if A[i] = C[i] or B[i] = C[i] recurse on remainder
5. Original
6. Time
• # subproblems: O(n4)
3. Topo
4. Base
5. Original
• # subproblems: O(nks)
Solution 4:
1. Subproblems
4. Base
5. Original
6. Time
• # subproblems: O(n2)