Knight Tour. By: Marouf Baghdadi

Similar documents
Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Random Map Generator v1.0 User s Guide

Small Maximal Independent Sets and Faster Exact Graph Coloring

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

5.1 Bipartite Matching

OA4-13 Rounding on a Number Line Pages 80 81

Near Optimal Solutions

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

The Taxman Game. Robert K. Moniot September 5, 2003

Homework until Test #2

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

An Introduction to Number Theory Prime Numbers and Their Applications.

Introduction. Appendix D Mathematical Induction D1

Approximation Algorithms

Introduction to Algorithms. Part 3: P, NP Hard Problems

Introduction Solvability Rules Computer Solution Implementation. Connect Four. March 9, Connect Four

Genetic Algorithms and Sudoku

Chapter 6: Graph Theory

CMPSCI611: Approximating MAX-CUT Lecture 20

NP-complete? NP-hard? Some Foundations of Complexity. Prof. Sven Hartmann Clausthal University of Technology Department of Informatics

Mathematics on the Soccer Field

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

Session 6 Number Theory

Graph Theory Problems and Solutions

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

Guessing Game: NP-Complete?

This puzzle is based on the following anecdote concerning a Hungarian sociologist and his observations of circles of friends among children.

each college c i C has a capacity q i - the maximum number of students it will admit

Tutorial 8. NP-Complete Problems

ARTIFICIAL INTELLIGENCE (CSCU9YE) LECTURE 6: MACHINE LEARNING 2: UNSUPERVISED LEARNING (CLUSTERING)

Session 7 Bivariate Data and Analysis

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

An Introduction to The A* Algorithm

CAD Algorithms. P and NP

1.2. Successive Differences

Radicals - Multiply and Divide Radicals

Smart Graphics: Methoden 3 Suche, Constraints

Kenken For Teachers. Tom Davis June 27, Abstract

Pythagorean Theorem: Proof and Applications

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Cycle transversals in bounded degree graphs

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Revised Version of Chapter 23. We learned long ago how to solve linear congruences. ax c (mod m)

Introduction to Data Structures

B490 Mining the Big Data. 2 Clustering

JUST-IN-TIME SCHEDULING WITH PERIODIC TIME SLOTS. Received December May 12, 2003; revised February 5, 2004

6.099, Spring Semester, 2006 Assignment for Week 13 1

POLYNOMIAL FUNCTIONS

Graph Theory Origin and Seven Bridges of Königsberg -Rhishikesh

Computational complexity theory

CoNP and Function Problems

Data Structures in Java. Session 15 Instructor: Bert Huang

1 if 1 x 0 1 if 0 x 1

Some Minesweeper Configurations

Union-Find Algorithms. network connectivity quick find quick union improvements applications

Pigeonhole Principle Solutions

Algorithms and optimization for search engine marketing

Fast Sequential Summation Algorithms Using Augmented Data Structures

SECTION 10-2 Mathematical Induction

Reading 13 : Finite State Automata and Regular Expressions

MATH 13150: Freshman Seminar Unit 10

Factorizations: Searching for Factor Strings

CSE 4351/5351 Notes 7: Task Scheduling & Load Balancing

Primes in Sequences. Lee 1. By: Jae Young Lee. Project for MA 341 (Number Theory) Boston University Summer Term I 2009 Instructor: Kalin Kostadinov

Constraint Programming for Random Testing of a Trading System

Lecture 7: NP-Complete Problems

8.1 Min Degree Spanning Tree

Universality in the theory of algorithms and computer science

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 313]

Fixed Point Theorems

1. The RSA algorithm In this chapter, we ll learn how the RSA algorithm works.

5.1 Radical Notation and Rational Exponents

Applied Algorithm Design Lecture 5

The Classes P and NP

A Non-Linear Schema Theorem for Genetic Algorithms

ASSIGNMENT 4 PREDICTIVE MODELING AND GAINS CHARTS

Constrained curve and surface fitting

Determine If An Equation Represents a Function

COMBINATORIAL PROPERTIES OF THE HIGMAN-SIMS GRAPH. 1. Introduction

One last point: we started off this book by introducing another famously hard search problem:

2) Write in detail the issues in the design of code generator.

Complexity Theory. Jörg Kreiker. Summer term Chair for Theoretical Computer Science Prof. Esparza TU München

IB Math Research Problem

CHAPTER 3. Methods of Proofs. 1. Logical Arguments and Formal Proofs

G C.3 Construct the inscribed and circumscribed circles of a triangle, and prove properties of angles for a quadrilateral inscribed in a circle.

USING BACKTRACKING TO SOLVE THE SCRAMBLE SQUARES PUZZLE

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

Integer Factorization using the Quadratic Sieve

From Last Time: Remove (Delete) Operation

Turing Machines: An Introduction

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

4.2 Euclid s Classification of Pythagorean Triples

Representing Vector Fields Using Field Line Diagrams

