代写代考 Solutions to Homework 1 Practice Problems

Solutions to Homework 1 Practice Problems
[DPV] Problem 6.1 – Maximum sum
First, a note: we typically would only ask for the maximum value, not the entire subsequence, eliminating the need for bookkeeeping and/or back- tracking. This algorithm identifies and returns the value of the maximum contiguous subsequence.
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….

Copyright By PowCoder代写 加微信 powcoder

Let T(i) = the maximum sum achieved for the subsequence ending at ai.
Note that since our subsequence is contiguous, the table entry includes the value of the number for the current index – this is an inclusive prefix. Our final answer will be the maximum value found in T(·)
(b) State recurrence for entries of table in terms of smaller subproblems. Our base case reflects the problem definition, the empty set has a maxi-
mum sum of zero:
Now, consider what happens at each element in S. If the previous sum is < 0, we want to restart our sequence; if the previous sum is > 0, the maximum sum achievable must include the prior position. Thus we have:
T(i)=ai +max(0,T(i−1))∀1≤i≤n
(c) Write pseudocode for your algorithm to solve this problem.
for i = 1 to n do
T(i)=ai +max(0,T(i−1)) end for
return max(T(·))

To recreate the subsequence we add a second table, B(i) which keeps track of the starting index for the subsequence ending at i.
for i = 1 to n do
if T (i − 1) > 0 then T(i)=ai +T(i−1) B(i) = B(i − 1)
B(i) = i end if
maxvalue = 0 maxindex = 0 for i = 1 to n do
if T (i) > maxvalue then maxindex = i maxvalue = T (i)
end if end for
return S[B(maxindex)…maxindex]
(d) Analyze the running time of your algorithm.
In both examples we have one loop through n inputs to set the table values, and a second loop to find the max. Overall run time is linear O(n).

[DPV] Problem 6.4 – Dictionary lookup
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
This subproblems consider prefixes but now the table just stores TRUE or FALSE. For 0 ≤ i ≤ n, let E(i) denote the TRUE/FALSE answer to the following problem: E(i): Can the string s1s2. . .si be broken into a sequence of valid words? Whether the whole string can be broken into valid words is determined by the boolean value of E(n).
(b) State recurrence for entries of table in terms of smaller subproblems.
The base case E(0) is always TRUE: the empty string is a valid string. The next case E(1) is simple: E(1) is TRUE iff dict(s[1]) returns TRUE. We will solve subproblems E(1), E(2), . . . , E(n) in that order.
How do we express E(i) in terms of subproblems E(0), E(1), . . . , E(i−1)? We consider all possibilities for the last word, which will be of the form sj . . .si where 1 ≤ j ≤ i. If the last word is sj…si, then the value of E(i) is TRUE iff both dict(sj . . .si) and E(j − 1) are TRUE. Clearly the last word can be any of the i strings s[j…i],1 ≤ j ≤ i, and hence we have to take an “or” over all these possibilities. This gives the following recurrence relation E(i) is TRUE iff the following is TRUE for at least one j ∈ [1,…,i]: {dict (s[j…i]) is TRUE AND E(j − 1) is TRUE }.
That recurrence can be expressed more compactly as the following where ∨ denotes boolean OR and ∧ denotes boolean AND:
E(i)=False 􏱋 {dict(s[j…i])∧E(j−1)} ∀1≤i≤n j ∈[1,…,i]
with the base case:
E(0) = TRUE
(c) Write pseudocode for your algorithm to solve this problem.
Finally, we get the following dynamic programming algorithm for checking whether s[·] can be reconstituted as a sequence of valid words: set E(0) to TRUE. Solve the remaining problems in the order E(1), E(2), E(3), . . . , E(n) using the above recurrence relation.

The second part of the problem asks us to give a reconstruction of the string if it is valid, i.e., if E(n) is TRUE. To reconstruct the string, we add an additional bookkeeping device here: for each subproblem E(i) we compute an additional quantity prev(i), which is the index j such that the expression dict(sj,…,si)∧E(j−1)isTRUE.Wecanthencomputeavalidreconstruction by following the prev(i) “pointers” back from the last problem E(n) to E(1), and outputting all the charaters between two consecutive ones as a valid word.
Here is the pseudocode.
E(0) = TRUE.
for i = 1 to n do
E(i) = FALSE. for j = 1 to i do
if E(j−1)=TRUEanddict(S[j…i]=TRUE)then E(i) = TRUE
prev(i) = j
end if end for
end for return E(n)
To output the partition into words after running the above algorithm we use a recursive procedure to work back through the list:
if E(n) = FALSE then return FALSE
return (Reconstruct(S[1…prev(n)−1]),S[prev(n)…n])
(d) Analyze the running time of your algorithm.
The run time is dominated by the nested for-loops, each of which is bounded by n, for O(n2) total time.

