## Exercises
#### Sections 5.1 and 5.2
1. Apply the Backtracking algorithm for the *n*-Queens problem ([Algorithm 5.1](LiB0040.html#492)) to the problem instance in which *n* = 8, and show the actions step by step. Draw the pruned state space tree produced by this algorithm up to the point where the first solution is found.
2. Write a backtracking algorithm for the *n*-Queens problem that uses a version of procedure *expand* instead ofa version of procedure *checknode*.
3. Show that, without backtracking, 155 nodes must be checked before the first solution to the *n* = 4 instance of the *n*-Queens problem is found (in contrast to the 27 nodes in [Figure 5.4](LiB0039.html#483)).
4. Implement the Backtracking algorithm for the *n*-Queens problem ([Algorithm 5.1](LiB0040.html#492)) on your system, and run it on problem instances in which *n* = 4, 8, 10, and 12.
5. Improve the Backtracking algorithm for the *n*-Queens problem ([Algorithm 5.1](LiB0040.html#492)) by having the promising function keep track of the set of columns, of left diagonals, and of right diagonals controlled by the queens already placed.
6. Modify the Backtracking algorithm for the *n*-Queens problem ([Algorithm 5.1](LiB0040.html#492)) so that, instead of generating all possible solutions, it finds only a single solution.
7. Suppose we have a solution to the *n*-Queens problem instance in which *n* = 4. Can we extend this solution to find a solution to the problem instance in which *n* = 5? Can we then use the solutions for *n* = 4 and *n* = 5 to construct a solution to the instance in which *n* = 6 and continue this dynamic programming approach to find a solution to any instance in which *n* > 4? Justify your answer.
8. Find at least two instances of the *n*-Queens problem that have no solutions.
#### Section 5.3
1. Implement [algorithm 5.3](LiB0041.html#503) (Monte Carlo estimate for the Backtracking algorithm for the *n*-Queens problem) on your system, run it 20 times on the problem instance in which *n* = 8, and find the average of the 20 estimates.
2. Modify the Backtracking algorithm for the *n*-Queens problem ([Algorithm 5.1](LiB0040.html#492)) so that it finds the number of nodes checked for an instance of a problem, run it on the problem instance in which *n* = 8, and compare the result against the average of Exercise 9.
#### Section 5.4
1. Use the Backtracking algorithm for the Sum-of-Subsets problem ([Algorithm 5.4](LiB0042.html#517)) to find all combinations of the following numbers that sum to *W* = 52:
![](https://box.kancloud.cn/b757c43a6ea4fda8939b6029d9924090_400x18.jpg)
Show the actions step by step.
2. Implement the Backtracking algorithm for the Sum-of-Subsets problem ([Algorithm 5.4](LiB0042.html#517)) on your system, and run it on the problem instance of Exercise 11.
3. Write a backtracking algorithm for the Sum-of-Subsets problem that does not sort the weights in advance. Compare the performance of this algorithm with that of [Algorithm 5.4](LiB0042.html#517).
4. Modify the Backtracking algorithm for the Sum-of-Subsets problem ([Algorithm 5.4](LiB0042.html#517)) so that, instead of generating all possible solutions, it finds only a single solution. How does this algorithm perform with respect to [Algorithm 5.4](LiB0042.html#517)?
5. Use the Monte Carlo technique to estimate the efficiency of the Backtracking algorithm for the Sum-of-Subsets problem ([Algorithm 5.4](LiB0042.html#517)).
#### Section 5.5
1. Use the Backtracking algorithm for the *m*-Coloring problem ([Algorithm 5.5](LiB0043.html#527)) to find all possible colorings of the graph below using the three colors red, green, and white. Show the actions step by step.
[![Click To expand](https://box.kancloud.cn/dba706909d8bcf56f1fd701486c0b72c_350x217.jpg)](figu229_1_0.jpg)
2. Suppose that to color a graph properly we choose a starting vertex and a color to color as many vertices as possible. Then we select a new color and a new uncolored vertex to color as many more vertices as possible. We repeat this process until all the vertices of the graph are colored or all the colors are exhausted. Write an algorithm for this greedy approach to color a graph of *n* vertices. Analyze this algorithm and show the results using order notation.
3. Use the algorithm of Exercise 17 to color the graph of Exercise 16.
4. Suppose we are interested in minimizing the number of colors used in coloring a graph. Does the greedy approach of Exercise 17 guarantee an optimal solution? Justify your answer.
5. Compare the performance of the Backtracking algorithm for the *m*-Coloring problem ([Algorithm 5.5](LiB0043.html#527)) and the greedy algorithm of Exercise 17. Considering the result(s) of the comparison and your answer to Exercise 19, why might one be interested in using an algorithm based on the greedy approach?
6. Write an algorithm for the 2-Coloring problem whose time complexity is not worst-case exponential in *n*.
7. List some of the practical applications that are representable in terms of the *m*-Coloring problem.
#### Section 5.6
1. Use the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)) to find all possible Hamiltonian Circuits of the following graph.
[![Click To expand](https://box.kancloud.cn/cfa87f599132d5218d3f7ee2c4169c01_350x149.jpg)](figu229_2_0.jpg)
Show the actions step by step.
2. Implement the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)) on your system, and run it on theproblem instance of Exercise 23.
3. Change the starting vertex in the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)) in Exercise 24 and compare its performance with that of [Algorithm 5.6](LiB0044.html#534).
4. Modify the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)) so that, instead of generating all possible solutions, it finds only a single solution. How does this algorithm perform with respect to [Algorithm 5.6](LiB0044.html#534)?
5. Analyze the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)) and show the worst-case complexity using order notation.
6. Use the Monte Carlo Technique to estimate the efficiency of the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)).
#### Section 5.7
1. Compute the remaining values and bounds after visiting node (4, 1) in [Example 5.6](LiB0045.html#541) ([Section 5.7.1](LiB0045.html#538)).
2. Use the Backtracking algorithm for the 0–1 Knapsack problem ([Algorithm 5.7](LiB0045.html#548)) to maximize the profit for the following problem instance. Show the actions step by step.
![](https://box.kancloud.cn/23ab4257ffb72747095c4706a5ea4f01_228x158.jpg)
3. Implement the Backtracking algorithm for the 0–1 Knapsack problem ([Algorithm 5.7](LiB0045.html#548)) on your system, and run it on the problem instance of Exercise 30.
4. Implement the dynamic programming algorithm for the 0–1 Knapsack problem (see Section 4.4.3) and compare the performance of this algorithm with the Backtracking algorithm for the 0–1 Knapsack problem ([Algorithm 5.7](LiB0045.html#548)) using large instances of the problem.
5. Improve the Backtracking algorithm for the 0–1 Knapsack problem ([Algorithm 5.7](LiB0045.html#548)) by calling the promising function after only a move to the right.
6. Use the Monte Carlo technique to estimate the efficiency of the Backtracking algorithm for the 0–1 Knapsack problem ([Algorithm 5.7](LiB0045.html#548)).
#### Additional Exercises
1. List three more applications of backtracking.
2. Modify the Backtracking algorithm for the *n*-Queens problem ([Algorithm 5.1](LiB0040.html#492)) so that it produces only the solutions that are invariant under reflections or rotations.
3. Given an *n × n × n* cube containing *n*3 cells, we are to place *n* queens in the cube so that no two queens challenge each other (so that no two queens are in the same row, column, or diagonal). Can the *n*-Queens algorithm ([Algorithm 5.1](LiB0040.html#492)) be extended to solve this problem? If so, write the algorithm and implement it on your system to solve problem instances in which *n* = 4 and *n* = 8.
4. Modify the Backtracking algorithm for the Sum-of-Subsets ([Algorithm 5.4](LiB0042.html#517)) to produce the solutions in a variable-length list.
5. Explain how we can use the Backtracking algorithm for the *m*-Coloring problem ([Algorithm 5.5](LiB0043.html#527)) to color the edges of the graph of Exercise 16 using the same three colors so that edges with a common end receive different colors.
6. Modify the Backtracking algorithm for the Hamiltonian Circuits problem ([Algorithm 5.6](LiB0044.html#534)) so that it finds a Hamiltonian Circuit with minimum cost for a weighted graph. How does your algorithm perform?
7. Modify the Backtracking algorithm for the 0–1 Knapsack problem ([Algorithm 5.7](LiB0045.html#548)) to produce a solution in a variable–length list.
- Table of Contents
- BackCover
- Foundations of Algorithms Using C++ Pseudocode, Third Edition
- Preface
- Chapter Contents
- Pedagogy
- Course Outlines
- Acknowledgments
- Errors
- Chapter 1: Algorithms - Efficiency, Analysis, and Order
- 1.2 The Importance of Developing Efficient Algorithms
- 1.3 Analysis of Algorithms
- 1.4 Order
- 1.5 Outline of This Book
- Exercises
- Chapter 2: Divide-and-Conquer
- 2.1 Binary Search
- 2.2 Mergesort
- 2.3 The Divide-and-Conquer Approach
- 2.4 Quicksort (Partition Exchange Sort)
- 2.5 Strassen's Matrix Multiplication Algorithm
- 2.6 Arithmetic with Large Integers
- 2.7 Determining Thresholds
- 2.8 When Not to Use Divide-and-Conquer
- Exercises
- Chapter 3: Dynamic Programming
- 3.1 The Binomial Coefficient
- 3.2 Floyd's Algorithm for Shortest Paths
- 3.3 Dynamic Programming and Optimization Problems
- 3.4 Chained Matrix Multiplication
- 3.5 Optimal Binary Search Trees
- 3.6 The Traveling Salesperson Problem
- Exercises
- Chapter 4: The Greedy Approach
- 4.1 Minimum Spanning Trees
- 4.2 Dijkstra's Algorithm for Single-Source Shortest Paths
- 4.3 Scheduling
- 4.4 Huffman Code
- 4.5 The Greedy Approach versus Dynamic Programming: The Knapsack Problem
- Exercises
- Chapter 5: Backtracking
- 5.2 The n-Queens Problem
- 5.3 Using a Monte Carlo Algorithm to Estimate the Efficiency of a Backtracking Algorithm
- 5.4 The Sum-of-Subsets Problem
- 5.5 Graph Coloring
- 5.6 The Hamiltonian Circuits Problem
- 5.7 The 0-1 Knapsack Problem
- Exercises
- Chapter 6: Branch-and-Bound
- 6.1 Illustrating Branch-and-Bound with the 0 - 1 Knapsack problem
- 6.2 The Traveling Salesperson Problem
- 6.3 Abductive Inference (Diagnosis)
- Exercises
- Chapter 7: Introduction to Computational Complexity - The Sorting Problem
- 7.2 Insertion Sort and Selection Sort
- 7.3 Lower Bounds for Algorithms that Remove at Most One Inversion per Comparison
- 7.4 Mergesort Revisited
- 7.5 Quicksort Revisited
- 7.6 Heapsort
- 7.6.1 Heaps and Basic Heap Routines
- 7.6.2 An Implementation of Heapsort
- 7.7 Comparison of Mergesort, Quicksort, and Heapsort
- 7.8 Lower Bounds for Sorting Only by Comparison of Keys
- 7.8.1 Decision Trees for Sorting Algorithms
- 7.8.2 Lower Bounds for Worst-Case Behavior
- 7.8.3 Lower Bounds for Average-Case Behavior
- 7.9 Sorting by Distribution (Radix Sort)
- Exercises
- Chapter 8: More Computational Complexity - The Searching Problem
- 8.1 Lower Bounds for Searching Only by Comparisons of Keys
- 8.2 Interpolation Search
- 8.3 Searching in Trees
- 8.4 Hashing
- 8.5 The Selection Problem: Introduction to Adversary Arguments
- Exercises
- Chapter 9: Computational Complexity and Interactability - An Introduction to the Theory of NP
- 9.2 Input Size Revisited
- 9.3 The Three General Problem Categories
- 9.4 The Theory of NP
- 9.5 Handling NP-Hard Problems
- Exercises
- Chapter 10: Number-Theoretic Algorithms
- 10.1 Number Theory Review
- 10.2 Computing the Greatest Common Divisor
- 10.3 Modular Arithmetic Review
- 10.4 Solving Modular Linear Equations
- 10.5 Computing Modular Powers
- 10.6 Finding Large Prime Numbers
- 10.7 The RSA Public-Key Cryptosystem
- Exercises
- Chapter 11: Introduction to Parallel Algorithms
- 11.1 Parallel Architectures
- 11.2 The PRAM Model
- Exercises
- Appendix A: Review of Necessary Mathematics
- A.2 Functions
- A.3 Mathematical Induction
- A.4 Theorems and Lemmas
- A.5 Logarithms
- A.6 Sets
- A.7 Permutations and Combinations
- A.8 Probability
- Exercises
- Appendix B: Solving Recurrence Equations - With Applications to Analysis of Recursive Algorithms
- B.2 Solving Recurrences Using the Characteristic Equation
- B.3 Solving Recurrences by Substitution
- B.4 Extending Results for n, a Power of a Positive Constant b, to n in General
- B.5 Proofs of Theorems
- Exercises
- Appendix C: Data Structures for Disjoint Sets
- References
- Index
- Index_B
- Index_C
- Index_D
- Index_E
- Index_F
- Index_G
- Index_H
- Index_I
- Index_J
- Index_K
- Index_L
- Index_M
- Index_N
- Index_O
- Index_P
- Index_Q
- Index_R
- Index_S
- Index_T
- Index_U
- Index_V
- Index_W-X
- Index_Y
- Index_Z
- List of Figures
- List of Tables
- List of Algorithms, Examples, and Theorems
- List of Sidebars