Figure 3.5 Almost a solution to the 8-queens problem. (Solution is left as an exercise.) Although efficient special-purpose algorithms exist for this problem and for the whole n-queens family, it remains a useful test problem for search algorithms. There are two main kinds of formulation. An incremental formulation involves operators that augment the state description, starting with an empty state; for the 8-queens problem, this means that each action adds a queen to the state. A complete-state formulation starts with all 8 queens on the board and moves them around. In either case, the path cost is of no interest because only the final state counts. The first incremental formulation one might try is the following: • States: Any arrangement of 0 to 8 queens on the board is a state. • Initial state: No queens on the board. • Actions: Add a queen to any empty square. • Transition model: Returns the board with a queen added to the specified square. • Goal test: 8 queens are on the board, none attacked. In this formulation, we have 64 · 63 ··· 57 ? 1.8 × 10^14 possible sequences to investigate. A better formulation would prohibit placing a queen in any square that is already attacked: • States: All possible arrangements of n queens (0 ? n ? 8), one per column in the leftmost n columns, with no queen attacking another. • Actions: Add a queen to any square in the leftmost empty column such that it is not attacked by any other queen. This formulation reduces the 8-queens state space from 1.8 × 10^14 to just 2,057, and solutions are easy to find. On the other hand, for 100 queens the reduction is from roughly 10^400 states to about 10^52 states (Exercise 3.5)—a big improvement, but not enough to make the problem tractable. Section 4.1 describes the complete-state formulation, and Chapter 6 gives a simple algorithm that solves even the million-queens problem with ease.
Added by Shawna M.
Close
Step 1
It is a classic AI problem where the task is to place n queens on an n×n chessboard such that no two queens threaten each other. This means that no two queens share the same row, column, or diagonal. The "efficient" incremental formulation mentioned in the Show more…
Show all steps
Your feedback will help us improve your experience
Haricharan Gupta and 81 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Consider the n-queens problem using the "efficient" incremental formulation given on page 72. Explain why the state space has at least 3 − n! states and estimate the largest n for which exhaustive exploration is feasible. Hint: Derive a lower bound on the branching factor by considering the maximum number of squares that a queen can attack in any column.
Supreeta N.
(15 pts) Recursive Backtracking. Refer to the n-queens problem from the textbook and from the lecture. You are given above code which finds a solution for n-queens problem. Assume the current board is 4 x 4 (thus 4 Queens problem). FYI, it is considered safe to place a queen to a particular row and column when there is no other queen to attack (i.e., no other queen in the row, or the column, or in the two diagonal directions). // post: prints all solutions to n queens public static void solve(Board b) { explore(b, 1); } // pre : queens have been safely placed in columns 1 through (col - 1) // post: recursively searches the board for all solutions starting at col, // and printing any solution it finds // hint: feel free to modify the code below private static void explore(Board b, int col) { if (col > b.size()) { b.print(); } else { for (int row = 1; row <= b.size(); row++) { if (b.safe(row, col)) { b.place(row, col); explore(b, col + 1); b.remove(row, col); } } } } Below is the one of the solutions found by above code given. 2-1) Hint - true or false: This solution is the first solution found by the code given. (5 pts) 2-2) Fill in the blanks. Right after the above solution gets printed when the method returns to its previous caller, at line 12 the value of col is 4 and the value of row is 2.
Exercises $38-45$ involve the Reve's puzzle, the variation of the Tower of Hanoi puzzle with four pegs and $n$ disks. Before presenting these exercises, we describe the Frame-Stewart algorithm for moving the disks from peg 1 to peg 4 so that no disk is ever on top of a smaller one. This algorithm, given the number of disks $n$ as input, depends on a choice of an integer $k$ with $1 \leq k \leq n .$ When there is only one disk, move it from peg 1 to peg 4 and stop. For $n>1$ , the algorithm proceeds recursively, using these three steps. Recursively move the stack of the $n-k$ smallest disks from peg 1 to peg $2,$ using all four pegs. Next move the stack of the $k$ largest disks from peg 1 to peg $4,$ using the three-peg algorithm from the Tower of Hanoi puzzle without using the peg holding the $n-k$ smallest disks. Finally, recursively move the smallest $n-k$ disks to peg $4,$ using all four pegs. Frame and Stewart showed that to produce the fewest moves using their algorithm, $k$ should be chosen to be the smallest integer such that $n$ does not exceed $t_{k}=k(k+1) / 2,$ the $k$ th triangular number, that is, $t_{k-1}<n \leq t_{k}$ . The long-standing conjecture, known as Frame's conjecture, that this algorithm uses the fewest number of moves required to solve the puzzle, was proved by Thierry Bousch in 2014 . Show that if $R(n)$ is the number of moves used by the Frame-Stewart algorithm to solve the Reve's puzzle with $n$ disks, where $k$ is chosen to be the smallest integer with $n \leq k(k+1) / 2,$ then $R(n)$ satisfies the recurrence relation $R(n)=2 R(n-k)+2^{k}-1,$ with $R(0)=0$ and $R(1)=1$.
Advanced Counting Techniques
Applications of Recurrence Relations
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD