Difference between revisions of "DivideTADM2E"
(Recovering wiki) 
(No difference)

Revision as of 18:14, 11 September 2014
Dynamic Programming
Edit Distance
<br>81. 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.
<br>82. 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 lefttoright ordering of the characters from each string.
 Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
 Give an efficient dynamicprogramming 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>83. 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.
 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.
 Give a simpler <math>\Theta(nm)</math> algorithm that does not rely on dynamic programming.
<br>84. 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>.
 Give efficient algorithms to find the LCS and SCS of two given sequences.
 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>85. 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>
 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 counterexample.
 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 counterexample.
<br>86. 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>87. 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>.
 How many ways are there to make change of 20 units from <math>\{1,6,10\}</math>?
 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.)
<br>88. In the singleprocessor 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 singleprocessor 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>89. 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.
<br>810. 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>811. 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.
<br>812. 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 20character string after characters 3, 8, and 10. If the breaks are made in leftright 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 rightleft 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>813. 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.
<br>814. 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.
 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>).
 Based on your recurrence, give a dynamic programming to calculate the champion's probability of retaining the title.
 Analyze its running time for an <math>n</math> game match.
<br>815. 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>(f1)</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.
 Show that <math>E(1,n)=n</math>.
 Show that <math>E(k,n)=\Theta(n^{\frac{1}{k}})</math>.
 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>?
Graph Problems
<br>816. Consider a city whose streets are defined by an <math>X \times Y</math> grid. We are interested in walking from the upper lefthand corner of the grid to the lower righthand 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>817. 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 lefthand corner of the grid to the lower righthand 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>(X1) + (Y1)</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+Y2</math>. For full credit, your algorithm must run in <math>O( X Y )</math>.
Design Problems
<br>818. 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>819. 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.
 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.
 Give an algorithm for this problem, and analyze its time complexity. Hint: use dynamic programming.
<br>820. We wish to compute the laziest way to dial given <math>n</math>digit number on a standard pushbutton 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>821. 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.
 Give a simple, clear, and correct <math>\Theta(n^2)</math>time algorithm to find the maximum contiguous subvector.
 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> divideandconquer algorithm.
<br>822. 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}{cccc} & 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>823. 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>.
Interview Problems
<br>824. Given a set of coin denominators, find the minimum number of coins to make a certain amount of change.
<br>825. 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.
<br>826. 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.