Difference between revisions of "Divide-TADM2E"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
(No difference)

Revision as of 18:14, 11 September 2014

Dynamic Programming

Edit Distance

<br>8-1. Typists often make transposition errors exchanging neighboring characters, such as typing setve when you mean steve. This requires two substitutions to fix under the conventional definition of edit distance. Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation.

(Solution 8.1)

<br>8-2. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y|=m</math>, and <math>|Z|=n+m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string.

  1. Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
  2. Give an efficient dynamic-programming algorithm that determines whether <math>Z</math> is a shuffle of <math>X</math> and <math>Y</math>. Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.

<br>8-3. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph.

  1. Let <math>n=|X|</math> and <math>m=|Y|</math>. Give a <math>\Theta(nm)</math> dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.
  2. Give a simpler <math>\Theta(nm)</math> algorithm that does not rely on dynamic programming.

(Solution 8.3)

<br>8-4. The longest common subsequence (LCS) of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The shortest common supersequence (SCS) of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>.

  1. Give efficient algorithms to find the LCS and SCS of two given sequences.
  2. Let <math>d(T,P)</math> be the minimum edit distance between <math>T</math> and <math>P</math> when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that <math>d(T,P)=|SCS(T,P)|-|LCS(T,P)|</math> where <math>|SCS(T,P)|</math> (<math>|LCS(T,P)|</math>) is the size of the shortest <math>SCS</math> (longest LCS) of <math>T</math> and <math>P</math>.


Greedy Algorithms

<br>8-5. Let <math>P_1 ,P_2, \ldots, P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i</math>

  1. Does a greedy algorithm that selects programs in order of nondecreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example.
  2. Does a greedy algorithm that selects programs in order of nonincreasing order <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example.

(Solution 8.5)

<br>8-6. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>\{d_1, \ldots, d_k \}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of coins for this country. <br> (a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are <math>\{1,6,10\}</math>. <br> (b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>\{d_1, \ldots, d_k \}</math>. Analyze its running time.

<br>8-7. In the United States, coins are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>\{d_1, \ldots, d_k \}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are <math>\{1,6,10\}</math>, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9)=2</math>, <math>C(10)=3</math>, and <math>C(12)=4</math>.

  1. How many ways are there to make change of 20 units from <math>\{1,6,10\}</math>?
  2. Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n,d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.)

(Solution 8.7)

<br>8-8. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible.


Number Problems

<br>8-9. The knapsack problem is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = \{1, 2, 5, 9, 10\}</math> there is a subset that adds up to <math>T=22</math> but not <math>T=23</math>. Give a correct programming algorithm for knapsack that runs in <math>O(n T)</math> time.

(Solution 8.9)

<br>8-10. The integer partition takes a set of positive integers <math>S=s_1, \ldots, s_n</math> and asks if there is a subset <math>I \in S</math> such that <math> \sum_{i \in I} s_i= \sum_{ i \notin I} s_i </math> Let <math>\sum_{i \in S} s_i = M</math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem.

<br>8-11. Assume that there are <math>n</math> numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem.

(Solution 8.11)

<br>8-12. A certain string processing language allows the programmer to break a string into two pieces. It costs <math>n</math> units of time to break a string of <math>n</math> characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 steps. If the breaks are made in right-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 steps. Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time.

<br>8-13. Consider the following data compression technique. We have a table of <math>m</math> text strings, each at most <math>k</math> in length. We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains {\em(a,ba,abab,b)} and the data string is bababbaababa, the best way to encode it is {\em(b,abab,ba,abab,a)}---a total of five code words. Give an <math>O(nmk)</math> algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table.

(Solution 8.13)

<br>8-14. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as <math>1</math>, losses as <math>0</math>, and draws as <math>1/2</math>. The players take turns playing white and black. White has an advantage, because he moves first. The champion plays white in the first game. He has probabilities <math>w_{\mbox{w}}</math>, <math>w_{\mbox{d}}</math>, and <math>w_{\mbox{l}}</math> of winning, drawing, and losing playing white, and has probabilities <math>b_{\mbox{w}}</math>, <math>b_{\mbox{d}}</math>, and <math>b_{\mbox{l}}</math> of winning, drawing, and losing playing black.

  1. Write a recurrence for the probability that the champion retains the title. Assume that there are <math>g</math> games left to play in the match and that the champion needs to win <math>i</math> games (which may end in a <math>1/2</math>).
  2. Based on your recurrence, give a dynamic programming to calculate the champion's probability of retaining the title.
  3. Analyze its running time for an <math>n</math> game match.

