Difference between revisions of "Chapter 10"

From The Algorithm Design Manual Solution Wiki
Jump to navigation Jump to search
Line 136: Line 136:
 
===Graphing Problem===
 
===Graphing Problem===
  
:[[10.27]]
+
:[[10.27]]. Consider a city whose streets are defined by an <math>X * Y</math> 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 <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> 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 <math>O(XY)</math> algorithm to find a path across the grid that avoids bad neighborhoods.
 +
:(c) Give an <math>O(XY)</math> algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an <math>O(X^2Y^2)</math> algorithm.
 +
[[10.27|Solution]]
  
  
:10.28
+
:10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an <math>X * Y</math> 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 <math>X * Y</math> matrix bad, where <math>bad[i,j] =</math> “yes” iff the intersection between streets <math>i</math> and <math>j</math> is somewhere we want to avoid.
 +
:If there were no bad neighborhoods to contend with, the shortest path across the grid would have length <math>(X - 1) + (Y - 1)</math> blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves.
 +
:Give an algorithm that takes the array bad and returns the number of safe paths of length <math>X + Y - 2</math>. For full credit, your algorithm must run in <math>O(XY)</math>.
  
  
:[[10.29]]
+
:[[10.29]]. You seek to create a stack out of <math>n</math> boxes, where box <math>i</math> has width <math>w_i</math>, height <math>h_i</math>, and depth <math>d_i</math>. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack.
 
+
[[10.29|Solution]]
  
 
===Design Problems===
 
===Design Problems===

Revision as of 20:26, 13 September 2020

Dynamic Programming

Elementary Recurrences

10.1. Up to steps in a single bound! A child is running up a staircase with steps and can hop between 1 and steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of and . 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 homes. You know the loot at house is worth , for , 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 . For = 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 . For = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.


10.5. Given an 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 and ?
(b) Give a solution based on dynamic programming. What is its time complexity as a function of and ?

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: , , and , where , , and . is said to be a shuffle of and iff can be formed by interleaving the characters from and 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 is a shuffle of and . (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 and 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 and . Give a dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.
(b) Give a simpler algorithm that does not rely on dynamic programming.


10.9. The longest common subsequence (LCS) of two sequences and is the longest sequence such that is a subsequence of both and . The shortest common supersequence (SCS) of and is the smallest sequence such that both and are a subsequence of .
(a) Give efficient algorithms to find the LCS and SCS of two given sequences.
(b) Let be the minimum edit distance between and when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that where is the size of the shortest SCS (longest LCS) of and .

Solution


10.10. Suppose you are given 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 . Playing red, green, and then blue suffices to clear the stacks in three moves. Give an dynamic programming algorithm to find the best strategy for a given pair of chip piles.

Greedy Algorithms

10.11. Let be programs to be stored on a disk with capacity megabytes. Program requires megabytes of storage. We cannot store them all because
(a) Does a greedy algorithm that selects programs in order of non-decreasing maximize the number of programs held on the disk? Prove or give a counter-example.
(b) Does a greedy algorithm that selects programs in order of non-increasing use as much of the capacity of the disk as possible? Prove or give a counter-example.

Solution


10.12. 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 units. We seek an algorithm to make change of units using the minimum number of this country’s coins.
(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 units using denominations . Analyze its running time.


10.13. 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 units. We want to count how many distinct ways there are to make change of units. For example, in a country whose denominations are {1, 6, 10}, , to , , and .
(a) How many ways are there to make change of 20 units from {1, 6, 10}?
(b) Give an efficient algorithm to compute , and analyze its complexity. (Hint: think in terms of computing , the number of ways to make change of units with highest denomination . Be careful to avoid overcounting.)

Solution


10.14. In the single-processor scheduling problem, we are given a set of jobs . Each job has a processing time , and a deadline . 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

10.15. You are given a rod of length inches and a table of prices obtainable for rod-pieces of size or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if and the values of different pieces are:

\begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array}

then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6.

Solution


10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression:
there exist parenthesizations with values ranging from −32 to 2.


10.17. Given a positive integer , find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to . What is the running time of your algorithm?

Solution


10.18. Given an array of integers, find an efficient algorithm to compute the largest sum of a continuous run. For , the largest such sum is 10, from the second through fifth positions.


10.19. Two drivers have to divide up suitcases between them, where the weight of the suitcase is . Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible.

Solution


10.20. The knapsack problem is as follows: given a set of integers , and a given target number , find a subset of that adds up exactly to . For example, within there is a subset that adds up to but not .
Give a dynamic programming algorithm for knapsack that runs in time.


10.21. The integer partition takes a set of positive integers and seeks a subset such that
Let . Give an dynamic programming algorithm to solve the integer partition problem.

Solution


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


10.23. A certain string processing language allows the programmer to break a string into two pieces. It costs units of time to break a string of 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-to-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 units. If the breaks are made in right-to-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 units.
Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in time.

Solution


10.24. Consider the following data compression technique. We have a table of text strings, each at most in length. We want to encode a data string of length using as few text strings as possible. For example, if our table contains and the data string is , the best way to encode it is total of five code words. Give an 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.


10.25. 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 . The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities , , and of winning, drawing, and losing playing white, and has probabilities , , and of winning, drawing, and losing playing black.
(a) Write a recurrence for the probability that the champion retains the title. Assume that there are games left to play in the match and that the champion needs to get points (which may be a multiple of ).
(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title.
(c) Analyze its running time for an game match.

Solution


10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor in any sufficiently tall building such that an egg dropped from the th floor breaks, but one dropped from the st floor will not. If the egg always breaks, then . If the egg never breaks, then .
You seek to find the critical floor using an building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let be the minimum number of egg drops that will always suffice.
(a) Show that .
(b) Show that .
(c) Find a recurrence for . What is the running time of the dynamic program to find ?

Graphing Problem

10.27. Consider a city whose streets are defined by an 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 matrix bad, where “yes” iff the intersection between streets and 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 algorithm to find a path across the grid that avoids bad neighborhoods.
(c) Give an 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 algorithm.

Solution


10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an 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 matrix bad, where “yes” iff the intersection between streets and is somewhere we want to avoid.
If there were no bad neighborhoods to contend with, the shortest path across the grid would have length 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 . For full credit, your algorithm must run in .


10.29. You seek to create a stack out of boxes, where box has width , height , and depth . The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack.

Solution

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