Chapter 9

From The Algorithm Design Manual Solution Wiki
Jump to navigation Jump to search

Combinatorial Search


9.1. A derangement is a permutation of such that no item is in its proper position, that is, for all . Write an efficient backtracking program with pruning that constructs all the derangements of items.


9.2. Multisets are allowed to have repeated elements. A multiset of n items may thus have fewer than 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 , find all permutations of the elements of the multiset such that for each integer from 1 to the number of intervening elements between its two appearances is equal to value of the element. For example, when the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3].


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 contains a total of distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for :
Give an efficient algorithm that returns the th of permutations in this see quence, for inputs and . For efficiency it should not construct the first , for a given value of .

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


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

9.9. Given a specified total t and a multiset of integers, find all distinct subsets from whose elements add up to . For example, if and , then there are four different sums that equal , and . A number can be used within a sum up to the number of times it appears in , and a single number counts as a sum.


9.10. Given a specified total and a multiset of integers, find all distinct subsets from whose elements add up to . For example, if and , then there are four different sums that equal , and . A number can be used within a sum up to the number of times it appears in , and a single number counts as a sum.

9.11. A team assignment of players is a partitioning of them into two teams with exactly people per team. For example, if the players are named , there are three distinct ways to partition them into two equal teams: .
(a) List the 10 possible team assignments for players.
(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution.


9.12. Given an alphabet , a set of forbidden strings , and a target length , give an algorithm to construct a string of length on without any element of as a substring. For , the two possible solutions are 0000 and 1111. For and , no such string exists.

9.13. In the -partition problem, we need to partition a multiset of positive integers into disjoint subsets that have equal sum. Design and implement an algorithm for solving the -partition problem.


9.14. You are given a weighted directed graph with vertices and 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 of minimum mean weight.

9.15. In the turnpike reconstruction problem, you are given a multiset of distances. The problem is to place points on the line such that their pairwise distances are . For example, the distances 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.


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 chessboard can make where the knight visits every square only once.


9.18. A Boggle board is an 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:
contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board consistent with a dictionary .

9.19. A Babbage square is a grid of words that reads the same across as it does down. Given a -letter word and a dictionary of words, find all Babbage squares starting with that word. For example, two squares for the word hair are:


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








Interview Problems









Back to Chapter List