# Difference between revisions of "Divide-TADM2E"

(Recovering wiki) |
(Recovering wiki) |
||

Line 5: | Line 5: | ||

'''Edit Distance''' | '''Edit Distance''' | ||

− | + | <br>8-1. | |

Typists often make transposition errors exchanging | Typists often make transposition errors exchanging | ||

neighboring characters, such as typing ''setve'' when you mean ''steve.'' | neighboring characters, such as typing ''setve'' when you mean ''steve.'' | ||

Line 16: | Line 16: | ||

[[TADM2E 8.1|(Solution 8.1)]] | [[TADM2E 8.1|(Solution 8.1)]] | ||

− | + | <br>8-2. | |

− | Suppose you are given three strings of characters: | + | Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, |

− | where | + | 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 | + | of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> |

− | and | + | and <math>Y</math> in a way that maintains the left-to-right ordering of the characters |

from each string. | from each string. | ||

#Show that ''cchocohilaptes'' is a shuffle of ''chocolate'' and ''chips'', but ''chocochilatspe'' is not. | #Show that ''cchocohilaptes'' is a shuffle of ''chocolate'' and ''chips'', but ''chocochilatspe'' is not. | ||

− | #Give an efficient dynamic-programming algorithm that determines whether | + | #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 | + | 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 | is the longest string that appears as a run of consecutive letters | ||

in both strings. | in both strings. | ||

For example, the longest common substring of | For example, the longest common substring of | ||

''photograph'' and ''tomography'' is ''ograph''. | ''photograph'' and ''tomography'' is ''ograph''. | ||

− | #Let | + | #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 | + | #Give a simpler <math>\Theta(nm)</math> algorithm that does not rely on dynamic programming. |

[[TADM2E 8.3|(Solution 8.3)]] | [[TADM2E 8.3|(Solution 8.3)]] | ||

− | + | <br>8-4. | |

The ''longest common subsequence (LCS)'' | The ''longest common subsequence (LCS)'' | ||

− | of two sequences | + | of two sequences <math>T</math> and <math>P</math> is the longest |

− | sequence | + | 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 | + | The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> |

− | such that both | + | 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. | #Give efficient algorithms to find the LCS and SCS of two given sequences. | ||

− | #Let | + | #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>. |

Line 50: | Line 50: | ||

'''Greedy Algorithms''' | '''Greedy Algorithms''' | ||

− | + | <br>8-5. | |

− | Let | + | Let <math>P_1 ,P_2, \ldots, P_n</math> be <math>n</math> programs to be stored on a disk |

− | with capacity | + | with capacity <math>D</math> megabytes. |

− | Program | + | Program <math>P_i</math> |

− | requires | + | requires <math>s_i</math> megabytes of storage. |

− | We cannot store them all because | + | 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 | + | #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. |

− | #Does a greedy algorithm that selects programs in order of nonincreasing order | + | #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. |

[[TADM2E 8.5|(Solution 8.5)]] | [[TADM2E 8.5|(Solution 8.5)]] | ||

− | + | <br>8-6. | |

Coins in the United States are minted with denominations of | Coins in the United States are minted with denominations of | ||

1, 5, 10, 25, and 50 cents. | 1, 5, 10, 25, and 50 cents. | ||

Now consider a country whose coins are minted with | Now consider a country whose coins are minted with | ||

− | denominations of | + | denominations of <math>\{d_1, \ldots, d_k \}</math> units. |

− | We seek an algorithm to make change of | + | We seek an algorithm to make change of <math>n</math> units |

using the minimum number of coins for this country. | using the minimum number of coins for this country. | ||

− | + | <br> | |

(a) The greedy algorithm repeatedly selects the biggest coin | (a) The greedy algorithm repeatedly selects the biggest coin | ||

no bigger than the amount to be changed and repeats until it is zero. | 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 | Show that the greedy algorithm does not always use the minimum number of | ||

− | coins in a country whose denominations are | + | coins in a country whose denominations are <math>\{1,6,10\}</math>. |

− | + | <br> | |

(b) Give an efficient algorithm that correctly determines the minimum number | (b) Give an efficient algorithm that correctly determines the minimum number | ||

− | of coins needed to make change of | + | of coins needed to make change of <math>n</math> units using |

− | denominations | + | denominations <math>\{d_1, \ldots, d_k \}</math>. |

