TMP - BKMARKO, 8, A, B, C, D) With Array Dimensions: Problem-Solv Inr - 2:gasching 457

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

tmp <- BKMARKO, 8, A, B, C, D) with array dimensions

A GENERAL BACKTRACK ALGORITHM THAT


D[1:NVARS] and C[1:NVARS, l : k ] , where k is the maximum
ELIMINATES MOST REDUNDANT TESTS
of t h e B [ i ] values (-8 for 8-queens). Initial values of A are
i r r e l e v a n t ; C and D values are initially 1. BKMARK returns
John Gaschnig
1, w i t h s o l u t i o n in ASSIGN, or returns 0 if no solution
Dept. of Computer Science
exists. Define PAIRTEST for 8-queens and trace the
C a r n e g i e - M e l l o n University
e x e c u t i o n (new vs. old versions) to see how it w o r k s .
P i t t s b u r g h , Pa. 15213
( S u g g e s t i o n : d e f i n e an array VALUES w i t h same dimensions
as MARK, so that an element of VALUES encodes a board
We d e f i n e a faster algorithm functionally equivalent
location.) For b r e v i t y , the symbol st.ands for "comment".
to the classical backtrack algorithm for assignment
p r o b l e m s , of w h i c h the Eight Queens puzzle is an
e l e m e n t a r y example (Fillmore & Williamson 1974, Knuth
1975]. E x p e r i m e n t a l measurements (figure 1) reveal
r e d u c t i o n by a factor of 2.5 for the 8-queens puzzle
( f a c t o r of 8.7 for 16 queens) in T, the number of pair-tests
p e r f o r m e d b e f o r e finding a solution (i.e., first solution). A
p a i r - t e s t in this case determines whether a queen on
s q u a r e ( i j , j j ) attacks a queen on square (12, j2^ i n C P U "
s e c o n d s , net s p e e d u p is by a factor of 2.0 and 6.0 for 8-
a n d 1 6 - q u e e n s , r e s p e c t i v e l y . 16-queens was solved in 0.14
s e c o n d s on a PDP K L / 1 0 . The speedup can be a t t r i b u t e d
to t h e e l i m i n a t i o n of almost all redundant tests otherwise
r e c o m p u t e d in many p a r t s of the search tree, as indicated
in f i g u r e 2, w h i c h shows the mean number of times, D, an
a r b i t r a r y p a i r - t e s t is executed. If D = 1 then all tests are
d i s t i n c t (no recomputatiOn). Note that each data point in
t h e f i g u r e s r e p r e s e n t s the mean over 30 or 70 problem
i n s t a n c e s that d i f f e r as follows: instead of instantiating
q u e e n 3, say, on square (3,1), then on (3,2),..., then (3,8),
t h e s e 8 s q u a r e s are o r d e r e d randomly. A problem instance
is d e f i n e d by choosing a "legal squares o r d e r i n g " for each
q u e e n . Random o r d e r i n g generally gives a smaller value of
T, on t h e a v e r a g e , than the " n a t u r a l " 1,2,3,...,N ordering (for
2 0 - q u e e n s , a factor of 5 00 smaller!). REFERENCES

The a l g o r i t h m exploits an advantageous time-space


1. F i l l m o r e , J., and S. Williamson, "On Backtracking: A
t r a d e o f f and is d e f i n e d below in general form by recursive
C o m b i n a t o r i a l Description of the Algorithm", S1AM J.
SAIL p r o c e d u r e BKMARK [Swinehart & Sproull 1971]. The
C o m p u t i n g (3), No. 1 (March 1974), pp. 4 1 - 5 5 .
classical b a c k t r a c k algorithm is defined the same, minus the
2. K n u t h , D., "Estimating the Efficiency of Backtrack
u n d e r l i n e d p o r t i o n s (except that "NEW[VAR]" in line 7 is
P r o g r a m s " , Mathematics of Computation (29), No. 129
r e p l a c e d by " 1 " ) . The algorithm applies to any problem
(Jan. 1975), pp. 1 2 1 - 1 3 6 .
h a v i n g NVARS v a r i a b l e s (8, for 8-queens), each variable Xj
3. S w i n e h a r t , D., and B. Sproull, SAIL, Stanford AI Project
h a v i n g NVALSH] a p r i o r i possible values (8 squares for
O p e r a t i n g Note No. 57.2, January 1 9 7 1 .
e a c h q u e e n (- one r o w of board), except 4 for queen 1 for
symmetry reasons). An assignment vector
This r e s e a r c h was s u p p o r t e d by the Defense Advanced
ASSIGN[1:NVARS] of values to variables is a solution iff
R e s e a r c h P r o j e c t s Agency under Contract no. F 4 4 6 2 0 - 7 3 -
PAIRTEST0, ASSIGN[i], j, ASSIGN[j]) is true for all
C - 0 0 7 4 and m o n i t o r e d by the Air Force Office of Scientific
0 < i < j < NVARS (iff no queen can take any other queen).
Research.
B e l o w , ASSIGN contains indices to the actual values. Top
level invocation for 8-queens takes the form

P r o b l e m - S o l v i n r ; - 2 : Gasching
457

You might also like