Giter VIP home page Giter VIP logo

pranavmswamy / knights-tour Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 477 KB

Knight’s Tour is a sequence of valid moves of a knight on a chessboard in such a way that the knight covers all the squares on the board. This is a Hamiltonian path problem in computer science which is NP-complete. In this project, I compare the time complexities of Knight's Tour while implementing i) Backtracking, and ii) Warnsdorff's heuristic.

Java 100.00%
knight-tour warnsdorff-rule backtracking-algorithm warnsdorff-heuristic chess

knights-tour's Introduction

knights-tour

Knight’s Tour is a sequence of valid moves of a knight on a chessboard in such a way that the knight covers all the squares on the board. This is a Hamiltonian path problem in computer science which is NP-complete. There are two types of tours – open and closed. If the knight ends on a square from which it can start the tour again from the same square it began, the tour is said to be closed. Otherwise, it is said to be an open tour.

Moves of a Knight

Moves of a Knight. The knight moves one square in one direction and two squares in the other in any order. Hence, if a knight is on a white square, it will end up on a black square and vice versa.

Moves of a knight

Algorithms to compute Knight’s Tour

There are many algorithmic techniques that can be used to compute the Knight’s tour on a chessboards of various dimensions such as brute-force, backtracking, divide and conquer, Warnsdorff’s rule, and also neural networks. This project focuses on two techniques - Warnsdorff’s rule and backtracking, and analyses their time complexity.

Backtracking Algorithm

Backtracking is an algorithm strategy where we go on building the solution in increments. The algorithm discards an increment as soon as it does not lead to a solution and backtracks to try the other increments. In the knight’s tour, the algorithm adds each valid square to the solution and builds the path recursively. If this does not lead to a solution, the square is removed and the algorithm tries other valid alternate squares. It performs an exhaustive search in the worst case. The worst case time complexity of backtracking algorithm is O(8^(N^2)), where N is the dimension of the chessboard. The number 8 serves as the total number of possible valid moves from a given position on the board. The backtracking algorithm tries every possible set of moves in order to obtain a complete solution, hence we can use this method to prove that the tour exists starting from each and every position on the board. Due to computational limitations, we have had to restrict the board size to a maximum of 8×8.

Backtracking of a knight in an 8x8 board. Screenshot

Backtracking in a 8x8 board. BAcktracking example

Warnsdorff’s Rule

In 1823, H. C. von Warnsdorff proposed a very efficient heuristic for finding the knight’s tour. The rule is as follows – Let an adjacent square be the one on which the knight will land on by making a valid move from the current square. Let the rank of a square on the chessboard be the number of unvisited adjacent squares the knight can move next. The next square in the path is the unvisited adjacent square which has the least rank of all the other unvisited adjacent squares. Ties may or may not be broken at random. Since Warnsdorff’s rule is a heuristic, it cannot guarantee a complete tour starting from every square. This is due to the random tie-breaks done while choosing the minimum rank when there is more than one minimum rank in order to traverse the knight to the next square. Warnsdorff’s rule finds a tour in linear time. The time complexity of this heuristic is O(N×N) where N is the dimension of the chessboard.

An example of Warnsdorff's heuristic on a 20x20 board. Warnsdorff's Rule on a 20x20 board

More...

Paper published on this project accessible here.

knights-tour's People

Contributors

pranavmswamy avatar

Watchers

 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.