Difference between revisions of "Divide-TADM2E"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
 
m (Protected "Divide-TADM2E" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)))
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
'''Edit Distance'''
 
'''Edit Distance'''
  
<br>8-1.  
+
<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)]]  
  
&lt;br&gt;8-2.  
+
<br>8-2.  
Suppose you are given three strings of characters: &lt;math&gt;X&lt;/math&gt;, &lt;math&gt;Y&lt;/math&gt;, and &lt;math&gt;Z&lt;/math&gt;,
+
Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>,
where &lt;math&gt;|X| = n&lt;/math&gt;, &lt;math&gt;|Y|=m&lt;/math&gt;, and &lt;math&gt;|Z|=n+m&lt;/math&gt;.
+
where <math>|X| = n</math>, <math>|Y|=m</math>, and <math>|Z|=n+m</math>.
&lt;math&gt;Z&lt;/math&gt; is said to be a ''shuffle''
+
<math>Z</math> is said to be a ''shuffle''
of &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; iff &lt;math&gt;Z&lt;/math&gt; can be formed by interleaving the characters from &lt;math&gt;X&lt;/math&gt;
+
of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math>
and &lt;math&gt;Y&lt;/math&gt; in a way that maintains the left-to-right ordering of the characters
+
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 &lt;math&gt;Z&lt;/math&gt; is a shuffle of &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt;. Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.
+
#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.
  
&lt;br&gt;8-3.  
+
<br>8-3.  
The longest common ''substring'' (not subsequence) of two strings &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt;
+
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 &lt;math&gt;n=|X|&lt;/math&gt; and &lt;math&gt;m=|Y|&lt;/math&gt;. Give a &lt;math&gt;\Theta(nm)&lt;/math&gt; dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.
+
#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 &lt;math&gt;\Theta(nm)&lt;/math&gt; algorithm that does not rely on dynamic programming.
+
#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)]]  
  
&lt;br&gt;8-4.  
+
<br>8-4.  
 
The ''longest common subsequence (LCS)''
 
The ''longest common subsequence (LCS)''
of two sequences &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; is the longest
+
of two sequences <math>T</math> and <math>P</math> is the longest
sequence &lt;math&gt;L&lt;/math&gt; such that &lt;math&gt;L&lt;/math&gt; is a subsequence of both &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt;.
+
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 &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; is the smallest sequence &lt;math&gt;L&lt;/math&gt;
+
The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math>
such that both &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; are a subsequence of &lt;math&gt;L&lt;/math&gt;.
+
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 &lt;math&gt;d(T,P)&lt;/math&gt; be the minimum edit distance between &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that &lt;math&gt;d(T,P)=|SCS(T,P)|-|LCS(T,P)|&lt;/math&gt; where &lt;math&gt;|SCS(T,P)|&lt;/math&gt; (&lt;math&gt;|LCS(T,P)|&lt;/math&gt;) is the size of the shortest &lt;math&gt;SCS&lt;/math&gt; (longest LCS) of &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt;.
+
#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'''
  
&lt;br&gt;8-5.  
+
<br>8-5.  
Let &lt;math&gt;P_1 ,P_2, \ldots, P_n&lt;/math&gt; be &lt;math&gt;n&lt;/math&gt; programs to be stored on a disk
+
Let <math>P_1 ,P_2, \ldots, P_n</math> be <math>n</math> programs to be stored on a disk
with capacity &lt;math&gt;D&lt;/math&gt; megabytes.
+
with capacity <math>D</math> megabytes.
Program &lt;math&gt;P_i&lt;/math&gt;
+
Program <math>P_i</math>
requires &lt;math&gt;s_i&lt;/math&gt; megabytes of storage.
+
requires <math>s_i</math> megabytes of storage.
We cannot store them all because &lt;math&gt;D &lt; \sum_{i=1}^n s_i&lt;/math&gt;
+
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 &lt;math&gt;s_i&lt;/math&gt; 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 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 &lt;math&gt;s_i&lt;/math&gt; use as much of the capacity of the disk as possible? Prove or give a counter-example.
+
#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)]]  
  
&lt;br&gt;8-6.  
+
<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 &lt;math&gt;\{d_1, \ldots, d_k \}&lt;/math&gt; units.
+
denominations of <math>\{d_1, \ldots, d_k \}</math> units.
We seek an algorithm to make change of &lt;math&gt;n&lt;/math&gt; units
+
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.
&lt;br&gt;
+
<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 &lt;math&gt;\{1,6,10\}&lt;/math&gt;.
+
coins in a country whose denominations are <math>\{1,6,10\}</math>.
&lt;br&gt;
+
<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 &lt;math&gt;n&lt;/math&gt; units using
+
of coins needed to make change of <math>n</math> units using
denominations &lt;math&gt;\{d_1, \ldots, d_k \}&lt;/math&gt;.
+
denominations <math>\{d_1, \ldots, d_k \}</math>.
 