[DPV] Problem 6.8 – Longest common substring
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
Here we are doing the longest common substring (LCStr), as opposed to the longest common subsequence (LCS). First, we need to figure out the subproblems. This time, we have two sequences instead of one. Therefore, we look at the longest common substring (LCStr) for a prefix of X with a prefix of Y . Since it is asking for substring which means that the sequence has to be continuous, we should define the subproblems so that the last letters in both strings are included. Notice that the subproblem only makes sense when the last letters in both strings are the same.
Let us define the subproblem for each i and j as:
P (i, j) = length of the LCStr for x1x2…xi with y1y2…yj
where we only consider substrings with xi = yj as its last letter. Forthoseiandjsuchthatxi ̸=yj,wesetP(i,j)=0.
(b) State recurrence for entries of table in terms of smaller subproblems.
Now, let us figure out the recurrence for P(i,j). Assume xi = yj. Say the LCStr for x1 …xi with y1 …yj is the string s1 …sl where sl = xi = yj. Then s1 …sl−1 is the LCStr for x1 …xi−1 with y1 …yj−1. Hence, in this case P (i, j) = 1 + P (i − 1, j − 1). Therefore, the recurrence is the following:
􏱅1+P(i−1,j−1) ifxi =yj P(i,j)= 0 ifxi̸=yj
where 1 ≤ i ≤ n and 1 ≤ j ≤ m
The base cases are simple, P(0,j) = P(i,0) = 0 for any i,j.

(c) Write pseudocode for your algorithm to solve this problem.
for i = 0 to n do P(i,0) = 0.
for j = 0 to m do
P(0,j) = 0. end for
for i = 1 to n do for j = 1 to m do
if xi = yj then
P (i, j) = 1 + P (i − 1, j − 1)
P(i,j) = 0 end if
end for end for
return max(P(·,·))
(d) Analyze the running time of your algorithm.
The two base case loops are linear O(n) and O(m). The nested loops to establish the values for P and find the maximum dominate the run time, which is O(nm).

[DPV] Problem 6.17 – Making change I (unlimited supply)
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
Our subproblem considers making change for ever increasing amounts un- tilwegettothevaluev: forall1≤w≤v,ifcanmakechangewwithcoins ofdenominationsx1,…,xn weletT(w)=TRUE,otherwiseT(w)=FALSE.
(b) State recurrence for entries of table in terms of smaller subproblems.
Informally, the recurrence does the following: for each denomination xi, if T(w−xi) is TRUE for at least one value of i, set T(w) to be TRUE. Else set it FALSE. Given the base case T (0) = TRUE, the formal recurrence is
T(w)=False 􏱋T(w−xi)wherexi ≤w∀1≤i≤n i
where ∨ is the OR logical operator.
(c) Write pseudocode for your algorithm to solve this problem.
T(0) = TRUE
for w = 1 to v do
T (w) = FALSE for i = 1 to n do if xi ≤ w then
T(w) = T(w) ∨ T(w − xi) end if
end for end for
return T(v)
(d) Analyze the running time of your algorithm.
The first step is O(1), The nested loops are O(v) and O(n), with updates in order O(1), this has running time O(nv). The final step is O(1) so the running time is O(nv).

[DPV] Problem 6.18 – Making change II
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
This problem is very similar to the knapsack problem without repetition that we saw in class. First of all, let’s identify the subproblems. Since each denomination is used at most once, consider the situation for xn. There are two cases, either
• We do not use xn then we need to use a subset of x1,…,xn−1 to form value v;
• We use xn then we need to use a subset of x1,…,xn−1 to form value v − xn. Note this case is only possible if xn ≤ v.
If either of the two cases is TRUE, then the answer for the original problem is TRUE, otherwise it is FALSE. These two subproblems can depend further on some subproblems defined in the same way recursively, namely, a subproblem considers a prefix of the denominations and some value.
We define a n × v sized table D defined as:
D(i, j) = {TRUE or FALSE where there is a subset of the coins of
denominations x1,…,xi to form the value j.} Our final answer is stored in the entry D(n, v).
(b) State recurrence for entries of table in terms of smaller subproblems. Analogous to the above scenario with denomiation xn we have the follow-
ing recurrence relation for D(i, j). For i > 0 and j > 0 then we have: 􏱅D(i−1,j)∨D(i−1,j−xi) ifxi ≤j
D(i,j)= D(i−1,j) ifxi >j. (Recall, ∨ denotes Boolean OR.) The base cases are
D(0, 0) = TRUE
D(0, j) = FALSE ∀ 1 ≤ j ≤ v

(c) Write pseudocode for your algorithm to solve this problem. The algorithm for filling in the table is the following.
D(0, 0) = TRUE. for j = 1 to v do
(0, j) = FALSE. end for
for i = 1 to n do for j = 0 to v do
if xi ≤ j then
D(i, j) ← D(i − 1, j) ∨ D(i − 1, j − xi)
D(i, j) ← D(i − 1, j) end if
end for end for
return D(n,v)
(d) Analyze the running time of your algorithm.
Each entry takes O(1) time to compute, and there are O(nv) entries. Hence, the total running time is O(nv).