Analyze its running time. | Analyze its running time. | ||

− | + | <br>8-7. | |

In the United States, coins are minted with denominations of | In the United States, coins are minted with denominations of | ||

1, 5, 10, 25, and 50 cents. | 1, 5, 10, 25, and 50 cents. | ||

Now consider a country whose coins are minted with | Now consider a country whose coins are minted with | ||

− | denominations of | + | denominations of <math>\{d_1, \ldots, d_k \}</math> units. |

− | We want to count how many distinct ways | + | We want to count how many distinct ways <math>C(n)</math> |

− | there are to make change of | + | there are to make change of <math>n</math> units. |

− | For example, in a country whose denominations are | + | 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 | + | #How many ways are there to make change of 20 units from <math>\{1,6,10\}</math>? |

− | #Give an efficient algorithm to compute | + | #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.) |

[[TADM2E 8.7|(Solution 8.7)]] | [[TADM2E 8.7|(Solution 8.7)]] | ||

− | + | <br>8-8. | |

In the ''single-processor scheduling problem'', we are | In the ''single-processor scheduling problem'', we are | ||

− | given a set of | + | given a set of <math>n</math> jobs <math>J</math>. |

− | Each job | + | 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 | A feasible schedule is a permutation of the jobs | ||

such that when the jobs are performed in that order, every job is | such that when the jobs are performed in that order, every job is | ||

Line 109: | Line 109: | ||

'''Number Problems''' | '''Number Problems''' | ||

− | + | <br>8-9. | |

The ''knapsack problem'' | The ''knapsack problem'' | ||

− | is as follows: given a set of integers | + | is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and |

− | a given target number | + | a given target number <math>T</math>, find a subset of <math>S</math> that adds up |

− | exactly to | + | exactly to <math>T</math>. |

− | For example, within | + | For example, within <math>S = \{1, 2, 5, 9, 10\}</math> there is a subset that |

− | adds up to | + | adds up to <math>T=22</math> but not <math>T=23</math>. |

− | Give a correct programming algorithm for knapsack that runs in | + | Give a correct programming algorithm for knapsack that runs in <math>O(n T)</math> time. |

[[TADM2E 8.9|(Solution 8.9)]] | [[TADM2E 8.9|(Solution 8.9)]] | ||

− | + | <br>8-10. | |

The ''integer partition'' takes a set of positive | The ''integer partition'' takes a set of positive | ||

− | integers | + | integers <math>S=s_1, \ldots, s_n</math> and asks if there is a subset <math>I \in S</math> |

such that | such that | ||

− | + | <math> \sum_{i \in I} s_i= \sum_{ i \notin I} s_i </math> | |

− | Let | + | Let <math>\sum_{i \in S} s_i = M</math>. |

− | Give an | + | Give an <math>O(nM)</math> dynamic programming algorithm to solve the |

integer partition problem. | integer partition problem. | ||

− | + | <br>8-11. | |

− | Assume that there are | + | Assume that there are <math>n</math> numbers (some possibly negative) |

on a circle, and we wish to find the maximum contiguous sum | on a circle, and we wish to find the maximum contiguous sum | ||

along an arc of the circle. | along an arc of the circle. | ||

Line 137: | Line 137: | ||

[[TADM2E 8.11|(Solution 8.11)]] | [[TADM2E 8.11|(Solution 8.11)]] | ||

− | + | <br>8-12. | |

A certain string processing language allows the programmer to break a | A certain string processing language allows the programmer to break a | ||

string into two pieces. | string into two pieces. | ||

It | It | ||

− | costs | + | 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. | pieces, since this involves copying the old string. | ||

A programmer wants to break a string into many pieces, and the | A programmer wants to break a string into many pieces, and the | ||

Line 157: | Line 157: | ||

Give a dynamic | Give a dynamic | ||

programming algorithm that takes a list of character positions | programming algorithm that takes a list of character positions | ||

− | after which to break and determines the cheapest break cost in | + | 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. | Consider the following data compression technique. | ||

− | We have a table of | + | 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 | + | We want to encode a data string <math>D</math> of length <math>n</math> using as few text strings |

as possible. | as possible. | ||

For example, if our table contains {\em(a,ba,abab,b)} | For example, if our table contains {\em(a,ba,abab,b)} | ||

