Exercises: Backtracking
These exercises review the two lessons on backtracking. They are different from the seminar activities (paths, Hamiltonian cycles and the traveling salesman) and from the lab problems, which are left for those sessions. Try each one before reading its solution: the solutions are grouped at the end of the page.
7.1. Balanced parentheses. Generate all the strings of pairs of balanced parentheses (for : ((())), (()()), (())(), ()(()), ()()()). Design a backtracking algorithm that never generates an invalid prefix. How many solutions are there for ? How many nodes does your tree have, compared with generating every string of parentheses and checking at the end?
7.2. Derangements. A derangement is a permutation in which no element stays in its original position (for example, a secret Santa where nobody gets themselves). Modify the permutation algorithm of the examples lesson to generate only derangements. How many are there for ?
7.3. Splitting a text into words. Given the string catsanddog and the dictionary formed by the words cat, cats, and, sand and dog, find every way to split the string into dictionary words. Design a backtracking algorithm. In the worst case it is exponential; how can dynamic programming help?
7.4. Counting nodes. A backtracking algorithm decides, for each of elements, whether to include it or not, and never prunes. How many nodes does its tree have? And an algorithm that generates every permutation of elements? Give both counts for .
7.5. Which scheme? For each task, would you use the "first solution" or the "all solutions" scheme, and what would you keep during the search?
- (a) Is there any way to place 10 queens on a board?
- (b) How many solutions does a Sudoku have? (A well-posed Sudoku must have exactly one.)
- (c) The cheapest way to assign 12 tasks to 12 agents.
Solutions
7.1. Build the string character by character, keeping the number of opening (o) and closing (c) parentheses used. An opening parenthesis can be added if o < n; a closing one only if c < o (otherwise the prefix would have more closings than openings).
void generate(String s, int o, int c, int n) {
if (s.length() == 2 * n) {
System.out.println(s);
return;
}
if (o < n) generate(s + "(", o + 1, c, n);
if (c < o) generate(s + ")", o, c + 1, n);
}
There are 14 solutions for (the Catalan numbers: 1, 2, 5, 14, 42, ...). Our implementation generated 64 nodes, against the 511 nodes of the full binary tree of height 8 that generates all strings before checking them. For : 16,796 solutions, 82,499 nodes, against more than 2 million.
7.2. Add one condition to the candidates: element cannot be placed in position if .
for (int i = 0; i < n; i++)
if (!used[i] && i != level) { ... }
For there are 44 derangements, out of permutations; our tree had 147 nodes. In general, the proportion of derangements tends to : the probability that nobody gets themselves in a random secret Santa is about 37% whatever the number of people.
7.3. At each position , try every dictionary word that starts there, and recurse from the end of the word. A solution is reached at the end of the string.
void split(String s, int i, List<String> words) {
if (i == s.length()) {
System.out.println(String.join(" ", words));
return;
}
for (int j = i + 1; j <= s.length(); j++)
if (dictionary.contains(s.substring(i, j))) {
words.add(s.substring(i, j));
split(s, j, words);
words.remove(words.size() - 1);
}
}
There are 2 solutions: cat sand dog and cats and dog. The number of splittings can be exponential (with aaaa...a and the dictionary with the words a and aa, it grows like the Fibonacci numbers), and then no algorithm can list them faster. But if we only need to know whether a splitting exists, the question "can the suffix from be split?" has only distinct subproblems: storing their answers (memoization) or filling a table from the end gives an dynamic programming algorithm (plus the cost of the dictionary lookups). Memoization also prunes the backtracking search: a position known to be a dead end is not explored again.
7.4. A binary tree of height has nodes: 2,047 for . The permutation tree has nodes, less than : for , 9,864,101 nodes. (This is exactly the number of calls counted for the task assignment without pruning in the examples lesson.)
7.5.
- (a) First solution, with a
foundflag: we only need to know whether one exists (and it does: every has solutions). - (b) All solutions, with a counter. In practice, we can stop as soon as the counter reaches 2: that is enough to know that the Sudoku is not well posed.
- (c) All solutions, keeping the best assignment found so far and its cost, which is also used to prune.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.