LVQ Plug-In Algorithm for SQL Server

6.080/6.089 GITCS Feb 12, Lecture 3

A Strategy for Teaching Finite Element Analysis to Undergraduate Students

Research Paper Business Analytics. Applications for the Vehicle Routing Problem. Jelmer Blok

Transcription:

+ Knight Tour By: Marouf Baghdadi

Introduction Knight Tour is a mathematical puzzle that involves a single knight on a chess board. This knight is allowed to move according to the laws of chess. The knight s objective is to visit every square on the board. One of the first mathematicians to research the problem was Leonhard Euler. A variation of the puzzle is to find a closed tour which brings the knight in its final move to a neighboring square to the one the tour started from. Graph Theory Analogy The graph s nodes are the squares of the board. It is only possible for two nodes to be adjacent if there is a legal knight move between them. Finding a knight tour is equivalent to finding a Hamiltonian path in the graph representation and finding a closed tour is equivalent to finding a Hamiltonian cycle in the graph representation. In theory the problem of determining whether an arbitrary graph has a Hamiltonian path is NPcomplete. However, it is known* for which boards there exists a knight tour and several linear (in the number of squares) algorithms for solving the problem. * Schwenk's Theorem For any m n board with m less than or equal to n, a closed knight's tour is always possible unless one or more of these three conditions are met: 1. m and n are both odd; m and n are not both 1 2. m = 1, 2, or 4; m and n are not both 1 3. m = 3 and n = 4, 6, or 8. The proof for the theorem is not provided here but is accessible in various sources. Uninformed search algorithms Since the search Space in the knight tour problem is finite it makes uninformed search algorithms such as BFS or DFS complete, meaning that if there is a solution both algorithms are guaranteed to find it.

From every square on the chess board there are at most 8 possible knight moves, which brings the total number of possible combinations of knight moves (n 2 ) 8, which means that normal graph search algorithms such as BFS or DFS would have the complexity of O((n 2 ) 8 ), meaning that these algorithms are exponential and inefficient. As an example take a 5x5 board and run DFS on it the algorithm would expand 119771 nodes in order to come up with a solution (very bad). 25,1,15,6,23 16,5,24,9,14 11,2,7,22,19 4,17,2,13,8 1,12,3,18,21 DFS solution for a 5 x 5 board Informed search algorithms The algorithm of Choice in the case of informed searching would be best first choice for several reasons. The depth of a solution in the search graph is known beforehand ( x 2 ) which makes algorithms such as A* and IDA* having performances that aren t better than best first choice algorithm. Local search algorithms aren t necessary since there is no local maximum in the problem; the algorithm either finds a solution and reports it or continues searching until all possibilities are covered. The heuristics As it has been mentioned above the depth of the solution is known beforehand. That means the used heuristics would not be normal search heuristics, but rather be along the lines of CSP (constraint satisfaction problems) heuristics. Specifically, they would be variations of the most constrained node heuristic.

The first heuristic is to prefer squares discovered so far that have the least available next moves to unvisited squares, ties are broken arbitrarily. This heuristic is choosing the most constrained node so as to try to avoid the situation where the square becomes unreachable further in the search process. The heuristic has a slight advantage on the amount of expanded nodes during search. But on the other hand the heuristic is still inefficient for two reasons: the first reason appears when using the heuristic on bigger boards since the number of squares with similar number of available moves increases and the heuristic s arbitrary way of breaking ties is not helping, the second reason of its inefficiency is that even though the most constrained square is chosen the search can still hit dead ends since the heuristic does not guarantee not to hit dead ends but rather try to avoid them. 18 Heuristic performance 16 14 12 1 8 Heuristic performance 6 4 2 5x5 6x6 7x7 Results may vary due to arbitrary tie breaking. From the above diagram it is noticeable how inefficient this heuristic is on small boards where it expands over 1,5, nodes for a 7x7 board. However, this is still a better performance than DFS/BFS. An improvement over the previous heuristic would be is to add the ability for the search algorithm to detect boards that lead to dead ends before actually getting there. This can be achieved by scanning the board and checking if there is any unvisited square with no possible knight moves. If this is the case, then the board and its descendants are trimmed from the search tree which would obviously bring improvement in the running time of the search algorithm.

18 Heuristic with dead-end trimming 16 14 12 1 8 6 Heuristic with dead-end trimming 4 2 5x5 6x6 7x7 8x8 Results may vary due to arbitrary tie breaking. From the above chart we see a serious improvement in the number of expanded nodes over the previous heuristic (without dead-end trimming). For instance, running the search with this heuristic on a 7x7 board expands 129497 nodes while the previous one expanded over 1,5, nodes. However, this is still not good enough since we can obviously see that bigger boards have exponential number of expanded nodes. Warnsdorff s rule The rule was presented by the mathematician H.C Warnsdorff in the 19 th century. Like before, the rule s goal is to prevent creating dead ends. However, this time the rule prefers squares with the minimum number of allowed moves which are deepest so far in the search tree and ties are broken arbitrarily, which makes the algorithm behave more like DFS than BFS. This heuristic shows incredible improvement over the previous heuristic since it keeps developing a certain path while avoiding dead ends until a solution is found. For small and some

