15
The N-queens Problem Max Groover Chris Wehunt Forest Gates

people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

The N-queens Problem

Max GrooverChris WehuntForest Gates

Page 2: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

Introduction

The N-queens problem is a constraint satisfaction problem that involves placing a

N amount of queens on a board of dimensions N by N squares so that no queens attack

each other. For no two queens to attack each other, two or more queens:

1. Must not be on the same row

2. Must not be on the same column

3. Must not be on the same top-right to bottom-left diagonal

4. Must not be on the same top-left to bottom-right diagonal

The eight-queens problem was first posed in 1841 by German chess composer

Max Bezzel in the Berliner Schachzeitung. Bezzel posed the question if eight queens

can be placed on standard eight-by-eight chessboard in a non-threatening fashion. The

first solutions were found by the legendary mathematician Carl Friedrich Gauss, who

tackled the problem at a leisurely pace in his own free time. All ninety-two solutions

were not published until the 1850s by Franz Nuack2. Discounting solutions that only

differ from other solutions by symmetrical properties of reflection or rotation, then there

is only 12 truly unique solutions to the eight-queens problem1. After Nuack had

published the first set of solutions, he proposed an extension to the original problem

involving placing N queens on a board of N by N dimensions. This is the problem we

are tackling.

For a standard sized chessboard of dimensions eight by eight squares. There is

a possible one-hundred seventy-eight and a half trillion blind placements of queens. All

of these combinations of queen placements must be checked for validity as a solution.

Using a computer, this is very resource-intensive. Finding more efficient ways of

generating and checking permutations of queen placements is a very common exercise

in algorithm design to this very day. In August 1971, E.W. Dijkstra detailed the eight-

Page 3: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

queens problem during his time at the University of Texas at Austin. He solved this

problem using a depth-first backtracking algorithm to demonstrate his ideas of

structured programming, which is is the extensive use of computer logic to refine

computer programming3.

Formal Problem Statement

X - chessboard with N x N dimensions

Xrc - permutation of a queen at the Rth row and Cth column

Place N amount of queens Xrc on board X subject to:

Abstract

We will use three different algorithms to find a singular solution to the N-queens

problem and analyze their efficiency at finding said solution. The size of N starts at 4, as

there is no valid solutions at N = 2 or N = 3. At N = 1, any algorithm can find a solution

Page 4: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

in constant time, thus N = 1 will be ignored. Each algorithm is run and timed at a given

N to determine their efficiency in finding a valid solution. If an algorithm takes an

untenable amount of time (in this case ten minutes) to find a solution at a certain value

of N, then N will no longer be incremented for testing with that algorithm. The N at which

the program took too long to find a solution will be recorded for later analysis and

comparison with other algorithms.

Algorithms will be analyzed for both their times at finding a solution and their

ability to efficiently find a valid solution at certain values of N in a reasonable amount of

time. The algorithms we have selected for experimentation are as follows:

● Brute force

● Backtracking

● Hill climbing

Brute Force

The brute force approach generates every possible combination of queen

placements, checking each one for validity as a solution using the four constraints

detailed above in the introduction and the formal problem statement. The unrefined

algorithm starts with the queens in an initial placement where the queens are placed in

the first row on each of the N columns, then iterates through all possible permutations

from the initial placements. Using this method, all possible permutations are exhausted

up to a possible 2N^N number of permutations.

There is a better way of doing this. Since we know that no queen can share a

row with another queen, we can assign a queen to each row. A queen on the first row

can be moved one column to the right, and all of the following queens can be moved in

such a way that successfully exhausted all possible permutations of the queen on that

row and column. Once this is done, the queen on the first row it moved one column over

and the process starts all over again. This reduced the amount of possible permutations

to N^N. Thus, the time complexity of this algorithm is O(N^N).

Page 5: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

Figure 1. Graph showing the trendline for the bruteforce algorithm at different Ns.

The brute force algorithm can handle lower values of N efficiently. When N starts

to approach ten, the shortcomings of the algorithm start to become pronounced. When

N = 15, the solution time takes over ten minutes and is no longer considered viable for

use.

Backtracking

The Backtracking algorithm is a recursive approach to the N queens problem. It

works by iterating column by column, placing a single queen in each column. In each

column, it iterates over each row until a spot is found that has no conflicts with already

placed queens. The queen is placed here and the process is repeated in the following

column. If no safe spot exists in the given column, the algorithm reverts to the previous

column and finds a new spot for that queen. If none exists, it returns to the column

before that one, and so on, until a safe spot is found in each column. The “safe spots” in

this algorithm are determined by the four constraints provided by the formal statement

of the problem. If each constraint is satisfied, the queens have been safely placed on

the board. Because of the recursive nature of this algorithm, the worst case time

complexity is equal to O(n!).

Page 6: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

Figure 2. Graphic shows the iterative process of backtracking for the n queens problem. This graphic goes row by row

instead of column by column like our implemented algorithm.

Certain changes can reduce the time this algorithm takes significantly. For

example, we added a line of code to start checking for safe spots in a column on the

third row instead of the first. If no safe spots exist in any other row, it reverts back to the

first two. We found that most collisions occur in these first two rows, so skipping them

reduces the total collisions by a considerable margin.

Figure 3. Graph showing the trendline for the backtracking algorithm at different Ns

