How good is the Warnsdorffs knights tour heuristic?

Warnsdorffs rule for a knights tour is a heuristic, i.e., it is a rule that does not produce the desired result all the time. It is a classic example of a greedy method in that it is based on a series of locally optimal choices. This note describes a…

Authors: Samuel L. Marateck

Ho w go o d is the W arnsdorff ’s knight’ s tour heuristic? Sam uel L. Maratec k Departmen t of Comp uter Science, New Y ork Un iv ersit y , New Y ork, N.Y.10012, USA W a rn sdorff ’s rule for a knigh t’s tour is a heur istic, i.e., it’s a rule that do es not pro du ce the desired result all the time. It is a cla ssic example of a greedy metho d in that it is based on a series of lo cally optimal c hoices. T his note describ es an analysis that determines h ow go o d the heu r istic is on a n 8 X 8 c hessb oard. The ord er of app earance in a p ermutat ion of th e eigh t p ossib le mo ves a knight can m ak e determines the path the kn ight tak es. A computer analysis is done of the 8! p ermutati ons of the order of a knigh t’s mo v es in W arnsdorff ’s rule on an 8 X 8 c hessb oard for tours starting on eac h of the 64 s q u ares. Whenever a tie o ccurs for mo ve s to v ertices that hav e the low est degree, the first of these v ertices en countered in the programming lo op is c hosen. Th e num b er of p ermutati ons of the 8! total that yield n on-Hamiltonian paths is tallied. This will b e the same v alue if we consisten tly c ho ose the last of these v ertices encoun tered. 1. In tro duction The Knight’s T our is an example of a Hamiltonian path on a graph in that the knight touc hes eac h squ are once and only once on the c hessb oard. A path in wh ich the starting square is reac hable from the last square the kn igh t visits is called c lose d or r e-entr ant or a Hamiltonian cycle . In 1823 W arnsdorff [5] published a r ule to p ro duce a Hamiltonian path: At e ach squar e a lo ok-ahe ad is p erforme d to se e which of the p ossible next squar es has the le ast numb er of valid exits. The squar e having the le ast numb er of valid exits – in graph th eory this translate to the vertex with the lo west degree – is the next squ ar e the knight lands on. If ther e is a tie b etwe en two or mor e squar es, the next squar e is arbitr arily chosen. The lo ok-ahead for th e p enultimat e square (the 63rd one) in a Hamiltonian path s h ould indicate the last square (the 64th one) has degree zero, i.e., there is no square to wh ic h the knight on the ultimate square can mov e. 2. Examples of Knigh t’s tours The knigh t’s mo v es are d escrib ed b y the graph G = (V, E) , where the squares on the b oard are the ve rtices V and the allo we d mov es of the knight are th e edges E , i.e., V = { ( i, j ) | 1 ≤ i, j ≤ 8 } and E = { (( i, j ) , ( k , l )) |{| i − k | , | j − l |} = { 1 , 2 }} . See for instance [3] [1] [2] [4]. W e describ e the c hange in th e kn igh t’s x and y p ositions by the p air h dx, dy i . If w e use the the follo win g or- der for the knight ’s eigh t p ossible mo ve s, h 1 , 2 ih 2 , 1 ih 1 , − 2 ih 2 , − 1 ih− 1 , 2 ih− 2 , 1 ih− 1 , − 2 ih− 2 , − 1 i to determine whic h square the knigh t pro cedes to next, if he starts at (0,0) 1 the path is des- crib ed in Fig. 1. The path is, incident ally , a closed path. 1 W e use the conv ention th at the numbering of the rows and columns of a matrix starts with zero. 1 1 4 61 20 41 6 43 22 34 19 2 5 60 21 40 7 3 64 35 62 37 42 23 44 18 33 48 57 46 59 8 39 49 14 63 36 55 38 45 24 32 17 56 47 58 27 54 9 13 50 15 30 11 52 25 28 16 31 12 51 26 29 10 53 Fig. 1. The Hamiltonian path starts at (0,0). The tour in W arnsdorff ’s algorithm stops wh en a square w ith zero exits is reac hed. If that square o ccurs b efore the knigh t reac hes the 63rd s quare, as it do es in Fig. 2. at the squ are at (5,0) the tour halts and the path descib ed is non-Hamiltonian. Th is tour uses the same h dx, dy i p ermutat ion as in Fig. 1. Rememb er if a few squares are left and one of them ha ving degree zero is o ccup ied by the knight, the other squares – they are mark ed in Fig. 2. by a zero – cannot reac h th e one last o ccupied. Wh y? Since a zero-degree sq u are cannot acess any other squ ares, no squ are can access it. 0 2 19 24 35 28 17 26 20 23 0 1 18 25 34 29 3 0 21 36 45 32 27 16 22 55 46 0 42 37 30 33 47 4 59 54 31 44 15 38 60 53 56 43 50 41 12 9 5 48 51 58 7 10 39 14 52 57 6 49 40 13 8 11 Fig. 2. A non-Hamiltonian path for which the program uses the s ame h dx, dy i p erm utation as is used in Fig. 1. b ut starts at square (1,3). 3. Analysis of the 8! p erm utations of the knight’s mo v es F o r eac h of the 8! or 40,32 0 p ermutatio ns of the kn ight’s mo v es, a program determines if a tour starting from any of the 64 squ ares pro du ces a non-Hamiltonian path. The result is that 32,944 p ermutations out of 40,320 pro d uce at least one non-Hamiltonian path. S o you only ha v e an 18% chance of rand omly c ho osing a p ermutat ion that w ill yield a b oard without any non-Hamiltonian paths. The most non-Hamiltonian paths resu lting from a p ermutat ion is 9. The p erm utation <1,2> <1,-2> <-2,-1 > <2,-1> <-2,1> <-1,-2><-1,2> <2,1> is one of th ose that pro du ces 9 non-Hamiltonian paths. On the other hand , <1,2> <2,1> <1,-2> <-1,2> <-2,-1> <2,-1> <-1,-2> <-2,1> 2 is one of th ose that pro du ces 64 Hamiltonian paths and thus no non-Hamiltonian p aths. In ca se of a tie o ccur ring for mo ve s to the t w o or more ve rtices ha ving th e same lo west degree, the program c ho oses the first vertex encountered. The same result for the sum of the non-Hamiltonian paths wo uld o ccur if you c ho ose the last verte x encounte red. Th e reason is that summing o v er all the p ossible p ermutat ions makes th e t w o n on -Hamiltonian su ms – one obtained us in g the fir st v ertex encount ered in a tie, and the other usin g the last v ertex encoun tered – ident ical b ecause of the sym metry of the p erm utations: F or eve ry p ermutat ion of th e form ab cdefgh there is a corresp onding one of the form hgfedcba. In an analysis of the tours themselv es, of the 64 x 40,320 or 2,580, 480 p ossib le tours, 78,832 are n on-Hamiltonian. Again, th e results are indep endent of w hether the fi rst or last ve rtex is c hosen in a tie. References [1] A xel Conrad, T anja Hind richs, Hussein Morsy , Ingo W egener, Solution of th e Knight’s Hamiltonian path pro- blem on c hessb oards, Discrete Applied Mathematics 50 ( 1994) 125-134. [2] Olaf K yek, Ian Parberry , I ngo W egener, Bound s on the number of k night’s tours, Discrete Applied Mathematics 74 (1997) 171-181. [3] Ian Parberry , An efficient algorithm for t he Knight’s tour problem, Discrete Ap plied Mathematics 73 (1997) 250-260. [4] Ira Pohl, A Method for Find in g H amiltonian Paths an d Knight’s T ours, Communicatio ns of the ACM 10 (1967) 446-449. [5] H . C. W arnsdorff, Des R ¨ o sselsprunges einfachste und allgemeinste L ¨ o sung. Schmalk alden, (1823). 3

Original Paper

Loading high-quality paper...

Comments & Academic Discussion

Loading comments...

Leave a Comment