and the data string is ''bababbaababa'', the best way to encode it | 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. | is {\em(b,abab,ba,abab,a)}---a total of five code words. | ||

− | Give an | + | 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. | You may assume that every string has at least one encoding in terms of the table. | ||

[[TADM2E 8.13|(Solution 8.13)]] | [[TADM2E 8.13|(Solution 8.13)]] | ||

− | + | <br>8-14. | |

The traditional world chess championship is a match of 24 games. | The traditional world chess championship is a match of 24 games. | ||

The current champion retains the title in case the match is a tie. | 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 | Each game ends in a win, loss, or draw (tie) where wins count | ||

− | as | + | as <math>1</math>, losses as <math>0</math>, and draws as <math>1/2</math>. |

The players take turns playing white and black. | The players take turns playing white and black. | ||

White has an advantage, because he moves first. | White has an advantage, because he moves first. | ||

The champion plays white in the first game. | The champion plays white in the first game. | ||

He has probabilities | 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, | of winning, drawing, and losing playing white, | ||

and has probabilities | 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. | of winning, drawing, and losing playing black. | ||

− | #Write a recurrence for the probability that the champion retains the title. Assume that there are | + | #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. | #Based on your recurrence, give a dynamic programming to calculate the champion's probability of retaining the title. | ||

− | #Analyze its running time for an | + | #Analyze its running time for an <math>n</math> game match. |

− | + | <br>8-15. | |

Eggs break when dropped from great enough height. | Eggs break when dropped from great enough height. | ||

− | Specifically, there must be a floor | + | Specifically, there must be a floor <math>f</math> in any sufficiently tall building |

such that | such that | ||

− | an egg dropped from the | + | an egg dropped from the <math>f</math>th floor breaks, |

− | but one dropped from the | + | but one dropped from the <math>(f-1)</math>st floor will not. |

− | If the egg always breaks, then | + | If the egg always breaks, then <math>f=1</math>. |

− | If the egg never breaks, then | + | If the egg never breaks, then <math>f=n+1</math>. |

− | You seek to find the critical floor | + | 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 | The only operation you can perform is to drop an egg | ||

off some floor and see what happens. | off some floor and see what happens. | ||

− | You start out with | + | You start out with <math>k</math> eggs, and seek to drop eggs as few times as possible. |

Broken eggs cannot be reused. | Broken eggs cannot be reused. | ||

− | Let | + | Let <math>E(k,n)</math> be the minimum number of egg droppings that will always |

suffice. | suffice. | ||

− | #Show that | + | #Show that <math>E(1,n)=n</math>. |

− | #Show that | + | #Show that <math>E(k,n)=\Theta(n^{\frac{1}{k}})</math>. |

− | #Find a recurrence for | + | #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>? |

[[TADM2E 8.15|(Solution 8.15)]] | [[TADM2E 8.15|(Solution 8.15)]] | ||

Line 215: | Line 215: | ||

'''Graph Problems''' | '''Graph Problems''' | ||

− | + | <br>8-16. | |

− | Consider a city whose streets are defined by an | + | 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 | We are interested in walking from the upper left-hand corner of the | ||

grid to the lower right-hand corner. | grid to the lower right-hand corner. | ||

Unfortunately, the city has bad neighborhoods, whose | Unfortunately, the city has bad neighborhoods, whose | ||

intersections we do not want to walk in. | intersections we do not want to walk in. | ||

− | We are given an | + | We are given an <math>X \times Y</math> matrix ''BAD'', where ''BAD[i,j] = ''yes'''' |

− | if and only if the intersection between streets | + | if and only if the intersection between streets <math>i</math> and <math>j</math> is |

in a neighborhood to avoid. | in a neighborhood to avoid. | ||

− | + | <br> | |

(a) Give an example of the contents of ''BAD'' such that there is no | (a) Give an example of the contents of ''BAD'' such that there is no | ||

path across the grid avoiding bad neighborhoods. | path across the grid avoiding bad neighborhoods. | ||

− | + | <br> | |

− | (b) Give an | + | (b) Give an <math>O( X Y )</math> algorithm to find a path across the grid that |

avoids bad neighborhoods. | avoids bad neighborhoods. | ||

− | + | <br> | |

− | (c) Give an | + | (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 | the grid that avoids bad neighborhoods. You may assume that all blocks | ||

are of equal length. | are of equal length. | ||

− | For partial credit, give an | + | For partial credit, give an <math>O(X^2 Y^2)</math> algorithm. |

− | + | <br>8-17. | |

Consider the same situation as the previous problem. | Consider the same situation as the previous problem. | ||

− | We have a city whose streets are defined by an | + | 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 | We are interested in walking from the upper left-hand corner of the | ||

grid to the lower right-hand corner. | grid to the lower right-hand corner. | ||

− | We are given an | + | We are given an <math>X \times Y</math> matrix ''BAD'', where ''BAD[i,j] = ''yes'''' |

