ICS 461 Homework #4, Due: Part I Friday, 11 April, Part II Friday, April 25 Instructor - Curt Powley

 

The main task for this homework is to write a program to find
solutions to the N-Queens problem using backtracking. In your
submission, include a list of observations you feel worthwhile, such
as concepts and/or lessons that you learned. Also state what other
people or other references you consulted in doing this homework. It is
okay to discuss general approaches to these problems with other
people, but your code must be your own. Make sure your code includes
high-level descriptions of how it works, including descriptions of
each function. If you can’t accomplish all of the requirements,
it’s better to turn some part in that is working, than to turn in
code that doesn’t work.

 

Use the following approach for your program. Assume each of the N queens is constrained to one row, and the queens are numbered from 0 to N-1, corresponding to rows 0 to N-1. Finding a solution thus corresponds to finding a column placement from 0 to N-1 for each of the N queens.

 

Part I  Basic Backtracking

 

For ordering your search, place queens by increasing value of row. That is place Queen 0, then Queen 1, etc. Of course, when a placement violates a constraint, the program should backtrack (move back up the search tree to make the next legal placement). Your program should determine and output the following:

 

1) total number of solutions for the N-Queens problem with N = 1 through 14 (that is for 14 different problems). For example, as shown on page 201 of the Korf notes, there are two solutions to the Four-Queens problem, so the output from your program for the total number of solutions for N = 4 should be 2 solutions (see table at end for example of a summary report). The reported numbers of solutions should be from actual program calculations and output. Include the program output in your homework submission. Also report the sum of the total numbers of solutions for all these problems.

     

2) nodes generated for solving each problem, where a node generation corresponds to placement of a queen on the board. Also report the sum of all the node generations for all these problems.

 

3) find a single solution for all problems from N = 1 through 27, and report nodes generated to find each solution, and also report the sum of the nodes generated for all 27 problems.

 

4) write your program to print out one solution for all problems from N = 1 through 27. For formatting the solutions, simply print the list of columns occupied by queens on rows 0 through N-1. For example, 1, 3, 0, 2 would be a correct output for the leftmost solution on page 201 of the Korf notes. This would describe placing the queen in row 0 onto column 1, the queen in row 1 onto column 3, the queen in row 2 onto column 0, and the queen in row 3 onto column 2. That is, this notation describes this board:

 

 

c0

c1

c2

c3

r0

 

Q

 

 

r1

 

 

 

Q

r2

Q

 

 

 

r3

 

 

Q

 

 

Part II Backtracking with Minimum-Remaining Values (MRV) heuristic):

 

1) Augment your backtracking program with variable ordering by choosing as the next queen to place the one with the smallest number of legal placements relative to the queens that have already been placed. This is called the Minimum Remaining Values (MRV) heuristic. It is discussed in the Korf notes in section 12.3.1 on page 202 (though it's not called MRV by Korf), and in the Russell textbook on page 143.

 

2) Report the same results as done in Part I above.

 

Include Summary for Part I and Part II submission:

 

In addition to including the direct output from your program for all of the above data, summarize the results for Parts I and II in a table organized as follows (the table will be partially filled for Part I, and should be completely filled for the submission of Part II):

 

 

backtracking

backtracking with MRV

N

number of solutions

nodes generated to find all solutions

nodes generated to find 1 solution

number of solutions

nodes generated to find all solutions

nodes generated to find 1 solution

1

 

 

 

 

 

 

2

 

 

 

 

 

 

3

 

 

 

 

 

 

4

 2

 

 

 2

 

 

5

 

 

 

 

 

 

6

 

 

 

 

 

 

7

 

 

 

 

 

 

8

 

 

 

 

 

 

9

 

 

 

 

 

 

10

 

 

 

 

 

 

11

 

 

 

 

 

 

12

 

 

 

 

 

 

13

 

 

 

 

 

 

14

 

 

 

 

 

 

15

don't do

don't do

 

don't do

don't do

 

16

don't do

don't do

 

don't do

don't do

 

17

don't do

don't do

 

don't do

don't do

 

18

don't do

don't do

 

don't do

don't do

 

19

don't do

don't do

 

don't do

don't do

 

20

don't do

don't do

 

don't do

don't do

 

21

don't do

don't do

 

don't do

don't do

 

22

don't do

don't do

 

don't do

don't do

 

23

don't do

don't do

 

don't do

don't do

 

24

don't do

don't do

 

don't do

don't do

 

25

don't do

don't do

 

don't do

don't do

 

26

don't do

don't do

 

don't do

don't do

 

27

don't do

don't do

 

don't do

don't do

 

Sums

 

 

 

 

 

 

(Totals)