Prompt Title: problem set

Created 1 year ago
Votes 0
Views 392 times
0

Prompt Detail:

Hello! How can I assist you today?

hi

Hello! How can I help you today?

(25 points) Consider a sequence alignment problem over an alphabet of size k, Z ={z1, z2, . . . , zk}, with given misalignment costs c(z, z') > 0 for any z != z' ∈ Z, and a given gap cost c(−) > 0. Assume that each of these parameters is a positive integer. Suppose you are given two strings A = a1a2 . . . am and B = b1b2 . . . bn. Give an O(mn) algorithm to find a minimum-cost alignment between A and B, where the cost of aligning z with z' is 0 if z = z', and c(z, z') if z != z', and the cost of aligning any letter with a dash is c(−)

To find a minimum-cost alignment between strings A and B with given misalignment costs and gap cost, we can use the dynamic programming approach. We will create a two-dimensional array DP of size (m+1) x (n+1), where DP[i][j] represents the minimum cost of aligning the prefixes Ai and Bj.

We can fill the DP array in a bottom-up manner, starting from DP[0][0] which represents the cost of aligning the empty strings. The cost of aligning the empty string with a non-empty string can be computed as the cost of inserting gaps in the non-empty string, and similarly for a non-empty string with an empty string.

The recurrence relation for filling DP[i][j] is as follows:

DP[i][j] = min( DP[i-1][j-1] + c(ai, bj), // align ai with bj DP[i-1][j] + c(-), // align ai with a gap DP[i][j-1] + c(-) // align bj with a gap )

where c(ai, bj) is the cost of aligning ai with bj, and c(-) is the gap cost.

We can fill the DP array using this recurrence relation in O(mn) time, and the minimum cost of aligning A with B will be stored in DP[m][n]. To reconstruct the alignment, we can start from DP[m][n] and backtrack along the array by choosing the minimum-cost option at each step, until we reach DP[0][0]. We can use a separate array or data structure to keep track of the alignment.

Given n positive integers x1, x2, . . . , xn. Your task is to determine whether it is possible to choose signs s1, s2, . . . , sn ∈ {+1, −1} such that: s1x1 + s2x2 + · · · + snxn = 0 . Suppose x1 + x2 + · · · + xn = X. Design an algorithm that runs in time polynomial in n and X.

We can solve this problem using dynamic programming. Let DP(i, j) be a boolean variable that denotes whether it is possible to choose signs s1, s2, . . . , si ∈ {+1, −1} such that their sum equals j.

We can initialize DP(0, 0) to true, since it is possible to choose signs for an empty sum that equals 0. For i > 0, we can fill DP(i, j) using the following recurrence relation:

DP(i, j) = DP(i-1, j-xi) or DP(i-1, j+xi)

This recurrence relation checks whether it is possible to choose a sign for xi that makes the sum equal to j, by considering the two cases where xi is positive or negative.

Once we have computed all values of DP(i, j) for i = 1 to n and j = -X to X, we can check whether DP(n, 0) is true, which means that it is possible to choose signs such that the sum equals 0.

The time complexity of this algorithm is O(nX), which is polynomial in n and X. Note that we only need to compute values of DP(i, j) for j = -X to X, since the maximum possible sum is X, and the minimum possible sum is -X. Therefore, we can use an array of size 2X+1 to store the DP values, and fill it in a bottom-up manner.

There are n kinds of coins carrying integral values v1, v2, · · · , vn, respectively, and there is unlimited supply to each kind of coins. (a) (25 points) Give an algorithm to determine whether it is feasible to make change for a cheque with a given integral value X (i.e., to find a set of coins whose total value is X). Note that each kind of coins can be used multiple times. The running time of your algorithm should be O(nX)

We can solve this problem using dynamic programming. Let DP(i, j) be a boolean variable that denotes whether it is possible to make change for the value j using the first i types of coins.

We can initialize DP(i, 0) to true, since it is always possible to make change for a value of 0 using any set of coins. For j > 0, we can fill DP(i, j) using the following recurrence relation:

DP(i, j) = DP(i-1, j) or DP(i, j-vi)

This recurrence relation checks whether it is possible to make change for the value j by either not using the ith type of coin (i.e., using only the first i-1 types of coins), or by using the ith type of coin and reducing the target value by vi.

Once we have computed all values of DP(i, j) for i = 1 to n and j = 1 to X, we can check whether DP(n, X) is true, which means that it is possible to make change for the value X using any set of coins.

The time complexity of this algorithm is O(nX), which is polynomial in n and X. Note that we only need to compute values of DP(i, j) for j = 1 to X, since it is not possible to make change for a negative value. Therefore, we can use an array of size X+1 to store the DP values, and fill it in a bottom-up manner.

pi is the price on day i. Now the fisherman wishes to find two days 1 ≤ i < j ≤ n such that buying on day i and selling on day j maximizes his gain, i.e., to maximize pi/pj (a) (25 points) Design a polynomial-time algorithm for the fisherman.

We can solve this problem in O(n) time using a greedy approach. We can keep track of the minimum price seen so far, and the maximum profit that can be obtained by selling at the current price. For each day i, we can update the minimum price seen so far and the maximum profit using the following steps:

  1. If pi is less than the minimum price seen so far, update the minimum price to pi.

  2. Otherwise, calculate the profit that can be obtained by selling at pi, which is pi divided by the minimum price seen so far. If this profit is greater than the maximum profit seen so far, update the maximum profit to this value.

At the end of this process, the maximum profit seen so far is the maximum possible profit that can be obtained by buying on some day i and selling on some day j, where i < j.

The intuition behind this approach is that we want to buy at the lowest price and sell at the highest price, which corresponds to maximizing the ratio pi/pj. Therefore, we keep track of the minimum price seen so far and calculate the profit that can be obtained by selling at the current price. If this profit is greater than the maximum profit seen so far, it means that we can make a better profit by selling at the current price.

The time complexity of this algorithm is O(n), since we need to process each day only once. Therefore, this algorithm runs in polynomial time in the size of the input.

avatar
lhwww
Shared 1 prompt
Created 1 year ago

Leave a Comment