medium boards this gives a linear running time but the heuristic still runs into trouble when running on bigger boards because of its arbitrary ways of breaking ties. 7 Warnsdorff's rule 6 5 4 3 Warnsdorff's rule 2 1 5x5 6x6 7x7 8x8 9x9 1x1 11x11 12x12 13x13 14x14 24x24 Results may vary due to arbitrary tie breaking. The above graph shows the substantial improvement in performance that Warnsdorff s rule introduces. However, the rule fails on 25x25 boards and bigger and the results are again exponential. Furthest from center heuristic The main problem that Warnsdorff s rule runs into is its way of breaking ties since on 25x25 boards and bigger it is easy to split the board into two parts with no possible move from one part to the other. To counter this problem, the following heuristic should be applied to breaking ties that result from Warnsdorff s rule. Given two squares that are equally valued by Warnsdorff s rule, the one which is furthest (Euclidean) from the center is preferred. This way the algorithm will try to visit outer squares first then move on to the inner squares which prevents the creation of disjoint areas in the board.

Warnsdorff's rule with furthest from center heuristic 4 35 3 25 2 15 Warnsdorff's rule with furthest from center heuristic 1 5 5x5 1x1 15x15 2x2 25x25 4x4 5x5 6x6 The above graph shows how on big graphs the heuristic has a linear amount of expanded nodes which leads to a linear running time. Closed knight tour The closed knight tour is computationally harder than the open tour since it has a stricter condition on the finishing state which is a small subset of possible finishing states in the open tour. The used heuristic here is the furthest from center. It should be noted that for an odd board dimension there doesn t exist a closed knight tour**. ** www.combinatorics.org/volume_18/pdf/v18i1p8.pdf

3 Closed tour with heuristic 25 2 15 Closed tour with heuristic 1 5 6x6 8x8 1x1 12x12 14x14 18x18 2x2 4x4 5x5 Results may vary based on the starting location; closer to the center a fewer number of nodes are expanded. In this graph the starting position was at the center of the board. A variant of the heuristic (better in some cases) is to change the preference of moves; meaning that instead of only preferring moves farthest from center, The heuristic will first prefer moves that are have fewest possible neighbors which are the farthest from the starting point, and break ties by choosing the move which puts the knight furthest from the center. This way the searching can try to keep the squares closest to the starting position unvisited for as long as possible while avoiding to split the board into disconnected unvisited parts. The heuristic isn t perfect, seeing that on big boards if the starting position is too far from the center the algorithm fails to give reasonable performance. While the previous heuristic showed bad performance for squares that aren t at the center of the board, this heuristic gives good performance that are a little further from the center, and still on bigger boards if the starting point is too far from the center the search would require exponential running time/memory.

18 16 14 12 1 8 6 4 2 Warnsdorff with farthest from start then farthest from center 6x6 8x8 1x1 12x12 14x14 16x16 18x18 2x2 4x4 Warnsdorff with farthest from start then farthest from center The graph shows results are from staring points that have Euclidean distance of 1-3 from the center of the board. In conclusion, while generally the problem of finding a Hamiltonian path/cycle is NP-complete, it is possible to find an efficient searching algorithm for a specific subset of the problem similar to the one shown here, it might even be possible to use the heuristics shown in this project in the general case of a Hamiltonian path search. A second thing worth mentioning is that it should be noticed that the bigger the dimension of the board gets the more tie breaking becomes an issue and better heuristics should be devised for breaking ties. The program The program has two agents, one of them is the automated search agent while the other is an agent that actively receives input from the user to solve the board. To compile the program, either run the attached Makefile with the command make all or run the following command: javac *.java Running the program: the command for running the program java Main <parameters go here>

The first parameter should be an integer which determines the size of the board. The second and third parameters should be integers that tell the starting location on the board. The fourth parameter is a string that indicates which agent to use: h for human age or -a for the automated agent. In case of human agent, after the program has started running and asking for input, the user must format his input in the form x,y where x and y are integers that define the square the knight should move to. The fifth parameter is a string that indicates the desired search algorithm the automated algorithm should use. -bfs or -dfs makes the algorithm use BFS or DFS respectively. -bfc makes the use of best first choice algorithm, with no additional parameters, the used heuristic is the first introduced heuristic (the one that prefers minimum next squares so far). Optional additional parameters: -o / -c : Should indicate whether the desired tour is open or closed. The default is open. -t : Indicates that the algorithm should employ dead end trimming. -w : Indicates that Warnsdorff s rule should be applied. -r/-g : Should only be used if the -w is used. -r flag indicates that the furthest from center heuristic should be used. g flag indicates that furthest from start heuristic should be used.