− | if and only if the intersection between streets | + | if and only if the intersection between streets <math>i</math> and <math>j</math> is somewhere |

we want to avoid. | we want to avoid. | ||

If there were no bad neighborhoods to contend with, the shortest | If there were no bad neighborhoods to contend with, the shortest | ||

− | path across the grid would have length | + | 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 | indeed there would be many such paths across the grid. Each path | ||

would consist of only rightward and downward moves. | would consist of only rightward and downward moves. | ||

Give an algorithm that takes the array ''BAD'' and returns the ''number'' | Give an algorithm that takes the array ''BAD'' and returns the ''number'' | ||

− | of safe paths of length | + | of safe paths of length <math>X+Y-2</math>. |

− | For full credit, your algorithm must run in | + | For full credit, your algorithm must run in <math>O( X Y )</math>. |

[[TADM2E 8.17|(Solution 8.17)]] | [[TADM2E 8.17|(Solution 8.17)]] | ||

Line 258: | Line 258: | ||

'''Design Problems''' | '''Design Problems''' | ||

− | + | <br>8-18. | |

− | Consider the problem of storing | + | 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 | The order of the books is fixed by the cataloging system and so cannot | ||

be rearranged. | be rearranged. | ||

Therefore, we | Therefore, we | ||

− | can speak of a book | + | can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a |

− | thickness | + | thickness <math>t_i</math> and height <math>h_i</math>. |

− | The length of each bookshelf at this library is | + | The length of each bookshelf at this library is <math>L</math>. |

− | Suppose all the books have the same height | + | 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 | The greedy algorithm would fill the first shelf with as many books as we can | ||

− | until we get the smallest | + | until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat |

with subsequent shelves. | with subsequent shelves. | ||

Show that the greedy algorithm always finds the optimal shelf | Show that the greedy algorithm always finds the optimal shelf | ||

placement, and analyze its time complexity. | placement, and analyze its time complexity. | ||

− | + | <br>8-19. | |

This is a generalization of the previous problem. | This is a generalization of the previous problem. | ||

Now consider the case where the height of the books is not constant, | Now consider the case where the height of the books is not constant, | ||

Line 286: | Line 286: | ||

[[TADM2E 8.19|(Solution 8.19)]] | [[TADM2E 8.19|(Solution 8.19)]] | ||

− | + | <br>8-20. | |

− | We wish to compute the laziest way to dial given | + | 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 | 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 | fingers start out on the * and \# keys, and that the effort required to | ||

Line 293: | Line 293: | ||

distance between them. Design an algorithm that computes | distance between them. Design an algorithm that computes | ||

the method of dialing that involves moving your fingers the | the method of dialing that involves moving your fingers the | ||

− | smallest amount of total distance, where | + | smallest amount of total distance, where <math>k</math> is the number of distinct |

