Difference between pages "Chapter 10" and "Chapter 9"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
=Dynamic Programming=
+
=Combinatorial Search=
  
===Elementary Recurrences===
+
===Permutations===
  
:[[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?
+
:[[9.1]]. A ''derangement'' is a permutation <math>p</math> of <math>{1, . . . , n}</math> such that no item is in its proper position, that is, <math>p_i 6= i</math> for all <math>1 \leq i \leq n</math>. Write an efficient backtracking program with pruning that constructs all the derangements of <math>n</math> items.
[[10.1|Solution]]
+
[[9.1|Soluiton]]
  
  
: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.
+
:9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than <math>n!</math> distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset.
  
  
:[[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).
+
:[[9.3]]. For a given a positive integer <math>n</math>, find all permutations of the <math>2n</math> elements of the multiset <math>S = {1, 1, 2, 2, 3, 3, . . . , n, n}</math> such that for each integer from 1 to <math>n</math> the number of intervening elements between its two appearances is equal to value of the element. For example, when <math>n = 3</math> the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3].
[[10.3|Solution]]
+
[[9.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.
+
:9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time.
  
  
:[[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.
+
:[[9.5]]. The set <math>{1, 2, 3, ..., n}</math> contains a total of <math>n!</math> distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for <math>n = 3</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>?
+
<center><math>[123, 132, 213, 231, 312, 321]</math></center>
::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>?
+
:Give an efficient algorithm that returns the <math>k</math>th of <math>n!</math> permutations in this see quence, for inputs <math>n</math> and <math>k</math>. For efficiency it should not construct the first <math>k - 1</math> permutations in the process.
[[10.5|Solution]]
+
[[9.5|Solution]]
  
===Edit Distance===
+
===Backtracking===
  
: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.
+
:9.6. Generate all structurally distinct binary search trees that store values <math>1 . . . n</math>, for a given value of <math>n</math>.
:Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation.
 
  
  
:[[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.
+
:[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses.
:(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
+
[[9.7|Solution]]
:(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]]
 
  
  
: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.
+
:9.8. Generate all possible topological orderings of a given DAG.
:(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.
 
  
  
:[[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>.
+
:[[9.9]]. Given a specified total t and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum.
:(a) Give efficient algorithms to find the LCS and SCS of two given sequences.
+
[[9.9|Solution]]
:(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]]
 
  
  
: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.  
+
:9.10. Given a specified total <math>t</math> and a multiset <math>S</math> of <math>n</math> integers, find all distinct subsets from <math>S</math> whose elements add up to <math>t</math>. For example, if <math>t = 4</math> and <math>S = {4, 3, 2, 2, 1, 1}</math>, then there are four different sums that equal <math>t: 4, 3 + 1, 2 + 2</math>, and <math>2 + 1 + 1</math>. A number can be used within a sum up to the number of times it appears in <math>S</math>, and a single number counts as a sum.
: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.
 
  
===Greedy Algorithms===
 
  
:[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math>
+
:[[9.11]]. A team assignment of <math>n = 2k</math> players is a partitioning of them into two teams with exactly <math>k</math> people per team. For example, if the players are named <math>{A, B, C, D}</math>, there are three distinct ways to partition them into two equal teams: <math>{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}</math>.
:(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example.
+
:(a) List the 10 possible team assignments for <math>n = 6</math> players.
:(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example.
+
:(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution.
[[10.11|Solution]]
+
[[9.11|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 <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins.
+
:9.12. Given an alphabet <math>\Sigma</math>, a set of forbidden strings <math>S</math>, and a target length <math>n</math>, give an algorithm to construct a string of length <math>n</math> on <math>\Sigma</math> without any element of <math>S</math> as a substring. For <math>\Sigma = {0, 1}, S = {01, 10}<math>, and <math>n = 4</math>, the two possible solutions are 0000 and 1111. For <math>S = {0, 11}</math> and <math>n = 4</math>, no such string exists.
:(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 <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. 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 <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <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>.
+
:[[9.13]]. In the <math>k</math>-partition problem, we need to partition a multiset of positive integers into <math>k</math> disjoint subsets that have equal sum. Design and implement an algorithm for solving the <math>k</math>-partition problem.
:(a) How many ways are there to make change of 20 units from {1, 6, 10}?
+
[[9.13|Solution]]
:(b) 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.)
 
[[10.13|Solution]]
 
  
  
:10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. 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 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.
+
:9.14. You are given a weighted directed graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in <math>G</math> of minimum mean weight.
: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 <math>n</math> inches and a table of prices obtainable for rod-pieces of size <math>n</math> or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if <math>n=8</math> and the values of different pieces are:
+
:[[9.15]]. In the turnpike reconstruction problem, you are given a multiset <math>D</math> of <math>n(n - 1)/2</math> distances. The problem is to place <math>n</math> points on the line such that their pairwise distances are <math>D</math>. For example, the distances <math>D = {1, 2, 3, 4, 5, 6}</math> can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time.
<center><math>
+
[[9.15|Solution]]
\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}</math>
 
</center>
 
:then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6.
 
[[10.15|Solution]]
 
  
 +
===Games and Puzzles===
  
: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:
+
:9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary.
<center><math>6 + 2 * 0 - 4</math></center>
 
:there exist parenthesizations with values ranging from −32 to 2.
 
  
  
:[[10.17]]. Given a positive integer <math>n</math>, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to <math>n</math>. What is the running time of your algorithm?
+
:[[9.17]]. Construct all sequences of moves that a knight on an <math>n * n</math> chessboard can make where the knight visits every square only once.
[[10.17|Solution]]
+
[[9.17|Solution]]
  
  
:10.18. Given an array <math>A</math> of <math>n</math> integers, find an efficient algorithm to compute the largest sum of a continuous run. For <math>A = [-3, 2, 7, -3, 4, -2, 0, 1]</math>, the largest such sum is 10, from the second through fifth positions.
+
:9.18. A Boggle board is an <math>n * m</math> grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board:
 +
<center><math>\begin{matrix}
 +
e & t & h & t\\
 +
n & d & t & i\\
 +
a & i & h & n\\
 +
r & h & u & b
 +
\end{matrix}</math></center>
 +
:contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board <math>B</math> consistent with a dictionary <math>D</math>.
  
  
:[[10.19]]. Two drivers have to divide up <math>m</math> suitcases between them, where the weight of the <math>ith</math> suitcase is <math>w_i</math>. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible.
+
:[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a <math>k</math>-letter word <math>w</math> and a dictionary of <math>n</math> words, find all Babbage squares starting with that word. For example, two squares for the word hair are:
[[10.19|Solution]]
+
<center><math>\begin{vmatrix}
 +
h & a & i & r\\
 +
a & i & d & e\\
 +
i & d & l & e\\
 +
r & e & e & f
 +
\end{vmatrix}\begin{vmatrix}
 +
h & a & i & r\\
 +
a & l & t & o\\
 +
i & t & e & m\\
 +
r & o & m & b
 +
\end{vmatrix}
 +
</math></center>
 +
[[9.19|Solution]]
  
  
:10.20. The ''knapsack problem'' is as follows: given a set of integers <math>S = {s_1, s_2, . . . , s_n}</math>, and a given target number <math>T</math>, find a subset of <math>S</math> that adds up exactly to <math>T</math>. For example, within <math>S = {1, 2, 5, 9, 10}</math> there is a subset that adds up to <math>T = 22</math> but not <math>T = 23</math>.
+
:9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph.
:Give a dynamic programming algorithm for knapsack that runs in <math>O(nT)</math> time.
 
  
 +
===Combinatorial Optimization===
 +
For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice?
  
:[[10.21]]. The integer partition takes a set of positive integers <math>S = {s_1, . . . , s_n}</math> and seeks a subset <math>I \subset S</math> such that
+
:[[9.21]]. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470).
<center><math> \sum_{i \in I} s_i = \sum_{i \notin I} s_i</math></center>
+
[[9.21|Solution]]
:Let <math> \sum_{i \in S} s_i = M </math>. Give an <math>O(nM)</math> dynamic programming algorithm to solve the integer partition problem.
 
[[10.21|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.
+
:9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537).
  
  
:[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It 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. 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.
+
:[[9.23]]. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586).
:Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in <math>O(n^3)</math> time.
+
[[9.23|Solution]]
[[10.23|Solution]]
 
  
  
:10.24. Consider the following data compression technique. 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 <math>D</math> of length <math>n</math> using as few text strings as possible. For example, if our table contains <math>(a,ba,abab,b)</math> and the data string is <math>bababbaababa</math>, the best way to encode it is <math>(b,abab,ba,abab,a)-a</math> total of five code words. 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.
+
:9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604).
  
  
:[[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 <math>1/2</math>. 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 <math>w_w</math>, <math>w_d</math>, and <math>w_l</math> of winning, drawing, and losing playing white, and has probabilities <math>b_w</math>, <math>b_d</math>, and <math>b_l</math> of winning, drawing, and losing playing black.
+
:[[9.25]]. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608).
:(a) 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 get <math>i</math> points (which may be a multiple of <math>1/2</math>).
+
[[9.25|Solution]]
:(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 <math>n</math> game match.
 
[[10.125|Solution]]
 
  
  
:10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor <math>f</math> in any sufficiently tall building such that an egg dropped from the <math>f</math>th floor breaks, but one dropped from the <math>(f - 1)</math>st floor will not. If the egg always breaks, then <math>f = 1</math>. If the egg never breaks, then <math>f = n + 1</math>.
+
:9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618).
:You seek to find the critical floor <math>f</math> using an <math>n-floor</math> building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with <math>k</math> eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let <math>E(k, n)</math> be the minimum number of egg drops that will always suffice.
 
:(a) Show that <math>E(1, n) = n</math>.
 
:(b) Show that <math>E(k, n) = \Theta (n^{1/k})</math>.
 
:(c) 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>?
 
  
===Graphing Problem===
 
  
:[[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.
+
:[[9.27]]. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678).
: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.
+
[[9.27|Solution]]
:(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]]
 
  
 +
===Interview Problems===
  
: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.
+
:9.28
: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]]. 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===
 
 
 
:10.30. 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 be rearranged. Therefore, we can speak of a book <math>b_i</math>, where <math>1 \leq i \leq n</math>, that has a thickness <math>t_i</math> and height <math>h_i</math>. The length of each bookshelf at this library is <math>L</math>.
 
:Suppose all the books have the same height <math>h</math> (i.e., <math>h = h_i</math> for all <math>i</math>) and the shelves are all separated by a distance greater than <math>h</math>, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest <math>i</math> such that <math>b_i</math> does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity.
 
 
 
 
 
:[[10.31]]. 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. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf.
 
:#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height.
 
:#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.)
 
[[10.31|Solution]]
 
 
 
 
 
:10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank.
 
:Give a dynamic programming algorithm that finds the most efficient way to type a given text of length <math>n</math>, in terms of minimizing total movement of the fingers involved. For the text <math>ABABABAB . . . ABAB</math>, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of <math>n</math> and <math>k</math>, the number of keys on the keyboard.
 
 
 
  
:[[10.33]]. You have come back from the future with an array <math>G</math>, where <math>G[i]</math> tells you the price of Google stock <math>i</math> days from now, for <math>1 \leq i \leq n</math>. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one.
 
[[10.33|Solution]]
 
  
 +
:[[9.27]]
  
:10.34. You are given a string of <math>n</math> characters <math>S = s_1 . . . s_n</math>, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''.
 
:(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function <math>dict(w)</math>, where <math>dict(w)</math> is true iff string <math>w</math> is a valid word in the language. Give an <math>O(n^2)</math> algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, assuming calls to <math>dict(w </math> take unit time.
 
:(b) Now assume you are given the dictionary as a set of <math>m</math> words each of length at most l. Give an efficient algorithm to determine whether string <math>S</math> can be reconstituted as a sequence of valid words, and its running time.
 
  
 +
:9.28
  
:[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer <math>N</math>. With each move, you get to take either the first digit or the last digit from what is left of <math>N</math>, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string <math>N</math>, assuming the second player is as smart as can be.
 
[[10.35|Solution]]
 
  
 +
:[[9.29]]
  
:10.36. Given an array of <math>n</math> real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array
 
<center><math>[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]</math></center>
 
:the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0.
 
#Give a simple and clear <math> \Theta (n^2)</math>-time algorithm to find the maximum contiguous subarray.
 
#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.
 
  
 +
:9.30
  
:[[10.37]]
 
 
 
:10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys <math>k_1, . . . , k_n</math> and the probabilities that each will be searched <math>p_1, . . . , p_n</math>.
 
 
===Interview Problems===
 
  
:[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change.
+
:[[9.31]]
[[10.39|Solution]]
 
  
  
:10.40. 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 <math>i</math> and <math>j</math> to obtain the maximum sum of the <math>i</math>th through <math>j</math>th numbers.
+
:9.32
  
  
:[[10.41]]. 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.
+
:[[9.33]]
[[10.41|Solution]]
 
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 21:40, 13 September 2020

Combinatorial Search

Permutations

9.1. A derangement is a permutation [math]\displaystyle{ p }[/math] of [math]\displaystyle{ {1, . . . , n} }[/math] such that no item is in its proper position, that is, [math]\displaystyle{ p_i 6= i }[/math] for all [math]\displaystyle{ 1 \leq i \leq n }[/math]. Write an efficient backtracking program with pruning that constructs all the derangements of [math]\displaystyle{ n }[/math] items.

Soluiton


9.2. Multisets are allowed to have repeated elements. A multiset of n items may thus have fewer than [math]\displaystyle{ n! }[/math] distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset.


9.3. For a given a positive integer [math]\displaystyle{ n }[/math], find all permutations of the [math]\displaystyle{ 2n }[/math] elements of the multiset [math]\displaystyle{ S = {1, 1, 2, 2, 3, 3, . . . , n, n} }[/math] such that for each integer from 1 to [math]\displaystyle{ n }[/math] the number of intervening elements between its two appearances is equal to value of the element. For example, when [math]\displaystyle{ n = 3 }[/math] the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3].

Solution


9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time.


9.5. The set [math]\displaystyle{ {1, 2, 3, ..., n} }[/math] contains a total of [math]\displaystyle{ n! }[/math] distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for [math]\displaystyle{ n = 3 }[/math]:
[math]\displaystyle{ [123, 132, 213, 231, 312, 321] }[/math]
Give an efficient algorithm that returns the [math]\displaystyle{ k }[/math]th of [math]\displaystyle{ n! }[/math] permutations in this see quence, for inputs [math]\displaystyle{ n }[/math] and [math]\displaystyle{ k }[/math]. For efficiency it should not construct the first [math]\displaystyle{ k - 1 }[/math] permutations in the process.

Solution

Backtracking

9.6. Generate all structurally distinct binary search trees that store values [math]\displaystyle{ 1 . . . n }[/math], for a given value of [math]\displaystyle{ n }[/math].


9.7. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses.

Solution


9.8. Generate all possible topological orderings of a given DAG.


9.9. Given a specified total t and a multiset [math]\displaystyle{ S }[/math] of [math]\displaystyle{ n }[/math] integers, find all distinct subsets from [math]\displaystyle{ S }[/math] whose elements add up to [math]\displaystyle{ t }[/math]. For example, if [math]\displaystyle{ t = 4 }[/math] and [math]\displaystyle{ S = {4, 3, 2, 2, 1, 1} }[/math], then there are four different sums that equal [math]\displaystyle{ t: 4, 3 + 1, 2 + 2 }[/math], and [math]\displaystyle{ 2 + 1 + 1 }[/math]. A number can be used within a sum up to the number of times it appears in [math]\displaystyle{ S }[/math], and a single number counts as a sum.

Solution


9.10. Given a specified total [math]\displaystyle{ t }[/math] and a multiset [math]\displaystyle{ S }[/math] of [math]\displaystyle{ n }[/math] integers, find all distinct subsets from [math]\displaystyle{ S }[/math] whose elements add up to [math]\displaystyle{ t }[/math]. For example, if [math]\displaystyle{ t = 4 }[/math] and [math]\displaystyle{ S = {4, 3, 2, 2, 1, 1} }[/math], then there are four different sums that equal [math]\displaystyle{ t: 4, 3 + 1, 2 + 2 }[/math], and [math]\displaystyle{ 2 + 1 + 1 }[/math]. A number can be used within a sum up to the number of times it appears in [math]\displaystyle{ S }[/math], and a single number counts as a sum.


9.11. A team assignment of [math]\displaystyle{ n = 2k }[/math] players is a partitioning of them into two teams with exactly [math]\displaystyle{ k }[/math] people per team. For example, if the players are named [math]\displaystyle{ {A, B, C, D} }[/math], there are three distinct ways to partition them into two equal teams: [math]\displaystyle{ {{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}} }[/math].
(a) List the 10 possible team assignments for [math]\displaystyle{ n = 6 }[/math] players.
(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution.

Solution


9.12. Given an alphabet [math]\displaystyle{ \Sigma }[/math], a set of forbidden strings [math]\displaystyle{ S }[/math], and a target length [math]\displaystyle{ n }[/math], give an algorithm to construct a string of length [math]\displaystyle{ n }[/math] on [math]\displaystyle{ \Sigma }[/math] without any element of [math]\displaystyle{ S }[/math] as a substring. For [math]\displaystyle{ \Sigma = {0, 1}, S = {01, 10}\lt math\gt , and \lt math\gt n = 4 }[/math], the two possible solutions are 0000 and 1111. For [math]\displaystyle{ S = {0, 11} }[/math] and [math]\displaystyle{ n = 4 }[/math], no such string exists.


9.13. In the [math]\displaystyle{ k }[/math]-partition problem, we need to partition a multiset of positive integers into [math]\displaystyle{ k }[/math] disjoint subsets that have equal sum. Design and implement an algorithm for solving the [math]\displaystyle{ k }[/math]-partition problem.

Solution


9.14. You are given a weighted directed graph [math]\displaystyle{ G }[/math] with [math]\displaystyle{ n }[/math] vertices and [math]\displaystyle{ m }[/math] edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in [math]\displaystyle{ G }[/math] of minimum mean weight.


9.15. In the turnpike reconstruction problem, you are given a multiset [math]\displaystyle{ D }[/math] of [math]\displaystyle{ n(n - 1)/2 }[/math] distances. The problem is to place [math]\displaystyle{ n }[/math] points on the line such that their pairwise distances are [math]\displaystyle{ D }[/math]. For example, the distances [math]\displaystyle{ D = {1, 2, 3, 4, 5, 6} }[/math] can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time.

Solution

Games and Puzzles

9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary.


9.17. Construct all sequences of moves that a knight on an [math]\displaystyle{ n * n }[/math] chessboard can make where the knight visits every square only once.

Solution


9.18. A Boggle board is an [math]\displaystyle{ n * m }[/math] grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board:
[math]\displaystyle{ \begin{matrix} e & t & h & t\\ n & d & t & i\\ a & i & h & n\\ r & h & u & b \end{matrix} }[/math]
contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board [math]\displaystyle{ B }[/math] consistent with a dictionary [math]\displaystyle{ D }[/math].


9.19. A Babbage square is a grid of words that reads the same across as it does down. Given a [math]\displaystyle{ k }[/math]-letter word [math]\displaystyle{ w }[/math] and a dictionary of [math]\displaystyle{ n }[/math] words, find all Babbage squares starting with that word. For example, two squares for the word hair are:
[math]\displaystyle{ \begin{vmatrix} h & a & i & r\\ a & i & d & e\\ i & d & l & e\\ r & e & e & f \end{vmatrix}\begin{vmatrix} h & a & i & r\\ a & l & t & o\\ i & t & e & m\\ r & o & m & b \end{vmatrix} }[/math]

Solution


9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph.

Combinatorial Optimization

For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice?

9.21. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470).

Solution


9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537).


9.23. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586).

Solution


9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604).


9.25. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608).

Solution


9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618).


9.27. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678).

Solution

Interview Problems

9.28


9.27


9.28


9.29


9.30


9.31


9.32


9.33


Back to Chapter List