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''
 
      ''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(<math>r</math>)
 
  
 +
===Edit Distance===
  
:[[2.5]]
+
:10.6
  
: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>
 
    end
 
#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.7]]
  
: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.8
  
===Big Oh===
 
  
 +
:[[10.9]]
  
:2.8. #Is <math>2^{n+1} = O (2^n)</math>?
 
#Is <math>2^{2n} = O(2^n)</math>?
 
  
 +
:10.10
  
:[[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.11|Solution]]
+
===Greedy Algorithms===
  
 +
:[[10.11]]
  
: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.
 
#<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>
 
  
 +
:10.12
  
:[[2.11]]
 
  
:2.12
+
:[[10.13]]
  
:2.13
 
  
:2.14
+
:10.14
  
:2.15
 
  
:2.16
+
===Number Problems===
  
:2.17
+
:[[10.15]]
  
:2.18
 
  
:2.19
+
:10.16
  
:2.20
 
  
:2.21
+
:[[10.17]]
  
:2.22
 
  
:2.23
+
:10.18
  
:2.24
 
  
:2.25
+
:[[10.19]]
  
:2.26
 
  
:2.27
+
:10.20
  
:2.28
 
  
:2.29
+
:[[10.21]]
  
:2.30
 
  
:2.31
+
:10.22
  
:2.32
 
  
:2.33
+
:[[10.23]]
  
:2.34
 
  
:2.35
+
:10.24
  
:2.36
 
  
===Summations===
+
:[[10.25]]
  
  
:2.37
+
:10.26
  
:2.38
 
  
:2.39
+
===Graphing Problem===
  
:2.40
+
:[[10.27]]
  
:2.41
 
  
:2.42
+
:10.28
  
:2.43
 
  
===Logartihms===
+
:[[10.29]]
  
  
:2.44
+
===Design Problems===
  
:2.45
+
:10.30
  
:2.46
 
  
:2.47
+
:[[10.31]]
 +
 
 +
 
 +
:10.32
 +
 
 +
 
 +
:[[10.33]]
 +
 
 +
 
 +
:10.34
 +
 
 +
 
 +
:[[10.35]]
 +
 
 +
 
 +
:10.36
 +
 
 +
 
 +
:[[10.37]]
  
===Interview Problems===
 
  
 +
:10.38
  
:2.48
 
  
:2.49
+
===Interview Problems===
  
:2.50
+
:[[10.39]]
  
: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 22:23, 11 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]?

Edit Distance

10.6


10.7


10.8


10.9


10.10


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