# Combinatorial Search

### Permutations

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

9.2. Multisets are allowed to have repeated elements. A multiset of n items may thus have fewer than ${\displaystyle n!}$ 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 ${\displaystyle n}$, find all permutations of the ${\displaystyle 2n}$ elements of the multiset ${\displaystyle S={1,1,2,2,3,3,...,n,n}}$ such that for each integer from 1 to ${\displaystyle n}$ the number of intervening elements between its two appearances is equal to value of the element. For example, when ${\displaystyle n=3}$ 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 ${\displaystyle {1,2,3,...,n}}$ contains a total of ${\displaystyle n!}$ distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for ${\displaystyle n=3}$:
${\displaystyle [123,132,213,231,312,321]}$
Give an efficient algorithm that returns the ${\displaystyle k}$th of ${\displaystyle n!}$ permutations in this see quence, for inputs ${\displaystyle n}$ and ${\displaystyle k}$. For efficiency it should not construct the first ${\displaystyle k-1, for a given value of ${\displaystyle n}$.

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 ${\displaystyle S}$ of ${\displaystyle n}$ integers, find all distinct subsets from ${\displaystyle S}$ whose elements add up to ${\displaystyle t}$. For example, if ${\displaystyle t=4}$ and ${\displaystyle S={4,3,2,2,1,1}}$, then there are four different sums that equal ${\displaystyle t:4,3+1,2+2}$, and ${\displaystyle 2+1+1}$. A number can be used within a sum up to the number of times it appears in ${\displaystyle S}$, and a single number counts as a sum.

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

9.11. A team assignment of ${\displaystyle n=2k}$ players is a partitioning of them into two teams with exactly ${\displaystyle k}$ people per team. For example, if the players are named ${\displaystyle {A,B,C,D}}$, there are three distinct ways to partition them into two equal teams: ${\displaystyle {{A,B},{C,D}},{{A,C},{B,D}},and{{A,D},{B,C}}}$.
(a) List the 10 possible team assignments for ${\displaystyle n=6}$ 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 ${\displaystyle \Sigma }$, a set of forbidden strings ${\displaystyle S}$, and a target length ${\displaystyle n}$, give an algorithm to construct a string of length ${\displaystyle n}$ on ${\displaystyle \Sigma }$ without any element of ${\displaystyle S}$ as a substring. For ${\displaystyle \Sigma ={0,1},S={01,10}[itex],and[itex]n=4}$, the two possible solutions are 0000 and 1111. For ${\displaystyle S={0,11}}$ and ${\displaystyle n=4}$, no such string exists.

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

9.14. You are given a weighted directed graph ${\displaystyle G}$ with ${\displaystyle n}$ vertices and ${\displaystyle m}$ 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 ${\displaystyle G}$ of minimum mean weight.

9.15. In the turnpike reconstruction problem, you are given a multiset ${\displaystyle D}$ of ${\displaystyle n(n-1)/2}$ distances. The problem is to place ${\displaystyle n}$ points on the line such that their pairwise distances are ${\displaystyle D}$. For example, the distances ${\displaystyle D={1,2,3,4,5,6}}$ 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.

9.16

9.17

9.18

9.19

9.20

9.21

9.22

9.23

9.24

9.25

9.26

9.27

### Interview Problems

9.28

9.27

9.28

9.29

9.30

9.31

9.32

9.33

Back to Chapter List