− | keys on the keypad ( | + | keys on the keypad (<math>k=16</math> for standard telephones). |

− | Try to use | + | Try to use <math>O(n k^3)</math> time. |

− | + | <br>8-21. | |

− | Given an array of | + | Given an array of <math>n</math> real numbers, consider the problem of |

finding the maximum sum in any | finding the maximum sum in any | ||

contiguous subvector of the input. | contiguous subvector of the input. | ||

− | For example, in the array | + | 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, | the maximum is achieved by summing the third through seventh elements, | ||

− | where | + | where <math>59+26+(-53)+58+97 = 187</math>. |

When all numbers are positive, the entire array is the answer, | When all numbers are positive, the entire array is the answer, | ||

while when all numbers are negative, the empty array maximizes the total | while when all numbers are negative, the empty array maximizes the total | ||

at 0. | at 0. | ||

− | #Give a simple, clear, and correct | + | #Give a simple, clear, and correct <math>\Theta(n^2)</math>-time algorithm to find the maximum contiguous subvector. |

− | #Now give a | + | #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. |

[[TADM2E 8.21|(Solution 8.21)]] | [[TADM2E 8.21|(Solution 8.21)]] | ||

− | + | <br>8-22. | |

Consider the problem of examining a string | 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. | symbols, and a multiplication table over this alphabet. | ||

Decide | Decide | ||

− | whether or not it is possible to parenthesize | + | whether or not it is possible to parenthesize <math>x</math> in such a way that |

− | the value of the resulting expression is | + | the value of the resulting expression is <math>a</math>, where <math>a</math> belongs to the |

alphabet. | alphabet. | ||

The multiplication table is neither commutative or associative, so the | The multiplication table is neither commutative or associative, so the | ||

order of multiplication matters. | order of multiplication matters. | ||

\vspace{0.1in} | \vspace{0.1in} | ||

− | + | <center> | |

− | + | <math>\begin{array}{c|ccc} | |

− | & | + | & a & b & c \\ |

\hline | \hline | ||

− | a & | + | a & a & c & c \\ |

− | b & | + | b & a & a & b \\ |

− | c & | + | c & c & c & c \\ |

− | \end{array} | + | \end{array}</math> |

− | + | </center> | |

For example, consider the above multiplication table and the string | For example, consider the above multiplication table and the string | ||

− | + | <math>bbbba</math>. | |

− | Parenthesizing it | + | 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 | + | 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 | such a parenthesization exists for a given string, multiplication table, and | ||

goal element. | goal element. | ||

− | + | <br>8-23. | |

− | Let | + | 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, | Devise an algorithm that builds a tree with optimal worst case cost, | ||

− | given keys | + | given keys <math>k_1,\ldots,k_n</math> and the probabilities that each will be |

− | searched | + | searched <math>p_1,\ldots,p_n</math>. |

[[TADM2E 8.23|(Solution 8.23)]] | [[TADM2E 8.23|(Solution 8.23)]] | ||

Line 350: | Line 350: | ||

'''Interview Problems''' | '''Interview Problems''' | ||

− | + | <br>8-24. | |

Given a set of coin denominators, find the minimum number of coins to | Given a set of coin denominators, find the minimum number of coins to | ||

make a certain amount of change. | make a certain amount of change. | ||

Line 356: | Line 356: | ||

[[TADM2E 8.24|(Solution 8.24)]] | [[TADM2E 8.24|(Solution 8.24)]] | ||

− | + | <br>8-25. | |

− | You are given an array of | + | You are given an array of <math>n</math> numbers, each of which may be positive, negative, |

or zero. | or zero. | ||

− | Give an efficient algorithm to identify the index positions | + | Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math> |

− | to the maximum sum of the | + | to the maximum sum of the <math>i</math>th through <math>j</math>th numbers. |

[[TADM2E 8.25|(Solution 8.25)]] | [[TADM2E 8.25|(Solution 8.25)]] | ||

− | + | <br>8-26. | |

Observe that | Observe that | ||

when you cut a character out of a | when you cut a character out of a |

## Revision as of 18:23, 11 September 2014

# Dynamic Programming

**Edit Distance**

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.

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

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

8-3.
The longest common *substring* (not subsequence) of two strings $ X $ and $ Y $
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 $ n=|X| $ and $ m=|Y| $. Give a $ \Theta(nm) $ dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.
- Give a simpler $ \Theta(nm) $ algorithm that does not rely on dynamic programming.

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

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

**Greedy Algorithms**

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

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

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 $ \{d_1, \ldots, d_k \} $ units.
We seek an algorithm to make change of $ n $ units
using the minimum number of coins for this country.

(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 $ \{1,6,10\} $.

(b) Give an efficient algorithm that correctly determines the minimum number
of coins needed to make change of $ n $ units using
denominations $ \{d_1, \ldots, d_k \} $.
Analyze its running time.

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 $ \{d_1, \ldots, d_k \} $ units.
We want to count how many distinct ways $ C(n) $
there are to make change of $ n $ units.
For example, in a country whose denominations are $ \{1,6,10\} $,
$ C(5) = 1 $, $ C(6) $ to $ C(9)=2 $, $ C(10)=3 $, and $ C(12)=4 $.

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

8-8.
In the *single-processor scheduling problem*, we are
given a set of $ n $ jobs $ J $.
Each job $ i $ has a processing time $ t_i $, and a deadline $ d_i $.
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**

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

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

8-11.
Assume that there are $ n $ 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.

8-12.
A certain string processing language allows the programmer to break a
string into two pieces.
It
costs $ n $ units of time to break a string of $ n $ 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 $ O(n^3) $ time.

8-13.
Consider the following data compression technique.
We have a table of $ m $ text strings, each at most $ k $ in length.
We want to encode a data string $ D $ of length $ n $ 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 $ O(nmk) $ 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.

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 $ 1 $, losses as $ 0 $, and draws as $ 1/2 $.
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
$ w_{\mbox{w}} $, $ w_{\mbox{d}} $, and $ w_{\mbox{l}} $
of winning, drawing, and losing playing white,
and has probabilities
$ b_{\mbox{w}} $, $ b_{\mbox{d}} $, and $ b_{\mbox{l}} $
of winning, drawing, and losing playing black.

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

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

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

**Graph Problems**

8-16.
Consider a city whose streets are defined by an $ X \times Y $ 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 $ X \times Y $ matrix *BAD*, where *BAD[i,j] = *yes'
if and only if the intersection between streets $ i $ and $ j $ is
in a neighborhood to avoid.

(a) Give an example of the contents of *BAD* such that there is no
path across the grid avoiding bad neighborhoods.

(b) Give an $ O( X Y ) $ algorithm to find a path across the grid that
avoids bad neighborhoods.

(c) Give an $ O( X Y ) $ 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 $ O(X^2 Y^2) $ algorithm.

8-17.
Consider the same situation as the previous problem.
We have a city whose streets are defined by an $ X \times Y $ 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 $ X \times Y $ matrix *BAD*, where *BAD[i,j] = *yes'
if and only if the intersection between streets $ i $ and $ j $ is somewhere
we want to avoid.
If there were no bad neighborhoods to contend with, the shortest
path across the grid would have length $ (X-1) + (Y-1) $ 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 $ X+Y-2 $.
For full credit, your algorithm must run in $ O( X Y ) $.

**Design Problems**

8-18.
Consider the problem of storing $ n $ 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 $ b_i $, where $ 1 \leq i \leq n $, that has a
thickness $ t_i $ and height $ h_i $.
The length of each bookshelf at this library is $ L $.
Suppose all the books have the same height $ h $ (i.e., $ h = h_i = h_j $ for all
$ i, j $) and the shelves are all separated by a distance of greater than
$ h $, 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 $ i $ such that $ b_i $ 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.

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.

- 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.

8-20.
We wish to compute the laziest way to dial given $ n $-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 $ k $ is the number of distinct
keys on the keypad ($ k=16 $ for standard telephones).
Try to use $ O(n k^3) $ time.

8-21.
Given an array of $ n $ real numbers, consider the problem of
finding the maximum sum in any
contiguous subvector of the input.
For example, in the array $ \{31,-41,59,26,-53,58,97,-93,-23,84\} $
the maximum is achieved by summing the third through seventh elements,
where $ 59+26+(-53)+58+97 = 187 $.
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 $ \Theta(n^2) $-time algorithm to find the maximum contiguous subvector.
- Now give a $ \Theta(n) $-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a
*correct*$ O(n \log n) $ divide-and-conquer algorithm.

8-22.
Consider the problem of examining a string
$ x = x_1 x_2 \ldots x_n $ from an alphabet of $ k $
symbols, and a multiplication table over this alphabet.
Decide
whether or not it is possible to parenthesize $ x $ in such a way that
the value of the resulting expression is $ a $, where $ a $ belongs to the
alphabet.
The multiplication table is neither commutative or associative, so the
order of multiplication matters.
\vspace{0.1in}

$ \begin{array}{c|ccc} & a & b & c \\ \hline a & a & c & c \\ b & a & a & b \\ c & c & c & c \\ \end{array} $

For example, consider the above multiplication table and the string $ bbbba $. Parenthesizing it $ (b(bb))(ba) $ gives $ a $, but $ ((((bb)b)b)a) $ gives $ c $. Give an algorithm, with time polynomial in $ n $ and $ k $, to decide whether such a parenthesization exists for a given string, multiplication table, and goal element.

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

**Interview Problems**

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

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

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.