09.09.2014 Views

algorithms

algorithms

algorithms

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6.8. Given two strings x = x 1 x 2 · · · x n and y = y 1 y 2 · · · y m , we wish to find the length of their longest<br />

common substring, that is, the largest k for which there are indices i and j with x i x i+1 · · · x i+k−1 =<br />

y j y j+1 · · · y j+k−1 . Show how to do this in time O(mn).<br />

6.9. A certain string-processing language offers a primitive operation which splits a string into two<br />

pieces. Since this operation involves copying the original string, it takes n units of time for a<br />

string of length n, regardless of the location of the cut. Suppose, now, that you want to break a<br />

string into many pieces. The order in which the breaks are made can affect the total running<br />

time. For example, if you want to cut a 20-character string at positions 3 and 10, then making<br />

the first cut at position 3 incurs a total cost of 20 + 17 = 37, while doing position 10 first has a<br />

better cost of 20 + 10 = 30.<br />

Give a dynamic programming algorithm that, given the locations of m cuts in a string of length<br />

n, finds the minimum cost of breaking the string into m + 1 pieces.<br />

6.10. Counting heads. Given integers n and k, along with p 1 , . . . , p n ∈ [0, 1], you want to determine the<br />

probability of obtaining exactly k heads when n biased coins are tossed independently at random,<br />

where p i is the probability that the ith coin comes up heads. Give an O(n 2 ) algorithm for this<br />

task. 2 Assume you can multiply and add two numbers in [0, 1] in O(1) time.<br />

6.11. Given two strings x = x 1 x 2 · · · x n and y = y 1 y 2 · · · y m , we wish to find the length of their longest<br />

common subsequence, that is, the largest k for which there are indices i 1 < i 2 < · · · < i k and<br />

j 1 < j 2 < · · · < j k with x i1 x i2 · · · x ik = y j1 y j2 · · · y jk . Show how to do this in time O(mn).<br />

6.12. You are given a convex polygon P on n vertices in the plane (specified by their x and y coordinates).<br />

A triangulation of P is a collection of n − 3 diagonals of P such that no two diagonals<br />

intersect (except possibly at their endpoints). Notice that a triangulation splits the polygon’s<br />

interior into n − 2 disjoint triangles. The cost of a triangulation is the sum of the lengths of the<br />

diagonals in it. Give an efficient algorithm for finding a triangulation of minimum cost. (Hint:<br />

Label the vertices of P by 1, . . . , n, starting from an arbitrary vertex and walking clockwise. For<br />

1 ≤ i < j ≤ n, let the subproblem A(i, j) denote the minimum cost triangulation of the polygon<br />

spanned by vertices i, i + 1, . . . , j.)<br />

6.13. Consider the following game. A “dealer” produces a sequence s 1 · · · s n of “cards,” face up, where<br />

each card s i has a value v i . Then two players take turns picking a card from the sequence, but<br />

can only pick the first or the last card of the (remaining) sequence. The goal is to collect cards of<br />

largest total value. (For example, you can think of the cards as bills of different denominations.)<br />

Assume n is even.<br />

(a) Show a sequence of cards such that it is not optimal for the first player to start by picking<br />

up the available card of larger value. That is, the natural greedy strategy is suboptimal.<br />

(b) Give an O(n 2 ) algorithm to compute an optimal strategy for the first player. Given the<br />

initial sequence, your algorithm should precompute in O(n 2 ) time some information, and<br />

then the first player should be able to make each move optimally in O(1) time by looking<br />

up the precomputed information.<br />

6.14. Cutting cloth. You are given a rectangular piece of cloth with dimensions X × Y , where X and<br />

Y are positive integers, and a list of n products that can be made using the cloth. For each<br />

product i ∈ [1, n] you know that a rectangle of cloth of dimensions a i × b i is needed and that the<br />

final selling price of the product is c i . Assume the a i , b i , and c i are all positive integers. You<br />

have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or<br />

vertically. Design an algorithm that determines the best return on the X × Y piece of cloth, that<br />

is, a strategy for cutting the cloth so that the products made from the resulting pieces give the<br />

2 In fact, there is also a O(n log 2 n) algorithm within your reach.<br />

183

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!