alg quiz
Consider the following generalization of the Activity Selection Problem: You are given a set of n activities each with a start time si, a finish time fi, and a weight wi. Design a dynamic programming algorithm to find the weight of a set of non-conflicting activities with maximum weight.
Formula: (Sort by finish time) A[i] = max (from activity 1 to i) { A[i - 1] max{A[x]} + wi } (x being activity whose finish time <= activity i's start time)
Firestones is considering opening a series of restaurants along Highway 1. The n possible locations are along the highway, and the distances of these locations from the downtown San Luis Obispo are, in miles and in increasing order, m1, m2, ..., mn. The constraints are: • At each location, Firestones may open at most one restaurant. The expected profit from opening a restaurant at location i is pi, where pi > 0 and i = 1, 2, . . . , n. • Any two restaurants must be at least k miles apart, where k is a positive integer. Give a dynamic programming algorithm to compute the maximum expected total profit subject to the given constraints
Formula: P[i] = max{ P[i-1] P[i] + max (mi - mj >= k) {P[j]} }
A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. For instance, if S = {5, 15, −30, 10, −5, 40, 10} then {15, −30, 10} is a contiguous subsequence but {5, 15, 40} is not. Give a dynamic programming algorithm for the following task: You are given a list of numbers, {a1, a2, . . . , an}. You should return the contiguous subsequence of maximum sum (a subsequence of length zero has sum zero). For the preceding example, the answer would be 10, −5, 40, 10, with a sum of 55.
Formula: CSH[i] = max{ 0 CSH[i - 1] + Vi }
Given an unlimited supply of coins of denominations x1, x2, . . . , xn, we wish to make change for a value v; that is, we wish to find a set of coins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 then we can make change for 15 but not for 12. Give a dynamic programming algorithm for the following problem: You are given x1, . . . , xn; v. You must answer the following question: Is it possible to make change for v using coins of denominations x1, . . . , xn?
Formula: C[t, k] = min{ S[i-1, l] if l - kai < 0 max{ S[i-1, l - kai], l - kai], S[i-1, l] otherwise note: [0 <= kai <= l] }
When a new gene is discovered, a standard approach to understanding its function is to look through a database of known genes and find close matches. The closeness of two genes is measured by the extent to which they are aligned. To formalize this, think of a gene as being a long string over an alphabet Σ = {A, C, G, T}. Consider two genes (strings) x = AT GCC and y = T ACGCA. An alignment of x and y is a way of matching up these two strings by writing them in columns, for instance: - A T - G C C T A - C G C A
Formula: D[x][y] = max{ D[i - 1][j - 1] + score[String1[i]][String2[j]] D[i - 1][j] + gapScore D[i][j - 1] + gapScore }
You are given a rectangular piece of cloth with dimensions X × Y , where X and Y are positive integers, and a list of n products that can be made using the cloth. For each product i ∈ [1, . . . , n] you know that a rectangle of cloth of dimensions ai × bi is needed and that the final selling price of the product is ci. Assume the ai, bi, and ci are all positive integers. You have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design a dynamic programming algorithm that determines the best return on the X ×Y piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired.
Formula: P(x,y) = max{ P(x,y) max (1 <= h <= X) { P[h, Y] + P(X - h, Y) } max (1 <= v <= Y) { P[X, v] + P[X, Y - v] } }
You are going on a long trip. You start on the road at mile post 0. Along the way there are n hotels, at mile posts a1 < a2 < · · · < an, where each ai is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance an), which is your destination. You would ideally like to travel 300 miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is (300 − x)^2. You want to plan your trip so as to minimize the total penalty-that is, the sum, over all travel days, of the daily penalties. Give a dynamic programming algorithm to determine the optimal sequence of hotels at which to stop.
Formula: P[i] = min (0 <= k <= i) { P[k] + ( 300 - (ak - ai))^2 }
Given two strings x = x1x2 · · · xn and y = y1y2 · · · ym, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with xixi+1 · · · xi+k−1 = yjyj+1 · · · yj+k−1. For example, if x = dcabagc and y = cbcbcabca, then the longest common substring is cab with length 3. Show how to do this in time O(mn).
Formula: S[i][j] = { 0 if i = 0, j = 0, or xi != yj 1 + S[i - 1][j - 1] if xi = yj }
Consider the following task: You are given a set of n positive integers, {a1, a2, . . . an} and some positive integer k. Give a dynamic programming algorithm to determine if there is some subset of the ai's that adds up to k
Formula: S[n, k] = { S[n - 1, k] if k - ai < 0 max{S[n - 1, k - ai], S[n-1, k] otherwise }
Consider the following variation on the change-making problem: you are given denominations x1, x2, . . . , xn, and you want to make change for a value v, but you are allowed to use each denomination at most once. For instance, if the denominations are 1, 5, 10, 20, then you can make change for 16 = 1 + 15 and for 31 = 1 + 10 + 20 but not for 40 (because you can't use 20 twice). You are given positive integers x1, x2, . . . , xn; v. Can you make change for v, using each denomination xi at most once? Show how to solve this problem in time O(nv).
Formula: S[n, k] = { S[n - 1, k] if k - ai < 0 max{S[n - 1, k - ai], S[n-1, k] otherwise }