Chapter 4: Backtracking
Multiple Choice · Core Idea
Question 1: How It Differs from Brute Force
What is the key property that distinguishes backtracking from brute force?
A: The solution is built step by step as a partial solution; when a step violates a constraint, that branch is abandoned and the subtree below it is never generated. B is brute force (8⁸ ≈ 16.7 million complete placements for 8 queens), C is the greedy approach, and D is dynamic programming.
Multiple Choice · Core Idea
Question 2: Forgetting to Undo
What happens if the remove(partial, c) (undo) line of the general template is deleted?
B: The partial solution (board, path, visited array) is a shared structure; if it is not restored on return, the next candidate is tested together with the leftovers of the previous attempt. In n-queens, if b[r][c] = 0 is skipped, or in word search visited = false, valid branches are wrongly pruned or invalid "solutions" are produced. Only primitive values passed as parameters (such as sum) are restored automatically.
Output Question · Subsets and Permutations
Question 3: Generating Permutations
The code below is called as generate(new ArrayList<>(), 3) (used is a boolean array with 4 cells). What does it print? How many nodes does the state-space tree have, including the root?
static boolean[] used = new boolean[4]; static void generate(List<Integer> path, int n) { if (path.size() == n) { for (int x : path) System.out.print(x); System.out.print(" "); return; } for (int v = 1; v <= n; v++) { if (!used[v]) { used[v] = true; path.add(v); // choose generate(path, n); // explore path.remove(path.size() - 1); // undo used[v] = false; } } }
Show answer
Output: 123 132 213 231 312 321
- Candidates are tried in the order 1, 2, 3, so the permutations come out in lexicographic order.
- Tree: root 1 + level 1: 3 + level 2: 3·2 = 6 + level 3: 6 = 16 nodes (calls of generate); 6 leaves = 3!.
- Without undoing through
used[v] = falseandremove, only 123 would be printed. O(n · n!) for n items.
Step-by-Step Trace · Subsets and Permutations
Question 4: Tracing Subset Sum
For a = {3, 5, 6, 7} and TARGET = 15, how does search(0, 0) traverse the state-space tree? Which subset is found first?
a = {3, 5, 6, 7}, TARGET = 15. Each level has two branches for one item: first "take" (+a[i]), then "skip" (−). The sum at the root is 0.
search(1, 3): a[0] = 3 taken → sum 3. It will try taking a[1] = 5 first.
search(2, 8): a[1] = 5 taken → sum 8. It will try taking a[2] = 6 first.
search(3, 14): a[2] = 6 taken → sum 14. It will try taking a[3] = 7 first.
search(4, 21): 21 > 15 → pruned, false. Nothing below this node is ever generated.
search(4, 14): no items left (i == n) and the sum is not 15 → false; return.
search(3, 8): a[2] = 6 skipped → sum 8. It will try taking a[3] = 7 first.
search(4, 15): sum == TARGET → true; true is passed up through all the calls. Solution {3, 5, 7}.
boolean search(int i, int sum) { if (sum == TARGET) return true; if (i == n || sum > TARGET) return false; taken[i] = true; // take if (search(i + 1, sum + a[i])) return true; taken[i] = false; // skip return search(i + 1, sum); }
Multiple Choice · Subsets and Permutations
Question 5: The Permutation Tree
How many nodes, including the root, does the state-space tree have for a backtracking search that generates all permutations of {1, 2, 3, 4}?
C: Levels: root 1, then 4, 4·3 = 12, 4·3·2 = 24, 24 → 1 + 4 + 12 + 24 + 24 = 65. 24 is only the number of leaves (4!); 256 = 4⁴ is the number of leaves of the unpruned tree that allows the same item to be chosen again.
Step-by-Step Trace · n-Queens
Question 6: Tracing 5 Queens
For n = 5, how does solve(b, 0) find the first solution? The reddish cells are the columns threatened by the queens in earlier rows.
n = 5, empty board. solve(b, 0): rows are filled from top to bottom; in each row columns 0, 1, 2, … are tried in order.
Row 0: column 0 is safe → b[0][0] = 1, solve(b, 1).
Row 1: columns 0, 1 are threatened; column 2 is safe → b[1][2] = 1, solve(b, 2).
Row 2: columns 0, 1, 2, 3 are threatened; column 4 is safe → b[2][4] = 1, solve(b, 3).
Row 3: column 0 is threatened; column 1 is safe → b[3][1] = 1, solve(b, 4).
Row 4: columns 0, 1, 2 are threatened; column 3 is safe → b[4][3] = 1, solve(b, 5).
row == n → true; true is passed up the stack. Solution [0, 2, 4, 1, 3]: 5 placements, no backtracking at all (for 4 queens, 8 placements and 4 backtracks were needed).
boolean solve(int[][] b, int row) { if (row == n) return true; for (int col = 0; col < n; col++) { if (isSafe(b, row, col)) { b[row][col] = 1; if (solve(b, row + 1)) return true; b[row][col] = 0; // undo } } return false; }
Output Question · n-Queens
Question 7: 6 Queens: First Solution
A placed counter that increases at every placement was added to the solve method from the lecture. What does the program print for n = 6 (columns in row order)?
static int placed = 0; static boolean solve(int[][] b, int row) { if (row == n) return true; for (int c = 0; c < n; c++) { if (isSafe(b, row, c)) { b[row][c] = 1; placed++; if (solve(b, row + 1)) return true; b[row][c] = 0; } } return false; } // main (n = 6): solve(b, 0); then the column // of the queen in each row is put in an array: System.out.println( Arrays.toString(cols) + " " + placed);
Show answer
Output: [1, 3, 5, 0, 2, 4] 31
- Columns are tried from smallest to largest, so the first solution in lexicographic order is found: every branch that starts with column 0 in row 0 hits a dead end; the solution starts with column 1.
- 31 queens are placed before the first solution (only 5 for n = 5, only 9 for n = 7): the work does not grow smoothly with n.
- n = 6 has only 4 solutions; finding all of them takes 152 placements.
Output Question · n-Queens
Question 8: The O(1) Safety Test
In the version that uses flag arrays, for n = 8 a queen is placed on square (2, 5). Which cells of cols, d1 and d2 become true? Are squares (4, 3) and (5, 2) safe afterwards?
boolean[] cols = new boolean[n]; boolean[] d1 = new boolean[2*n-1]; // r+c boolean[] d2 = new boolean[2*n-1]; // r-c+n-1 // is (r, c) safe? int a = r + c, b = r - c + n - 1; if (cols[c] || d1[a] || d2[b]) continue; cols[c] = d1[a] = d2[b] = true; // place
Show answer
- cols[5], d1[2 + 5] = d1[7], d2[2 − 5 + 7] = d2[4] become true.
- (4, 3): d1[4 + 3] = d1[7] is already true → on the same anti-diagonal (2,5)–(3,4)–(4,3): not safe.
- (5, 2): cols[2] is false, but d1[7] → 5 + 2 = 7 is true → again on the anti-diagonal: not safe. (5, 1), however, has d1[6], d2[11] and cols[1] all false: safe.
On the same anti-diagonal r + c is constant, on the same diagonal r − c is constant; + n − 1 makes the index start from 0. The test becomes O(1) instead of O(n).
Multiple Choice · n-Queens
Question 9: Counting All Solutions
What is the correct way to change the solve method, which stops at the first solution, so that it counts all solutions?
A: Returning false keeps the search going: the caller undoes the board and tries the next column. If true is returned, the search stops at the first solution. With this change, 4 solutions are counted for n = 6 and 92 for n = 8; the numbers of placements are 152 and 2056, respectively.
Output Question · Min-Conflicts
Question 10: Conflict Counts
On a 4 × 4 board there is one queen in each column; the rows of the queens in columns 0, 1, 2, 3 are [2, 0, 3, 2]. The queen in column 3 was chosen for repair. If this queen were moved to each row within its column, how many of the other queens would it conflict with? Where does min-conflicts move it?
Show answer
Conflict counts for column 3 (rows 0–3): 1, 0, 3, 1.
- row 0: 1 ((0,1) same row) · row 1: 0 · row 2: 3 ((2,0) same row, (0,1) diagonal, (3,2) diagonal) · row 3: 1 ((3,2) same row). Notation: (r, c) = (row, column).
- The current row 2 is the worst (3). The smallest value is 0, only in row 1 → the queen moves there.
- New placement [2, 0, 3, 1]: no conflicts remain; the 4-queens solution was found with a single repair step. With a tie, the choice would be random.
Multiple Choice · Min-Conflicts
Question 11: The Limit of Min-Conflicts
Comparing the min-conflicts heuristic with backtracking, which of the following is true?
D: Min-conflicts is a local search that starts from a complete placement and repairs it locally: it can wander on plateaus and is not complete; hitting the step limit does not mean "no solution". On the other hand, it finds a solution in a few dozen steps on average even for n = 1,000,000. Backtracking is complete and can count all solutions (92 for 8) but slows down quickly as n grows.
Output Question · Sudoku
Question 12: The Corner of the Box
The box computation from the isValid method of a 9 × 9 Sudoku is shown below. What does the code print? Which rows and columns does the box of cell (7, 4) cover?
int[][] h = {{7, 4}, {2, 8}, {5, 5}}; for (int[] x : h) { int br = x[0] / 3 * 3; int bc = x[1] / 3 * 3; System.out.print(br + " " + bc + " "); }
Show answer
Output: 6 3 0 6 3 3
- Integer division: 7 / 3 = 2 → 2·3 = 6; 4 / 3 = 1 → 3. The box of (7, 4) is rows 6–8, columns 3–5.
- (2, 8) → (0, 6); (5, 5) → (3, 3).
r / 3 * 3rounds r down to a multiple of 3; if the multiplication is forgotten (only r / 3), the box number (0–2) is obtained, not the index. - If the box check is forgotten, "solutions" with correct rows and columns but invalid boxes are produced.
Multiple Choice · Sudoku
Question 13: The MRV Heuristic
What does the MRV (minimum remaining values) heuristic change in a Sudoku solver?
D: MRV is a variable ordering: instead of the first empty cell in row order, the cell with the fewest candidates is filled. Cells with a single candidate are filled first and contradictions show up early; for the puzzle in the lecture, the number of backtracks drops from 4157 to 0. B is a separate improvement (bit masks).
Step-by-Step Trace · Graph Coloring and Hamiltonian Path
Question 14: Tracing Graph Coloring
Edges 0–1, 0–4, 1–2, 1–3, 1–4, 2–3, 3–4; m = 3. How does color(0) assign colors, and where does it backtrack?
5 vertices, m = 3 colors (c1, c2, c3). Vertices are colored in the order 0, 1, 2, …; at each vertex colors 1, 2, 3 are tried in order.
Vertex 0: color 1 is allowed → assigned, color(1).
Vertex 1: color 1 is used by a neighbor; color 2 is allowed → assigned, color(2).
Vertex 2: color 1 is allowed → assigned, color(3).
Vertex 3: colors 1, 2 are used by neighbors; color 3 is allowed → assigned, color(4).
Vertex 4: no color left to try → return false (dead end).
Undo: the color (3) of vertex 3 is erased; the next color will be tried at the same vertex.
Vertex 3: no color left to try → return false (dead end).
Undo: the color (1) of vertex 2 is erased; the next color will be tried at the same vertex.
Vertex 2: color 2 is used by a neighbor; color 3 is allowed → assigned, color(3).
Vertex 3: color 1 is allowed → assigned, color(4).
Vertex 4: colors 1, 2 are used by neighbors; color 3 is allowed → assigned, color(5). All five vertices are colored: a valid 3-coloring [1, 2, 3, 1, 3].
boolean color(int v) { if (v == n) return true; for (int c = 1; c <= m; c++) { if (isSafe(v, c)) { // no neighbor has c colors[v] = c; if (color(v + 1)) return true; colors[v] = 0; // undo } } return false; }
Classic Question · Graph Coloring and Hamiltonian Path
Question 15: The Odd-Length Cycle
The cycle graph C₅ with 5 vertices (0–1–2–3–4–0) is to be colored with m = 2 colors using color(0). What does the search return and why? What is the minimum number of colors needed?
Show answer
- Vertex 0: color 1 → vertex 1: 1 is used by a neighbor, color 2 → vertex 2: color 1 → vertex 3: color 2 → vertex 4: 1 (at 0) and 2 (at 3) are used by neighbors → dead end.
- Backtracking does not change the result: with 2 colors the colors are forced to alternate (1, 2, 1, 2, …) and vertex 4 is adjacent to both colors. The search traverses the whole tree and returns false; this proves that there is no solution (the search is complete).
- Odd-length cycles need 3 colors: 1, 2, 1, 2, 3. Even-length cycles (bipartite graphs) can be colored with 2 colors.
Step-by-Step Trace · Word Search
Question 16: Tracing Word Search
Grid ABCE / SFCS / ADEE. How does the search call starting at (1, 0) find the word "SADE"? (dr = {−1, 1, 0, 0}, dc = {0, 0, −1, 1})
Grid ABCE / SFCS / ADEE, word "SADE". search(grid, "SADE", 0, 1, 0, visited): start from the S at (1, 0). Direction order: up, down, left, right.
(1, 0) = 'S' matches letter 0 of the word → marked visited, the 4 neighbors are tried.
(0, 0) = 'A' matches letter 1 of the word → marked visited, the 4 neighbors are tried.
No neighbor of (0, 0) continues with 'D' (B, border or visited) → mark removed, false.
(2, 0) = 'A' matches letter 1 of the word → marked visited, the 4 neighbors are tried.
(2, 1) = 'D' matches letter 2 of the word → marked visited, the 4 neighbors are tried.
(2, 2) = 'E' matches letter 3 of the word → marked visited, the 4 neighbors are tried.
i == 4 == word length → true. Path: (1,0) S → (2,0) A → (2,1) D → (2,2) E.
boolean search(char[][] grid, String word, int i, int r, int c, boolean[][] visited) { if (i == word.length()) return true; if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || visited[r][c] || grid[r][c] != word.charAt(i)) return false; visited[r][c] = true; for (int d = 0; d < 4; d++) if (search(grid, word, i + 1, r + dr[d], c + dc[d], visited)) return true; visited[r][c] = false; // undo return false; }
Multiple Choice · Word Search
Question 17: Not Removing the Visited Mark
What happens in word search if the line visited[r][c] = false is forgotten?
B: If the mark stays, cells passed through in a failed attempt look "used" in later attempts; a word that exists may not be found. Using the same cell twice on a path (A) is already prevented by the mark itself. The cell is marked first, then the neighbors are explored, and the mark is removed last: choose → explore → undo.
Multiple Choice · Analysis
Question 18: Complexity
Which pairing is correct for the worst-case complexities of backtracking?
C: In Sudoku each of the m empty cells takes 9 candidates: 9ᵐ. In m-coloring each of the n vertices takes m colors, and the check costs O(n) per vertex: O(n · mⁿ). n-queens is O(n² · n!) (O(n!) nodes × n columns × O(n) check), Hamiltonian path O(n · n!), word search O(R · C · 3ᴸ); subsets O(2ⁿ), permutations O(n · n!). Memory, on the other hand, is only O(depth).