Analyze its running time.
 
Analyze its running time.
  
&lt;br&gt;8-7.  
+
<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 &lt;math&gt;\{d_1, \ldots, d_k \}&lt;/math&gt; units.
+
denominations of <math>\{d_1, \ldots, d_k \}</math> units.
We want to count how many distinct ways &lt;math&gt;C(n)&lt;/math&gt;
+
We want to count how many distinct ways <math>C(n)</math>
there are to make change of &lt;math&gt;n&lt;/math&gt; units.
+
there are to make change of <math>n</math> units.
For example, in a country whose denominations are &lt;math&gt;\{1,6,10\}&lt;/math&gt;,
+
For example, in a country whose denominations are <math>\{1,6,10\}</math>,
&lt;math&gt;C(5) = 1&lt;/math&gt;, &lt;math&gt;C(6)&lt;/math&gt; to &lt;math&gt;C(9)=2&lt;/math&gt;, &lt;math&gt;C(10)=3&lt;/math&gt;, and &lt;math&gt;C(12)=4&lt;/math&gt;.
+
<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 &lt;math&gt;\{1,6,10\}&lt;/math&gt;?
+
#How many ways are there to make change of 20 units from <math>\{1,6,10\}</math>?
#Give an efficient algorithm to compute &lt;math&gt;C(n)&lt;/math&gt;, and analyze its complexity. (Hint: think in terms of computing &lt;math&gt;C(n,d)&lt;/math&gt;, the number of ways to make change of &lt;math&gt;n&lt;/math&gt; units with highest denomination &lt;math&gt;d&lt;/math&gt;. Be careful to avoid overcounting.)
+
#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)]]  
  
&lt;br&gt;8-8.  
+
<br>8-8.  
 
In the ''single-processor scheduling problem'', we are
 
