Difference between pages "Chapter 2" and "Chapter 10"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
=Algorithm Analysis=
+
=Dynamic Programming=
  
===Program Analysis===
+
===Elementary Recurrences===
  
:[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation.
+
:[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm?
  mystery(''n'')
+
[[10.1|Solution]]
      r:=0
 
      ''for'' i:=1 ''to'' n-1 ''do''
 
          ''for'' j:=i+1 ''to'' n ''do''
 
              ''for'' k:=1 ''to'' j ''do''
 
                  r:=r+1
 
        ''return''(r)
 
  
[[2.1|Solution]]
 
  
 +
:10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.
  
:2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
 
    pesky(n)
 
        r:=0
 
        ''for'' i:=1 ''to'' n ''do''
 
            ''for'' j:=1 ''to'' i ''do''
 
                ''for'' k:=j ''to'' i+j ''do''
 
                    r:=r+1
 
        ''return''(r)
 
  
 +
:[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).
 +
[[10.3|Solution]]
  
:[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
 
    prestiferous(n)
 
        r:=0
 
        ''for'' i:=1 ''to'' n ''do''
 
            ''for'' j:=1 ''to'' i ''do''
 
                ''for'' k:=j ''to'' i+j ''do''
 
                    ''for'' l:=1 ''to'' i+j-k ''do''
 
                        r:=r+1
 
        ''return''(r)
 
  
[[2.3|Solution]]
+
:10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.
  
  
:2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
+
:[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
  conundrum(<math>n</math>)
+
::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>?
      <math>r:=0</math>
+
::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>?
      ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do''
+
[[10.5|Solution]]
      ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do''
 
      ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do''
 
      <math>r:=r+1</math>
 
      ''return''(r)
 
  
 +
===Edit Distance===
  
:[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation <math>x = 2x</math> doubles the value of the variable <math>x</math>).
+
:10.6. Typists often make transposition errors exchanging neighboring characters,
    ''for'' <math> k = 1</math> to <math>n</math>
+
such as typing “setve” for “steve.” This requires two substitutions to fix under
        <math>x = k</math>
+
the conventional definition of edit distance.
        ''while'' (<math>x < n</math>):
+
:Incorporate a swap operation into our edit distance function, so that such neigh-
          ''print'' '*'
+
boring transposition errors can be fixed at the cost of one operation.
          <math>x = 2x</math>
 
:Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>/Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>.
 
  
[[2.5|Solution]]
 
  
 +
:[[10.7]]. Suppose you are given three strings of characters: <math>X</math>, <math>Y</math>, and <math>Z</math>, where <math>|X| = n</math>, <math>|Y| = m</math>, and <math>|Z| = n + m</math>. <math>Z</math> is said to be a shuffle of <math>X</math> and <math>Y</math> iff <math>Z</math> can be formed by interleaving the characters from <math>X</math> and <math>Y</math> in a way that maintains the left-to-right ordering of the characters from each string.
 +
:(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
 +
:(b) 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.)
 +
[[10.7|Solution]]
  
:2.6. Suppose the following algorithm is used to evaluate the polynomial
 
::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math>
 
    <math>p:=a_0;</math>
 
    <math>xpower:=1;</math>
 
    for <math>i:=1</math> to <math>n</math> do
 
    <math>xpower:=x*xpower;</math>
 
    <math>p:=p+a_i * xpower</math>
 
#How many multiplications are done in the worst-case? How many additions?
 
#How many multiplications are done on the average?
 
#Can you improve this algorithm?
 
  
 +
:10.8. The longest common substring (not subsequence) of two strings <math>X</math> and <math>Y</math> is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph.
 +
:(a) 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.
 +
(b) Give a simpler <math>\Theta (nm)</math> algorithm that does not rely on dynamic programming.
  
:2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct.
 
  max(A)
 
      <math>m:=A[1]</math>
 
      ''for'' <math>i:=2</math> ''to'' n ''do''
 
            ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math>
 
      ''return'' (m)
 
  
[[2.7|Solution]]
+
:[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences <math>T</math> and <math>P</math> is the longest sequence <math>L</math> such that <math>L</math> is a subsequence of both <math>T</math> and <math>P</math>. The ''shortest common supersequence (SCS)'' of <math>T</math> and <math>P</math> is the smallest sequence <math>L</math> such that both <math>T</math> and <math>P</math> are a subsequence of <math>L</math>.
 +
:(a) Give efficient algorithms to find the LCS and SCS of two given sequences.
 +
:(b) 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)| (|LCS(T, P)|)</math> is the size of the shortest SCS (longest LCS) of <math>T</math> and <math>P</math>.
 +
[[10.9||Solution]]
  
===Big Oh===
 
  
 +
:10.10. Suppose you are given <math>n</math> poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone.
 +
:For example, consider the stacks <math>(RRGG, GBBB)</math>. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an <math>O(n^2)</math> dynamic programming algorithm to find the best strategy for a given pair of chip piles.
  
:2.8. True or False?
+
===Greedy Algorithms===
#Is <math>2^{n+1} = O (2^n)</math>?
 
#Is <math>2^{2n} = O(2^n)</math>?
 
  
 +
:[[10.11]]
  
:[[2.9]]. For each of the following pairs of functions, either <math>f(n )</math> is in <math>O(g(n))</math>, <math>f(n)</math> is in <math>\Omega(g(n))</math>, or <math>f(n)=\Theta(g(n))</math>. Determine which relationship is correct and briefly explain why.
 
#<math>f(n)=\log n^2</math>; <math>g(n)=\log n</math> + <math>5</math>
 
#<math>f(n)=\sqrt n</math>; <math>g(n)=\log n^2</math>
 
#<math>f(n)=\log^2 n</math>; <math>g(n)=\log n</math>
 
#<math>f(n)=n</math>; <math>g(n)=\log^2 n</math>
 
#<math>f(n)=n \log n + n</math>; <math>g(n)=\log n</math>
 
#<math>f(n)=10</math>; <math>g(n)=\log 10</math>
 
#<math>f(n)=2^n</math>; <math>g(n)=10 n^2</math>
 
#<math>f(n)=2^n</math>; <math>g(n)=3^n</math>
 
  
[[2.9|Solution]]
+
:10.12
  
  
:2.10. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, determine whether <math>f(n) = O(g(n))</math>, <math>g(n) = O(f(n))</math>, or both.
+
:[[10.13]]
#<math>f(n) = (n^2 - n)/2</math>,  <math>g(n) =6n</math>
 
#<math>f(n) = n +2 \sqrt n</math>, <math>g(n) = n^2</math>
 
#<math>f(n) = n \log n</math>, <math>g(n) = n \sqrt n /2</math>
 
#<math>f(n) = n + \log n</math>, <math>g(n) = \sqrt n</math>
 
#<math>f(n) = 2(\log n)^2</math>, <math>g(n) = \log n + 1</math>
 
#<math>f(n) = 4n\log n + n</math>, <math>g(n) = (n^2 - n)/2</math>
 
  
  
:[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>?
+
:10.14
  
[[2.11|Solution]]
 
  
 +
===Number Problems===
  
:2.12. Prove that <math>n^3 - 3n^2-n+1 = \Theta(n^3)</math>.
+
:[[10.15]]
  
  
:2.13. Prove that <math>n^2 = O(2^n)</math>.
+
:10.16
  
[[2.13|Solution]]
 
  
 +
:[[10.17]]
  
:2.14. Prove or disprove: <math>\Theta(n^2) = \Theta(n^2+1)</math>.
 
  
 +
:10.18
  
:[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one?
 
::(a) <math>n^2</math>  (b) <math>n^3</math>  (c) <math>100n^2</math>  (d) <math>nlogn</math>  (e) <math>2^n</math>
 
  
[[2.15|Solution]]
+
:[[10.19]]
  
  
:2.16.  Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size <math>n</math>.)Suppose you have a computer that can perform <math>10^10</math> operations per second. For each algorithm, what is the largest input size n that you can complete within an hour?
+
:10.20
::(a) <math>n^2</math>  (b) <math>n^3</math>  (c) <math>100n^2</math>  (d) <math>nlogn</math>  (e) <math>2^n</math>  (f) <math>2^{2^n}</math>
 
  
  
:[[2.17]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, give an appropriate positive constant <math>c</math> such that <math>f(n) \leq c \cdot g(n)</math> for all <math>n > 1</math>.
+
:[[10.21]]
#<math>f(n)=n^2+n+1</math>, <math>g(n)=2n^3</math>
 
#<math>f(n)=n \sqrt n + n^2</math>, <math>g(n)=n^2</math>
 
#<math>f(n)=n^2-n+1</math>, <math>g(n)=n^2/2</math>
 
  
[[2.17|Solution]]
 
  
 +
:10.22
  
:2.18. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n)+f_2(n) = O(g_1(n)+g_2(n))</math>.
 
  
 +
:[[10.23]]
  
:[[2.19]]. Prove that if <math>f_1(N)=\Omega(g_1(n))</math> and <math>f_2(n)=\Omega(g_2(n) </math>, then <math>f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))</math>.
 
  
[[2.19|Solution]]
+
:10.24
  
  
:2.20. Prove that if <math>f_1(n)=O(g_1(n))</math> and <math>f_2(n)=O(g_2(n))</math>, then <math>f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))</math>
+
:[[10.25]]
  
  
:[[2.21]]. Prove for all <math>k \geq 1</math> and all sets of constants <math>\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R</math>, <math> a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)</math>
+
:10.26
  
[[2.21|Solution]]
 
  
 +
===Graphing Problem===
  
:2.22. Show that for any real constants <math>a</math> and <math>b</math>, <math>b > 0</math>
+
:[[10.27]]
<center><math>(n + a)^b = \Omega (n^b)</math></center>
 
  
  
:[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.
+
:10.28
<center>
 
<math>\begin{array}{llll}
 
n & 2^n & n \lg n & \ln n \\
 
n-n^3+7n^5 & \lg n & \sqrt n & e^n \\
 
n^2+\lg n & n^2 & 2^{n-1} &  \lg \lg n \\
 
n^3 & (\lg n)^2 & n! & n^{1+\varepsilon} where 0< \varepsilon <1
 
\\
 
\end{array}</math>
 
</center>
 
  
[[2.23|Solution]]
 
  
 +
:[[10.29]]
  
:2.24
 
  
 +
===Design Problems===
  
:[[2.25]]
+
:10.30
  
[[2.25|Solution]]
 
  
 +
:[[10.31]]
  
:2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.
 
<center>
 
<math>\begin{array}{lll}
 
\sqrt{n} & n & 2^n \\
 
n \log n &  n - n^3 + 7n^5 &  n^2 + \log n \\
 
n^2 &  n^3 &  \log n \\
 
n^{\frac{1}{3}} + \log n & (\log n)^2 &  n! \\
 
\ln n & \frac{n}{\log n} &  \log \log n \\
 
({1}/{3})^n &  ({3}/{2})^n &  6 \\
 
\end{array}</math>
 
</center>
 
  
 +
:10.32
  
:[[2.27]]. Find two functions <math>f(n)</math> and <math>g(n)</math> that satisfy the following relationship. If no such <math>f</math> and <math>g</math> exist, write ''None.''
 
#<math>f(n)=o(g(n))</math> and <math>f(n) \neq \Theta(g(n))</math>
 
#<math>f(n)=\Theta(g(n))</math> and <math>f(n)=o(g(n))</math>
 
#<math>f(n)=\Theta(g(n))</math> and <math>f(n) \neq O(g(n))</math>
 
#<math>f(n)=\Omega(g(n))</math> and <math>f(n) \neq O(g(n))</math>
 
  
[[2.27|Solution]]
+
:[[10.33]]
  
  
:2.28. True or False?
+
:10.34
#<math>2n^2+1=O(n^2)</math>
 
#<math>\sqrt n= O(\log n)</math>
 
#<math>\log n = O(\sqrt n)</math>
 
#<math>n^2(1 + \sqrt n) = O(n^2 \log n)</math>
 
#<math>3n^2 + \sqrt n = O(n^2)</math>
 
#<math>\sqrt n \log n= O(n) </math>
 
#<math>\log n=O(n^{-1/2})</math>
 
  
  
:[[2.29]]. For each of the following pairs of functions <math>f(n)</math> and <math>g(n)</math>, state whether <math>f(n)=O(g(n))</math>, <math>f(n)=\Omega(g(n))</math>, <math>f(n)=\Theta(g(n))</math>, or none of the above.
+
:[[10.35]]
#<math>f(n)=n^2+3n+4</math>, <math>g(n)=6n+7</math>
 
#<math>f(n)=n \sqrt n</math>, <math>g(n)=n^2-n</math>
 
#<math>f(n)=2^n - n^2</math>, <math>g(n)=n^4+n^2</math>
 
  
[[2.29|Solution]].
 
  
 +
:10.36
  
:2.30. For each of these questions, briefly explain your answer.
 
<br>
 
::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs?
 
<br>
 
::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs?
 
<br>
 
::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs?
 
<br>
 
::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs?
 
<br>
 
::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>?
 
  
 +
:[[10.37]]
  
:[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning.
 
<br>
 
::(a) Is <math>3^n = O(2^n)</math>?
 
<br>
 
::(b) Is <math>\log 3^n = O( \log 2^n )</math>?
 
<br>
 
::(c) Is <math>3^n = \Omega(2^n)</math>?
 
<br>
 
::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>?
 
  
[[2.31|Solution]]
+
:10.38
  
 
:2.32. For each of the following expressions <math>f(n)</math> find a simple <math>g(n)</math> such that <math>f(n)=\Theta(g(n))</math>.
 
#<math>f(n)=\sum_{i=1}^n {1\over i}</math>.
 
#<math>f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil</math>.
 
#<math>f(n)=\sum_{i=1}^n \log i</math>.
 
#<math>f(n)=\log (n!)</math>.
 
 
 
:[[2.33]]. Place the following functions into increasing asymptotic order.
 
::<math>f_1(n) = n^2\log_2n</math>,
 
<math>f_2(n) = n(\log_2n)^2</math>,
 
<math>f_3(n) = \sum_{i=0}^n 2^i</math>,
 
<math>f_4(n) = \log_2(\sum_{i=0}^n 2^i)</math>.
 
 
[[2.33|Solution]]
 
 
 
:2.34. Which of the following are true?
 
#<math>\sum_{i=1}^n 3^i = \Theta(3^{n-1})</math>.
 
#<math>\sum_{i=1}^n 3^i = \Theta(3^n)</math>.
 
#<math>\sum_{i=1}^n 3^i = \Theta(3^{n+1})</math>.
 
 
 
:[[2.35]]. For each of the following functions <math>f</math> find a simple function <math>g</math> such that <math>f(n)=\Theta(g(n))</math>.
 
#<math>f_1(n)= (1000)2^n + 4^n</math>.
 
#<math>f_2(n)= n + n\log n + \sqrt n</math>.
 
#<math>f_3(n)= \log (n^{20}) + (\log n)^{10}</math>.
 
#<math>f_4(n)= (0.99)^n + n^{100}.</math>
 
 
[[2.35|Solution]]
 
 
 
:2.36. For each pair of expressions <math>(A,B)</math> below, indicate whether <math>A</math> is <math>O</math>, <math>o</math>, <math>\Omega</math>, <math>\omega</math>, or <math>\Theta</math> of
 
<math>B</math>.  Note that zero, one or more of these relations may hold for a
 
given pair; list all correct ones.
 
<br><math>
 
\begin{array}{lcc}
 
::        & A                    & B \\
 
::(a)    & n^{100}              & 2^n \\
 
::(b)    & (\lg n)^{12}        & \sqrt{n} \\
 
::(c)    & \sqrt{n}              & n^{\cos (\pi n/8)} \\
 
::(d)    & 10^n                  & 100^n \\
 
::(e)    & n^{\lg n}            & (\lg n)^n \\
 
::(f)    & \lg{(n!)}            & n \lg n
 
\end{array}
 
</math>
 
 
===Summations===
 
 
 
:[[2.37]]. Find an expression for the sum of the <math>i</math>th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0.
 
<center>
 
<math>\begin{array}{ccccccccc}
 
&&&&1&&&& \\
 
&&&1&1&1&&&\\
 
&&1&2&3&2&1&&\\
 
&1&3&6&7&6&3&1&\\
 
1&4&10&16&19&16&10&4&1\\
 
\end{array}</math>
 
</center>
 
 
[[2.37|Solution]]
 
 
 
:2.38. Assume that Christmas has <math>n</math> days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.)
 
 
 
:[[2.39]]
 
 
[[2.39|Solution]]
 
 
 
:2.40. Consider the following code fragment.
 
<tt>
 
  for i=1 to n do
 
      for j=i to 2*i do
 
        output ''foobar''
 
</tt>
 
:Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>.
 
#Express <math>T(n)</math> as a summation (actually two nested summations).
 
#Simplify the summation.  Show your work.
 
 
 
:[[2.41]].Consider the following code fragment.
 
<tt>
 
  for i=1 to n/2 do
 
      for j=i to n-i do
 
        for k=1 to j do
 
            output ''foobar''
 
</tt>
 
:Assume <math>n</math> is even. Let <math>T(n)</math> denote the number of times `foobar' is printed as a function of <math>n</math>.
 
#Express <math>T(n)</math> as three nested summations.
 
#Simplify the summation.  Show your work.
 
 
[[2.41|Solution]]
 
 
 
:2.42. When you first learned to multiply numbers, you were told that <math>x \times y</math> means adding <math>x</math> a total of <math>y</math> times, so <math>5 \times 4 = 5+5+5+5 = 20</math>. What is the time complexity of multiplying two <math>n</math>-digit numbers in base <math>b</math> (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of <math>n</math> and <math>b</math>. Assume that single-digit by single-digit addition or multiplication takes
 
<math>O(1)</math> time. (Hint: how big can <math>y</math> be as a function of <math>n</math> and <math>b</math>?)
 
 
 
:[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that <math>127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397</math>. Analyze the time complexity of multiplying two <math>n</math>-digit numbers with this method as a function of <math>n</math> (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes <math>O(1)</math> time.
 
 
[2.43|Solution]]
 
 
===Logartihms===
 
 
 
:2.44
 
 
:2.45
 
 
:2.46
 
 
:2.47
 
  
 
===Interview Problems===
 
===Interview Problems===
  
 +
:[[10.39]]
  
:2.48
 
 
:2.49
 
 
:2.50
 
 
:2.51
 
  
:2.52
+
:10.40
  
:2.53
 
  
:2.54
+
:[[10.41]]
  
:2.55
 
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 19:25, 13 September 2020

Dynamic Programming

Elementary Recurrences

10.1. Up to [math]\displaystyle{ k }[/math] steps in a single bound! A child is running up a staircase with [math]\displaystyle{ n }[/math] steps and can hop between 1 and [math]\displaystyle{ k }[/math] steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of [math]\displaystyle{ n }[/math] and [math]\displaystyle{ k }[/math]. What is the running time of your algorithm?

Solution


10.2. Imagine you are a professional thief who plans to rob houses along a street of [math]\displaystyle{ n }[/math] homes. You know the loot at house [math]\displaystyle{ i }[/math] is worth [math]\displaystyle{ m_i }[/math], for [math]\displaystyle{ 1 \le i \le n }[/math], but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.


10.3. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given [math]\displaystyle{ n }[/math]. For [math]\displaystyle{ n }[/math] = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).

Solution


10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given [math]\displaystyle{ n }[/math]. For [math]\displaystyle{ n }[/math] = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.


10.5. Given an [math]\displaystyle{ s * t }[/math] grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math]?
(b) Give a solution based on dynamic programming. What is its time complexity as a function of [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math]?

Solution

Edit Distance

10.6. Typists often make transposition errors exchanging neighboring characters,

such as typing “setve” for “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 neigh-

boring transposition errors can be fixed at the cost of one operation.


10.7. Suppose you are given three strings of characters: [math]\displaystyle{ X }[/math], [math]\displaystyle{ Y }[/math], and [math]\displaystyle{ Z }[/math], where [math]\displaystyle{ |X| = n }[/math], [math]\displaystyle{ |Y| = m }[/math], and [math]\displaystyle{ |Z| = n + m }[/math]. [math]\displaystyle{ Z }[/math] is said to be a shuffle of [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] iff [math]\displaystyle{ Z }[/math] can be formed by interleaving the characters from [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] in a way that maintains the left-to-right ordering of the characters from each string.
(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
(b) Give an efficient dynamic programming algorithm that determines whether [math]\displaystyle{ Z }[/math] is a shuffle of [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math]. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.)

Solution


10.8. The longest common substring (not subsequence) of two strings [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph.
(a) Let [math]\displaystyle{ n = |X| }[/math] and [math]\displaystyle{ m = |Y| }[/math]. Give a [math]\displaystyle{ \Theta (nm) }[/math] dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.

(b) Give a simpler [math]\displaystyle{ \Theta (nm) }[/math] algorithm that does not rely on dynamic programming.


10.9. The longest common subsequence (LCS) of two sequences [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] is the longest sequence [math]\displaystyle{ L }[/math] such that [math]\displaystyle{ L }[/math] is a subsequence of both [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math]. The shortest common supersequence (SCS) of [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] is the smallest sequence [math]\displaystyle{ L }[/math] such that both [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] are a subsequence of [math]\displaystyle{ L }[/math].
(a) Give efficient algorithms to find the LCS and SCS of two given sequences.
(b) Let [math]\displaystyle{ d(T, P) }[/math] be the minimum edit distance between [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that [math]\displaystyle{ d(T, P) = |SCS(T, P)| − |LCS(T, P)| }[/math] where [math]\displaystyle{ |SCS(T, P)| (|LCS(T, P)|) }[/math] is the size of the shortest SCS (longest LCS) of [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math].

|Solution


10.10. Suppose you are given [math]\displaystyle{ n }[/math] poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone.
For example, consider the stacks [math]\displaystyle{ (RRGG, GBBB) }[/math]. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an [math]\displaystyle{ O(n^2) }[/math] dynamic programming algorithm to find the best strategy for a given pair of chip piles.

Greedy Algorithms

10.11


10.12


10.13


10.14


Number Problems

10.15


10.16


10.17


10.18


10.19


10.20


10.21


10.22


10.23


10.24


10.25


10.26


Graphing Problem

10.27


10.28


10.29


Design Problems

10.30


10.31


10.32


10.33


10.34


10.35


10.36


10.37


10.38


Interview Problems

10.39


10.40


10.41


Back to Chapter List