[DPV] Problem 6.20 – Optimal Binary Search Tree
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
This is similar to the chain matrix multiply problem that we did in class. Here we have to use substrings instead of prefixes for our subproblem. For all i, j where 1 ≤ i ≤ j ≤ n, let
C(i,j) = minimum cost for a binary search tree for words pi,pi+1,…,pj. (b) State recurrence for entries of table in terms of smaller subproblems.
The base case is when i = j, and the expected cost is simply the word pi,henceC(i,i)=pi. Let’salsosetforj j will be helpful for simplifying our recurrence; we need to include i = n + 1 to ensure that all references are established. Formally, the base case is:
T(i,i)=pi forall1≤i≤n
T (i, j) = 0 for all 1 ≤ i ≤ (n + 1) and 0 ≤ j ≤ n and j < i To make the recurrence for C(i, j) we need to decide which word to place at the root. If we place pk at the root then we need to place pi,...,pk−1 in the left-subtree and pk+1 . . . , pj in the right subtree. The expected number of comparisons involves 3 parts: words pi, . . . , pj all take 1 comparison at the root , the remaining cost for the left-subtree is C(i, k − 1), and for the right-subtree it’s C(k + 1, j). Therefore, for 1 ≤ i < j ≤ n we have: C(i,j)= min((pi +···+pj)+C(i,k−1)+C(k+1,j)) i≤k≤j To fill the table C we do so by increasing width w = j − i. Finally we output the entry C(1, n). (c) Write pseudocode for your algorithm to solve this problem. Here’s our pseudocode to identify the lowest cost. The backtracking to produce the tree is left as an exercise for you to consider: for i = 1 to n do C(i,i) = pi for i = 1 to n + 1 do for j = 0 to i − 1 do C(i,j) = 0 end for end for for w = 1 to n − 1 do for i = 1 to n − w do C(i,j) = ∞ levelcost=pi +···+pj for k = i to j do cur = levelcost + C(i, k − 1) + C(k + 1, j) if C(i, j) > cur then
C(i,j) = cur end if
end for end for
return (C(1, n))
(d) Analyze the running time of your algorithm.
There are O(n2) entries in the table and each entry takes O(n) time to
fill, hence the total running time is O(n3).

[DPV] Problem 6.26 – Alignment
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
This is similar to the Longest Common Subsequence (LCS) problem, not the Longest Common Substring from this homework, just a bit more complicated. Let
P(i,j) = maximum score of an alignment of x1x2 …xi with y1y2…yj. (b) State recurrence for entries of table in terms of smaller subproblems.
Now, we figure out the dependency relationship. What subproblems does P (i, j) depend on? There are three cases:
• Matchxi withyj,thenP(i,j)=δ(xi,yj)+P(i−1,j−1); • Matchxi with−,thenP(i,j)=δ(xi,−)+P(i−1,j);
• Matchyj with−,thenP(i,j)=δ(−,yj)+P(i,j−1).
The recurrence then is the best choice among those three cases: δ(xi, yj ) + P (i − 1, j − 1)
P(i,j)=max δ(xi,−)+P(i−1,j) δ(−, yj ) + P (i, j − 1)
where1≤i≤nand1≤j≤m. Forthebasecase,wehavetobe a bit careful, there is no problem with assigning P(0,0) = 0. But how about P(0,j) and P(i,0)? Can they also be zero? The answer is no, they should not even be the base case and should follow the recurrence of assigning P (0, 1) = δ(−, y1) and generally P (0, j) = P (0, j − 1) + δ(−, yj ).

(c) Write pseudocode for your algorithm to solve this problem.
P(0,0) = 0.
for i = 1 to n do
P (i, 0) = P (i − 1, 0) + δ(xi, −). end for
for j = 1 to m do
P (0, j) = P (0, j − 1) + δ(−, yj ).
for i = 1 to n do
for j = 1 to m do
P (i, j) = max{δ(xi, yj ) + P (i − 1, j − 1),
δ(xi,−)+P(i−1,j), δ(−, yj ) + P (i, j − 1)}
end for end for
return P(n,m)
(d) Analyze the running time of your algorithm.
The running time is O(nm), the time required to fill our n × m table.

(Jumping Frog)
(a) Define the entries of your table in words. E.g., T (i) or T (i, j) is ….
Let T [i] = the number of ways Ren ́e can go from rock 1 to rock i
(b) State recurrence for entries of table in terms of smaller subproblems.
Lets start with a base case T [1] = 1, giving Ren ́e credit for being on the first rock. What about the rest of the rocks? For any given rock i, Ren ́e can land on that rock from either rock i − 1 or rock i − 4. Let’s give Ren ́e credit each time that prior rock has been visited (meaning that the value at the prior rock is > 0). And if there were two ways to get to rock i − 1, there would be two ways to get to rock i – so we accumulate the totals at each prior rock. This gives us the recurrence:
T [i] = T [i − 1] (when 2 ≤ i ≤ 4)
T [i] = T [i − 1] + T [i − 4] (when 5 ≤ i ≤ n)
(c) Write pseudocode for your algorithm to solve this problem.
for i = 2 → n
T [i] = T [i − 1] if i > 4 then
T [i] = T [i] + T [i − 4] return T[n]
(d) Analyze the running time of your algorithm.
We have a single loop through the n rocks, thus a linear run time O(n)
Final note: this could also be framed as a graph problem, do you see how?

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com