In the ''single-processor scheduling problem'', we are
given a set of &lt;math&gt;n&lt;/math&gt; jobs &lt;math&gt;J&lt;/math&gt;.
+
given a set of <math>n</math> jobs <math>J</math>.
Each job &lt;math&gt;i&lt;/math&gt; has a processing time &lt;math&gt;t_i&lt;/math&gt;, and a deadline &lt;math&gt;d_i&lt;/math&gt;.
+
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'''
  
&lt;br&gt;8-9.  
+
<br>8-9.  
 
The ''knapsack problem''
 
The ''knapsack problem''
is as follows: given a set of integers &lt;math&gt;S = \{s_1, s_2, \ldots, s_n\}&lt;/math&gt;, and
+
is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and
a given target number &lt;math&gt;T&lt;/math&gt;, find a subset of &lt;math&gt;S&lt;/math&gt; that adds up
+
a given target number <math>T</math>, find a subset of <math>S</math> that adds up
exactly to &lt;math&gt;T&lt;/math&gt;.
+
exactly to <math>T</math>.
For example, within &lt;math&gt;S = \{1, 2, 5, 9, 10\}&lt;/math&gt; there is a subset that
+
For example, within <math>S = \{1, 2, 5, 9, 10\}</math> there is a subset that
adds up to &lt;math&gt;T=22&lt;/math&gt; but not &lt;math&gt;T=23&lt;/math&gt;.
+
adds up to <math>T=22</math> but not <math>T=23</math>.
Give a correct programming algorithm for knapsack that runs in &lt;math&gt;O(n T)&lt;/math&gt; time.
+
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)]]  
  
&lt;br&gt;8-10.  
+
<br>8-10.  
 
The ''integer partition'' takes a set of positive
 
The ''integer partition'' takes a set of positive
integers &lt;math&gt;S=s_1, \ldots, s_n&lt;/math&gt; and asks if there is a subset &lt;math&gt;I \in S&lt;/math&gt;
+
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
&lt;math&gt; \sum_{i \in I} s_i= \sum_{ i \notin I} s_i &lt;/math&gt;
+
<math> \sum_{i \in I} s_i= \sum_{ i \notin I} s_i </math>
Let &lt;math&gt;\sum_{i \in S} s_i = M&lt;/math&gt;.
+
Let <math>\sum_{i \in S} s_i = M</math>.
Give an &lt;math&gt;O(nM)&lt;/math&gt; dynamic programming algorithm to solve the
+
Give an <math>O(nM)</math> dynamic programming algorithm to solve the
 
integer partition problem.
 
integer partition problem.
  
&lt;br&gt;8-11.  
+
<br>8-11.  
Assume that there are &lt;math&gt;n&lt;/math&gt; numbers (some possibly negative)
+
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)]]  
  
&lt;br&gt;8-12.  
+
<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 &lt;math&gt;n&lt;/math&gt; units of time to break a string of &lt;math&gt;n&lt;/math&gt; characters into two
+
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 &lt;math&gt;O(n^3)&lt;/math&gt; time.
+
after which to break and determines the cheapest break cost in <math>O(n^3)</math> time.
  
&lt;br&gt;8-13.  
+
<br>8-13.  
 
Consider the following data compression technique.
 
Consider the following data compression technique.
We have a table of &lt;math&gt;m&lt;/math&gt; text strings, each at most &lt;math&gt;k&lt;/math&gt; in length.
+
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 &lt;math&gt;D&lt;/math&gt; of length &lt;math&gt;n&lt;/math&gt; using as few text strings
+
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 &lt;math&gt;O(nmk)&lt;/math&gt; algorithm to find the length of the best encoding.
+
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)]]  
  
&lt;br&gt;8-14.  
+
<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 &lt;math&gt;1&lt;/math&gt;, losses as &lt;math&gt;0&lt;/math&gt;, and draws as &lt;math&gt;1/2&lt;/math&gt;.
+
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
&lt;math&gt;w_{\mbox{w}}&lt;/math&gt;, &lt;math&gt;w_{\mbox{d}}&lt;/math&gt;, and &lt;math&gt;w_{\mbox{l}}&lt;/math&gt;
+
<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
&lt;math&gt;b_{\mbox{w}}&lt;/math&gt;, &lt;math&gt;b_{\mbox{d}}&lt;/math&gt;, and &lt;math&gt;b_{\mbox{l}}&lt;/math&gt;
+
<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 &lt;math&gt;g&lt;/math&gt; games left to play in the match and that the champion needs to win &lt;math&gt;i&lt;/math&gt; games (which may end in a &lt;math&gt;1/2&lt;/math&gt;).
+
#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 &lt;math&gt;n&lt;/math&gt; game match.
+
#Analyze its running time for an <math>n</math> game match.
  
&lt;br&gt;8-15.  
+
<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 &lt;math&gt;f&lt;/math&gt; in any sufficiently tall building
+
Specifically, there must be a floor <math>f</math> in any sufficiently tall building
 
such that
 
such that
an egg dropped from the &lt;math&gt;f&lt;/math&gt;th floor breaks,
+
an egg dropped from the <math>f</math>th floor breaks,
but one dropped from the &lt;math&gt;(f-1)&lt;/math&gt;st floor will not.
+
but one dropped from the <math>(f-1)</math>st floor will not.
If the egg always breaks, then &lt;math&gt;f=1&lt;/math&gt;.
+
If the egg always breaks, then <math>f=1</math>.
If the egg never breaks, then &lt;math&gt;f=n+1&lt;/math&gt;.
+
If the egg never breaks, then <math>f=n+1</math>.
You seek to find the critical floor &lt;math&gt;f&lt;/math&gt; using an &lt;math&gt;n&lt;/math&gt;-story building.
+
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 &lt;math&gt;k&lt;/math&gt; eggs, and seek to drop eggs as few times as possible.
+
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 &lt;math&gt;E(k,n)&lt;/math&gt; be the minimum number of egg droppings that will always
+
Let <math>E(k,n)</math> be the minimum number of egg droppings that will always
 
suffice.
 
suffice.
#Show that &lt;math&gt;E(1,n)=n&lt;/math&gt;.
+
#Show that <math>E(1,n)=n</math>.
#Show that &lt;math&gt;E(k,n)=\Theta(n^{\frac{1}{k}})&lt;/math&gt;.
+
#Show that <math>E(k,n)=\Theta(n^{\frac{1}{k}})</math>.
#Find a recurrence for &lt;math&gt;E(k,n)&lt;/math&gt;. What is the running time of the dynamic program to find &lt;math&gt;E(k,n)&lt;/math&gt;?
+
#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'''
  
