Git Product home page Git Product logo

n-queens's Introduction

Open in GitHub Codespaces Linux/Mac/Windows build status

N Queens

The n-queens problem refers to the problem of placing n queens on an n*n chessboard, such that no two queens are able to attack each other.

Here is an example solution for a 6-queens problem:

6-queens Solution

Though the n-queens problem is more often studied theoretically, it has been shown to have its applications. For instance, in a system requiring deadlock prevention, solving the n-queens problem would be equivalent to finding a set of deadlock free paths [4]. Other practical applications utilizing the n-queens problem include parallel memory storage schemes, VLSI testing, and traffic control [4].

This example demonstrates how to formulate the n-queens problem as a quadratic unconstrained binary optimization (QUBO) problem, which we then solve with Leap's hybrid solvers.

Usage

To run this example:

python n_queens.py

The program prompts the user to enter the number of queens (n) to place. It should be noted that larger n will take longer to run and we do not recommend running this example on n > 200.

The solution image file ('n-queens-solution.png') will be saved in the root directory.

Code Overview

We formulate the n-queens problem as a generalized exact cover problem with four types of constraints:

  1. Exactly one queen in each column.
  2. Exactly one queen in each row.
  3. At most one queen in each diagonal from top-left to bottom-right.
  4. At most one queen in each diagonal from bottom-left to top-right.

Here is a brief overview of the code:

  • Represent each constraint with a unique number (ID).
  • Represent each position on the chessboard with a subset of constraint IDs.
  • Form a binary quadratic model (BQM) using these subsets of constraints.
  • Run the problem (solve the BQM).
  • Validate the solution.
  • Plot the solution on a chessboard and save the solution image file.

Code Specifics

Some notes to consider:

  • Since there is exactly one queen on each row and column, we utilize a generalized version of the exact cover algorithm (specified in [1]) to handle the row and column constraints. This code can be found in exact_cover.py. The diagonal constraints are handled separately in n_queens.py.

  • Each position on the chessboard (each subset of constraint IDs) becomes a variable in the BQM. That means that there are n**2 variables.

References

[1] Andrew Lucas, "Ising formulations of many NP problems", doi:10.3389/fphy.2014.00005

[2] Thijs Metsch, "Dancing links, algorithm X and the n-queens puzzle", http://www.nohuddleoffense.de/2019/01/20/dancing-links-algorithm-x-and-the-n-queens-puzzle/

[3] Wikipedia contributors, "Eight queens puzzle" Wikipedia, The Free Encyclopedia, https://en.wikipedia.org/wiki/Eight_queens_puzzle

[4] Jordan Bell & Brett Stevens, "A survey of known results and research areas for n-queens", doi.org/10.1016/j.disc.2007.12.043

License

Released under the Apache License 2.0. See LICENSE file.

n-queens's People

Contributors

hemantdwave avatar joelpasvolsky avatar randomir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.