<br>8-15. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f-1)</math>st floor will not. If the egg always breaks, then <math>f=1</math>. If the egg never breaks, then <math>f=n+1</math>. You seek to find the critical floor <math>f</math> using an <math>n</math>-story building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to drop eggs as few times as possible. Broken eggs cannot be reused. Let <math>E(k,n)</math> be the minimum number of egg droppings that will always suffice.

  1. Show that <math>E(1,n)=n</math>.
  2. Show that <math>E(k,n)=\Theta(n^{\frac{1}{k}})</math>.
  3. Find a recurrence for <math>E(k,n)</math>. What is the running time of the dynamic program to find <math>E(k,n)</math>?

(Solution 8.15)


Graph Problems

<br>8-16. Consider a city whose streets are defined by an <math>X \times Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an <math>X \times Y</math> matrix BAD, where BAD[i,j] = yes' if and only if the intersection between streets <math>i</math> and <math>j</math> is in a neighborhood to avoid. <br> (a) Give an example of the contents of BAD such that there is no path across the grid avoiding bad neighborhoods. <br> (b) Give an <math>O( X Y )</math> algorithm to find a path across the grid that avoids bad neighborhoods. <br> (c) Give an <math>O( X Y )</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2 Y^2)</math> algorithm.

<br>8-17. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X \times Y</math> grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an <math>X \times Y</math> matrix BAD, where BAD[i,j] = yes' if and only if the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid. If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X-1) + (Y-1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves. Give an algorithm that takes the array BAD and returns the number of safe paths of length <math>X+Y-2</math>. For full credit, your algorithm must run in <math>O( X Y )</math>.

(Solution 8.17)


Design Problems

<br>8-18. Consider the problem of storing <math>n</math> books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>. Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i = h_j</math> for all <math>i, j</math>) and the shelves are all separated by a distance of greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the optimal shelf placement, and analyze its time complexity.

<br>8-19. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Thus the cost of a particular layout is the sum of the heights of the largest book on each shelf.

  1. Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height.
  2. Give an algorithm for this problem, and analyze its time complexity. Hint: use dynamic programming.

(Solution 8.19)

<br>8-20. We wish to compute the laziest way to dial given <math>n</math>-digit number on a standard push-button telephone using two fingers. We assume that the two fingers start out on the * and \# keys, and that the effort required to move a finger from one button to another is proportional to the Euclidean distance between them. Design an algorithm that computes the method of dialing that involves moving your fingers the smallest amount of total distance, where <math>k</math> is the number of distinct keys on the keypad (<math>k=16</math> for standard telephones). Try to use <math>O(n k^3)</math> time.

<br>8-21. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subvector of the input. For example, in the array <math>\{31,-41,59,26,-53,58,97,-93,-23,84\}</math> the maximum is achieved by summing the third through seventh elements, where <math>59+26+(-53)+58+97 = 187</math>. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0.

  1. Give a simple, clear, and correct <math>\Theta(n^2)</math>-time algorithm to find the maximum contiguous subvector.
  2. Now give a <math>\Theta(n)</math>-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct <math>O(n \log n)</math> divide-and-conquer algorithm.

(Solution 8.21)

<br>8-22. Consider the problem of examining a string <math>x = x_1 x_2 \ldots x_n</math> from an alphabet of <math>k</math> symbols, and a multiplication table over this alphabet. Decide whether or not it is possible to parenthesize <math>x</math> in such a way that the value of the resulting expression is <math>a</math>, where <math>a</math> belongs to the alphabet. The multiplication table is neither commutative or associative, so the order of multiplication matters. \vspace{0.1in} <center> <math>\begin{array}{c|ccc} & a & b & c \\ \hline a & a & c & c \\ b & a & a & b \\ c & c & c & c \\ \end{array}</math> </center> For example, consider the above multiplication table and the string <math>bbbba</math>. Parenthesizing it <math>(b(bb))(ba)</math> gives <math>a</math>, but <math>((((bb)b)b)a)</math> gives <math>c</math>. Give an algorithm, with time polynomial in <math>n</math> and <math>k</math>, to decide whether such a parenthesization exists for a given string, multiplication table, and goal element.

<br>8-23. Let <math>\alpha</math> and <math>\beta</math> be constants. Assume that it costs <math>\alpha</math> to go left in a tree, and <math>\beta</math> to go right. Devise an algorithm that builds a tree with optimal worst case cost, given keys <math>k_1,\ldots,k_n</math> and the probabilities that each will be searched <math>p_1,\ldots,p_n</math>.

(Solution 8.23)

Interview Problems

<br>8-24. Given a set of coin denominators, find the minimum number of coins to make a certain amount of change.

(Solution 8.24)

<br>8-25. You are given an array of <math>n</math> numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> to the maximum sum of the <math>i</math>th through <math>j</math>th numbers.

(Solution 8.25)

<br>8-26. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position.