&lt;br&gt;8-16.  
+
<br>8-16.  
Consider a city whose streets are defined by an &lt;math&gt;X \times Y&lt;/math&gt; grid.
+
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 &lt;math&gt;X \times Y&lt;/math&gt; matrix ''BAD'', where ''BAD[i,j] = ''yes''''
+
We are given an <math>X \times Y</math> matrix ''BAD'', where ''BAD[i,j] = ''yes''''
if and only if the intersection between streets &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; is
+
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.
&lt;br&gt;
+
<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.
&lt;br&gt;
+
<br>
(b) Give an &lt;math&gt;O( X Y )&lt;/math&gt; algorithm to find a path across the grid that
+
(b) Give an <math>O( X Y )</math> algorithm to find a path across the grid that
 
avoids bad neighborhoods.
 
avoids bad neighborhoods.
&lt;br&gt;
+
<br>
(c) Give an &lt;math&gt;O( X Y )&lt;/math&gt; algorithm to find the ''shortest'' path across
+
(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 &lt;math&gt;O(X^2 Y^2)&lt;/math&gt; algorithm.
+
For partial credit, give an <math>O(X^2 Y^2)</math> algorithm.
  
&lt;br&gt;8-17.  
+
<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 &lt;math&gt;X \times Y&lt;/math&gt; grid.
+
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 &lt;math&gt;X \times Y&lt;/math&gt; matrix ''BAD'', where ''BAD[i,j] = ''yes''''
+
We are given an <math>X \times Y</math> matrix ''BAD'', where ''BAD[i,j] = ''yes''''
if and only if the intersection between streets &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; is somewhere
+
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 &lt;math&gt;(X-1) + (Y-1)&lt;/math&gt; blocks, and
+
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 &lt;math&gt;X+Y-2&lt;/math&gt;.
+
of safe paths of length <math>X+Y-2</math>.
For full credit, your algorithm must run in &lt;math&gt;O( X Y )&lt;/math&gt;.
+
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'''
  
&lt;br&gt;8-18.  
+
<br>8-18.  
Consider the problem of storing &lt;math&gt;n&lt;/math&gt; books on shelves in a library.
+
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 &lt;math&gt;b_i&lt;/math&gt;, where &lt;math&gt;1 \leq i \leq n&lt;/math&gt;, that has a
+
can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a
thickness &lt;math&gt;t_i&lt;/math&gt; and height &lt;math&gt;h_i&lt;/math&gt;.
+
thickness <math>t_i</math> and height <math>h_i</math>.
The length of each bookshelf at this library is &lt;math&gt;L&lt;/math&gt;.
+
The length of each bookshelf at this library is <math>L</math>.
Suppose all the books have the same height &lt;math&gt;h&lt;/math&gt; (i.e., &lt;math&gt;h = h_i = h_j&lt;/math&gt; for all
+
Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i = h_j</math> for all
&lt;math&gt;i, j&lt;/math&gt;) and the shelves are all separated by a distance of greater than
+
<math>i, j</math>) and the shelves are all separated by a distance of greater than
&lt;math&gt;h&lt;/math&gt;, so any book fits on any shelf.
+
<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 &lt;math&gt;i&lt;/math&gt; such that &lt;math&gt;b_i&lt;/math&gt; does not fit, and then repeat
+
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.
  
&lt;br&gt;8-19.  
+
<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)]]  
  
&lt;br&gt;8-20.  
+
<br>8-20.  
We wish to compute the laziest way to dial given &lt;math&gt;n&lt;/math&gt;-digit number on a
+
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 &lt;math&gt;k&lt;/math&gt; is the number of distinct
+
smallest amount of total distance, where <math>k</math> is the number of distinct
keys on the keypad (&lt;math&gt;k=16&lt;/math&gt; for standard telephones).
+
keys on the keypad (<math>k=16</math> for standard telephones).
Try to use &lt;math&gt;O(n k^3)&lt;/math&gt; time.
+
Try to use <math>O(n k^3)</math> time.
  
&lt;br&gt;8-21.  
+
<br>8-21.  
Given an array of &lt;math&gt;n&lt;/math&gt; real numbers, consider the problem 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 &lt;math&gt;\{31,-41,59,26,-53,58,97,-93,-23,84\}&lt;/math&gt;
+
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 &lt;math&gt;59+26+(-53)+58+97 = 187&lt;/math&gt;.
+
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 &lt;math&gt;\Theta(n^2)&lt;/math&gt;-time algorithm to find the maximum contiguous subvector.
+
#Give a simple, clear, and correct <math>\Theta(n^2)</math>-time algorithm to find the maximum contiguous subvector.
#Now give a &lt;math&gt;\Theta(n)&lt;/math&gt;-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a ''correct'' &lt;math&gt;O(n \log n)&lt;/math&gt; divide-and-conquer algorithm.
+
#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)]]  
  
&lt;br&gt;8-22.  
+
<br>8-22.  
 
Consider the problem of examining a string
 
Consider the problem of examining a string
&lt;math&gt;x = x_1 x_2 \ldots x_n&lt;/math&gt; from an alphabet of &lt;math&gt;k&lt;/math&gt;
+
<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 &lt;math&gt;x&lt;/math&gt; in such a way that
+
whether or not it is possible to parenthesize <math>x</math> in such a way that
the value of the resulting expression is &lt;math&gt;a&lt;/math&gt;, where &lt;math&gt;a&lt;/math&gt; belongs to the
+
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}
&lt;center&gt;
+
<center>
&lt;math&gt;\begin{array}{c|ccc}
+
<math>\begin{array}{c|ccc}
&amp;       a      &amp;       b      &amp;       c      \\
+
&      a      &      b      &      c      \\
 
\hline
 
\hline
a      &amp;       a      &amp;       c      &amp;       c  \\
+
a      &      a      &      c      &      c  \\
b      &amp;       a      &amp;       a      &amp;       b  \\
+
b      &      a      &      a      &      b  \\
c      &amp;       c      &amp;       c      &amp;       c  \\
+
c      &      c      &      c      &      c  \\
\end{array}&lt;/math&gt;
+
\end{array}</math>
&lt;/center&gt;
+
</center>
 
For example, consider the above multiplication table and the string
 
For example, consider the above multiplication table and the string
&lt;math&gt;bbbba&lt;/math&gt;.
+
<math>bbbba</math>.
Parenthesizing it &lt;math&gt;(b(bb))(ba)&lt;/math&gt; gives &lt;math&gt;a&lt;/math&gt;, but &lt;math&gt;((((bb)b)b)a)&lt;/math&gt; gives &lt;math&gt;c&lt;/math&gt;.
+
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 &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;, to decide whether
+
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.
  
&lt;br&gt;8-23.  
+
<br>8-23.  
Let &lt;math&gt;\alpha&lt;/math&gt; and &lt;math&gt;\beta&lt;/math&gt; be constants. Assume that it costs
+
Let <math>\alpha</math> and <math>\beta</math> be constants. Assume that it costs
&lt;math&gt;\alpha&lt;/math&gt; to go left in a tree, and &lt;math&gt;\beta&lt;/math&gt; to go right.
+
<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 &lt;math&gt;k_1,\ldots,k_n&lt;/math&gt; and the probabilities that each will be
+
given keys <math>k_1,\ldots,k_n</math> and the probabilities that each will be
searched &lt;math&gt;p_1,\ldots,p_n&lt;/math&gt;.
+
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'''
  
&lt;br&gt;8-24.  
+
<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)]]  
  