Page 7: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

The Backtracking algorithm is extremely fast with lower Ns up to around 30.

Once it hits 30, however, its worst case time complexity starts to become evident.

Hill Climbing

The Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a

Greedy local search. Hill-climbing searches work by starting off with an initial state of a

solution by placing n queens on a n x n board, then iteratively making local changes to it

until either the solution is found or the heuristic gets stuck in a local maximum. The

algorithm always moves in the direction of increasing value(up the hill) and terminates

when no neighbor has a higher value. The heuristic method provides a score for each

problem based on the number of queen collisions. If the score is ten that means there

are ten collisions to be fixed before the problem is solved. If the heuristic score is zero

the problem is solved. There are many ways to try to avoid getting stuck in local

maxima, such as running many searches in parallel, or probabilistically choosing the

successor state, etc. A problem with the Hill Climbing technique is some arrangements

of queens preplaced on the board cannot be improved any further. This is called a local

minima.. The board would be stuck in a local minima if only two of the queens conflict

and the rest were fine. To fix this type of situation, one of the conflicting queens is

moved to a random row to create conflicts and start the process over again. In many

cases, hill-climbing algorithms will rapidly converge on the correct answer. However,

none of these approaches are guaranteed to find the optimal solution. The Big-O

Notation for the Hill Climbing algorithm is worst case O(n) and best case O(logn).

State: Position of the n queens, one per column or row

Successor States: are generated by moving a single queen to another space in its

column (n(n-1))

Page 8: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

Cost of a State: number of constraint violations

Pseudo Code:

Hill Climbing = generate and test + heuristics

t = neighbor with a better heuristic score

1. Pick initial state s

2. Pick t in neighbors(s) with the largest f(t)

3. If f(t) <= f(s) then STOP, return s

4. If s = t Go Back to #2

Figure 4. Graph showing the trendline for the hill climbing algorithm at different Ns

This algorithm was very quick up until the mid 20’s. Once you get to 30 it is

downhill from there. This algorithm uses a heuristic which helps you look for an answer

but the results are subject to chance. The heuristic tells you only how to look not what to

find. This algorithm may fail to reach global maxima.

Page 9: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

Figure 5This image explains how the heuristic function checks the best score from the neighbors and continues down to next set of neighbors and finds the best heuristic score until the score is zero which then returns the correct arrangement of queens.

Figure 6.This image shows the current state climbing the wrong hill to the local maximum instead of climbing the right hill behind it to find the

global maximum which would provide the correct solution.

Page 10: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

Interpretation of results

For clarity of results in comparing the three algorithms, the trendlines for the

algorithms were used rather than the hard data for the graph comparisons. For both the

backtracking solution and hill climbing solution, often a solution could be found in forty

seconds for one solution while the next solution only took a single millisecond. Only the

brute force algorithm gave a consistent increase in times with a rise in the value of N.

Therefore, the trendlines were used in the comparisons.

Figure 7. A graph comparing the time efficiencies of all three algorithms

Comparing all the times of all three algorithms at the same N yields interesting

results. Brute force clearly lags behind the rest both in terms of time efficiency and

abilities to handle larger sizes of N. Backtracking is faster than hill climbing up to N = 30,

where backtracking is no longer able to continue based on our imposed time limit of ten

minutes. Hill climbing, despite appearing slower than backtracking, is able to handle a

larger N.

Conclusion

The most interesting takeaway from our results is that faster times does not

always mean a more efficient algorithm. While often it seems that the algorithm with the

Page 11: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

fastest times is also able to handle the larger workloads, this is not always the case. It is

up to the user to determine what algorithm best fits the type of work they required done,

and the user should put extra research into determining whether faster times or an

ability to handle larger N values is more important.

Our experiment only covered three algorithms. There are many different

algorithms and variations that can be used to solve the N-queens problem such as

branch/bound and genetic. It is possible to achieve much faster times and handle much

larger values of N using some of these algorithms. Some very efficient algorithms can

allegedly handle Ns upwards of five hundred.

Future work would entail evaluating these other algorithms to determine their

efficiencies compared to the ones we have already looked into, and to see if they can

handle larger values of N. The algorithms we have used could also possibly be refined

to squeeze out even greater efficiency. It would be very interesting to find the absolute

biggest and baddest algorithm for solving N queens.

Works Cited

1) Wikipedia - Eight Queens Puzzle

https://en.wikipedia.org/wiki/Eight_queens_puzzle

2) The Initial n-Queens problem

http://www.math.armstrong.edu/faculty/brown/InitialNQueens.htm

3) EWD316: A Short Introduction to the Art of Programming by E.W. Dijkstra

https://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD316.9.html

4) Geeks for geeks https://www.geeksforgeeks.org/n-queen-problem-backtracking-

3/

5) CMU Lecture Notes http://www.cs.cmu.edu/~arielpro/15381f16/c_slides/781f16-

2a.pdf

6) N-Queens Research paper

https://pdfs.semanticscholar.org/88f6/ca4292fd72b24e005858041829dbc327674

3.pdf

Page 12: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/S2019 papers and... · Web viewThe Hill Climbing algorithm is a minimum-conflicts algorithm, it is also known as a Greedy local

http://www.math.armstrong.edu/faculty/brown/InitialNQueens.htm