&lt;br&gt;8-25.  
+
<br>8-25.  
You are given an array of &lt;math&gt;n&lt;/math&gt; numbers, each of which may be positive, negative,
+
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 &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt;
+
Give an efficient algorithm to identify the index positions <math>i</math> and <math>j</math>
to the maximum sum of the &lt;math&gt;i&lt;/math&gt;th through &lt;math&gt;j&lt;/math&gt;th numbers.
+
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)]]  
  
&lt;br&gt;8-26.  
+
<br>8-26.  
 
Observe that
 
Observe that
 
when you cut a character out of a
 
when you cut a character out of a

Latest revision as of 18:43, 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.

(Solution 8.1)


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.

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

  1. 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.
  2. Give a simpler $ \Theta(nm) $ algorithm that does not rely on dynamic programming.

(Solution 8.3)


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

  1. Give efficient algorithms to find the LCS and SCS of two given sequences.
  2. 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 $

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

(Solution 8.5)


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

  1. How many ways are there to make change of 20 units from $ \{1,6,10\} $?
  2. 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.)

(Solution 8.7)


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.

(Solution 8.9)


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.

(Solution 8.11)


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.

(Solution 8.13)


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.

  1. 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 $).
  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 $ 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.

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

(Solution 8.15)


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

(Solution 8.17)


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.

  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)


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.

  1. Give a simple, clear, and correct $ \Theta(n^2) $-time algorithm to find the maximum contiguous subvector.
  2. 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.

(Solution 8.21)


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

(Solution 8.23)

Interview Problems


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

(Solution 8.24)


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.

(Solution 8.25)


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.