A Faster Scrabble Move Generation Algorithm

13 downloads 90459 Views 220KB Size Report
tation is well-suited for these applications, it is not the most efficient representation for generating Scrabble moves. This is because, in Scrabble, a word is played ...
SOFTWARE—PRACTICE AND EXPERIENCE, VOL. 24(2), 219–232 (FEBRUARY 1994)

A Faster Scrabble Move Generation Algorithm steven a. gordon

Department of Mathematics, East Carolina University, Greenville, NC 27858, U.S.A. (email: magordonKecuvax.cis.ecu.edu)

SUMMARY Appel and Jacobson1 presented a fast algorithm for generating every possible move in a given position in the game of Scrabble using a DAWG, a finite automaton derived from the trie of a large lexicon. This paper presents a faster algorithm that uses a GADDAG, a finite automaton that avoids the non-deterministic prefix generation of the DAWG algorithm by encoding a bidirectional path starting from each letter of each word in the lexicon. For a typical lexicon, the GADDAG is nearly five times larger than the DAWG, but generates moves more than twice as fast. This time/space trade-off is justified not only by the decreasing cost of computer memory, but also by the extensive use of move-generation in the analysis of board positions used by Gordon 2 in the probabilistic search for the most appropriate play in a given position within realistic time constraints. key words: Finite automata Lexicons Backtracking Games

Artificial intelligence

INTRODUCTION Appel and Jacobson1 presented a fast algorithm for generating every possible move given a set of tiles and a position in Scrabble (in this paper Scrabble refers to the SCRABBLE  brand word game, a registered trade mark of Milton Bradley, a division of Hasbro, Inc.). Their algorithm was based on a large finite automaton derived from the trie3,4 of the entire lexicon. This large structure was called a directed acyclic word graph (DAWG). Structures equivalent to a DAWG have been used to represent large lexicons for spell-checking, dictionaries, and thesauri. 5–7 Although a left-to-right lexical representation is well-suited for these applications, it is not the most efficient representation for generating Scrabble moves. This is because, in Scrabble, a word is played by ‘hooking’ any of its letters onto the words already played on the board, not just the first letter. The algorithm presented here uses a structure similar to a DAWG, called a GADDAG, that encodes a bidirectional path starting from each letter in each word in the lexicon. The minimized GADDAG for a large American English lexicon is approximately five times larger than the minimized DAWG for the same lexicon, but the algorithm generates moves more than twice as fast on average. This faster CCC 0038–0644/94/020219–14  1994 by John Wiley & Sons, Ltd.

Received 29 March 1993 Revised 30 August 1993

220

a faster scrabble move generation algorithm

algorithm makes the construction of a program that plays Scrabble intelligently within realistic time constraints a more feasible project. Bidirectional string processing is not a novel concept. One notable example is the Boyer–Moore string searching algorithm.8–10 In addition to moving left or right, this algorithm also sometimes skips several positions in searching for a pattern string within a target string. The advantage of a faster algorithm The DAWG algorithm is extremely fast. There would be little use for a faster algorithm if the highest scoring move was always the ‘best’ one. Although a program that simply plays the highest scoring play will beat most people, it would not fare well against most tournament players. North American tournament Scrabble differs from the popular version in that games are always one-on-one, have a time limit of 25 minutes per side, and have a strict word challenge rule. When a play is challenged and is not in the official dictionary, OSPD2,11 the play is removed, and the challenger gets to play next. Otherwise, the play stands and the challenger loses his/her turn. The most apparent characteristic of tournament play is the use of obscure words (e.g. XU, QAT and JAROVIZE). However, the inability of a program which knows every word and always plays the highest scoring one to win even half of its games against expert players indicates that strategy must be a significant component of competitive play. Nevertheless, there would still be no need for a faster algorithm if expert strategy could be modeled effectively by easily computed heuristic functions. Modeling the strategy of Scrabble is made difficult by the presence of incomplete information. In particular, the opponent’s rack and the next tiles to be drawn are unknown, but the previous moves make some possibilities more likely than others. Gordon2 compares the effectiveness of weighted heuristics and simulation for evaluating potential moves. Heuristics that weigh the known factors in the proportions that perform most effectively over a large random sample of games give an effective, but unintelligent, strategy. Simulating candidate moves in a random sample of plausible scenarios leads to a strategy that responds more appropriately to individual situations. Faster move generation facilitates the simulation of more candidate moves in more scenarios within competitive time constraints. Furthermore, in end game positions, where the opponent’s rack can be deduced, faster move generation would make an exhaustive search for a winning line more feasible. NON-DETERMINISM IN THE FAST ALGORITHM Appel and Jacobson acknowledged that the major remaining source of inefficiency in their algorithm is the unconstrained generation of prefixes. Words can only be generated from left to right with a DAWG. Starting from each anchor square (a square on the board onto which a word could be hooked) the DAWG algorithm handles prefixes (letters before the anchor square) differently to suffixes (those on or after the anchor square). The DAWG algorithm builds every string shorter than a context-dependent length that can be composed from the given rack and is the prefix of at least one word in the lexicon. It then extends each such prefix into complete words as constrained by the board and the remaining tiles in the rack.

s. a. gordon

221

When each letter of a prefix is generated, the number of letters that will follow it is variable, so where it will fall on the board is unknown. The DAWG algorithm therefore only generates prefixes as long as the number of unconstrained squares left of an anchor square. Nevertheless, many prefixes are generated that have no chance of being completed, because the prefix cannot be completed with any of the remaining tiles in the rack, the prefix cannot be completed with the letter(s) on the board that the play must go through, or the only hookable letters were already consumed in building the prefix. They suggest eliminating this non-determinism with a ‘two-way’ DAWG. A literal interpretation of their proposal is consistent with their prediction that it would be a huge structure. The node for substring x could be merged with the node for substring y if and only if {(u,v) u uxv is a word} = {(u,v) u uyv is a word}, so minimization would be ineffective. A MORE DETERMINISTIC ALGORITHM A practical variation on a two-way DAWG would be the DAWG for the language L = {REV(x)ey u xy is a word and x is not empty}, where e is just a delimiter. This structure would be much smaller than a complete two-way DAWG and still avoid the non-deterministic generation of prefixes. Each word has as many representations as letters, so, before minimization, this structure would be approximately n times larger than an unminimized DAWG for the same lexicon, where n is the average length of a word. Each word in the lexicon can be generated starting from each letter in that word by placing tiles leftward upon the board starting at an anchor square while traversing the corresponding arcs in the structure until e is encountered, and then placing tiles rightward from square to the right of the anchor square while still traversing corresponding arcs until acceptance. A backtracking, depth-first search12 for every possible path through the GADDAG given the rack of tiles and board constraints generates every legal move. Being the reverse of the directed acyclic graph for prefixes followed by the directed acyclic graph for suffixes, it will be called a GADDAG. Reversing the prefixes allows them to be played just like suffixes, one tile at a time, moving away from anchor squares. The location of each tile in the prefix is known, so board constraints can be considered, eliminating unworkable prefixes as soon as possible. Requiring the prefix to be non-empty allows the first tile in the reverse of the prefix to be played directly on the anchor square. This immediately eliminates many otherwise feasible paths through the GADDAG. A DAGGAD, the DAWG for {yeREV(x) u xy is a word and y is not empty}, would work just as well—tiles would be played rightward starting at an anchor square and then leftward from the square left of the anchor square. The following conventions allow a compressed representation of a GADDAG, as well as partial minimization during construction: 1. If the y in REV(x)ey is empty, the e is omitted altogether. 2. A state specifies the arcs leaving it and their associated letters. 3. An arc specifies (a) its destination state (b) its letter set—the letters which, if encountered next, make a word.

222

a faster scrabble move generation algorithm

Figure 1. Subgraph of unminimized GADDAG for ‘CARE’ (see Table I for letter sets)

Placing letter sets on arcs avoids designating states as final or not. Figure 1 is the subgraph of an unminimized GADDAG that contains the representations of the word CARE. The letter sets on the arcs in Figure 1 can be found in Table I. CARE has four distinct paths, CeARE, ACeRE, RACeE, and ERAC, corresponding to hooking the C, A, R, and E, respectively, onto the board. The move generation algorithm Figure 2 illustrates the production of one play using each path for CARE through the GADDAG in Figure 1 on a board containing just the word ABLE. A play can Table I. Letter sets for Figures 1, 5, and 6. S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 S13 S14 S15 S16 S17 S18 S19 S20 S21 S22 S23 S24

= = = = = = = = = = = = = = = = = = = = = = = =

{D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D {D

u u u u u u u u u u u u u u u u u u u u u u u u

DC is a word} DA is a word} DR is a word} DE is a word} CD is a word} DCA is a word} DAR is a word} DRE is a word} CAD is a word} DCAR is a word} DARE is a word} CARD is a word} DN is a word} DEE is a word} DEN is a word} DREE is a word} DEEN is a word} DCARE is a word} DAREE is a word} DREEN is a word} CARED is a word} DCAREE is a word} DAREEN is a word} CAREED is a word}

= = = = = = = = = = = = = = = = = = = = = = = =

[. {A,B,D,F,H,K,L,M,N,P,T,Y}. {A,E,O}. {A,B,D,H,M,N,O,P,R,W,Y}. [. {O}. {B,C,E,F,G,J,L,M,O,P,T,V,W,Y}. {A,E,I,O}. {B,D,M,N,P,R,T,W,Y}. {S}. {B,C,D,F,H,M,P,R,T,W,Y}. {B,D,E,K,L,N,P,S,T}. {A,E,I,O,U}. {B,C,D,F,G,J,L,N,P,R,S,T,V,W,Z}. {B,D,F,H,K,M,P,S,T,W,Y}. {B,D,F,G,P,T}. {B,K,P,S,T,W}. {S}. [. {G,P}. {D,R,S,T,X}. [. {C}. {N,R}.

s. a. gordon

223

Figure 2. Four ways to play ‘CARE’ on ‘ABLE’

connect in front (above), in back (below), through, or in parallel with words already on the board, as long as every string formed is a word in the lexicon. Consider, for example, the steps (corresponding to the numbers in the upper left corners of the squares) involved in play (c) of Figure 2. CARE can be played perpendicularly below ABLE as follows: 1. 2. 3. 4. 5.

Play R (since ABLER is a word); move left; follow the arc for R. Play A; move left; follow the arc for A. Play C; move left; follow the arc for C. Go to the square right of the original starting point; follow the arc for e. Play the E, since it is in the last arc’s letter set.

The GADDAG algorithm for generating every possible move with a given rack from a given anchor square is presented in Figure 3 in the form of backtracking, recursive co-routines. Gen(0,NULL,RACK,INIT) is called, where INIT is an arc to the initial state of the GADDAG with a null letter set. The Gen procedure is independent of direction. It plays a letter only if it is allowed on the square, whether letters are being played leftward or rightward. In the GoOn procedure, the direction determines which side of the current word to concatenate the current letter to, and can be shifted just once, from leftward to rightward, when the e is encountered. A GADDAG also allows a reduction in the number of anchor squares used. There is no need to generate plays from every other internal anchor square of a sequence of contiguous anchor squares (e.g. the square left or right of the B in Figure 2), since every play from a given anchor square would be generated from the adjacent anchor square either to the right (above) or to the left (below). In order to avoid generating the same move twice, the GADDAG algorithm was implemented with a parameter to prevent leftward movement to the previously used anchor square. The GADDAG algorithm is still non-deterministic in that it runs into many deadends. Nevertheless, it requires fewer anchor squares, hits fewer dead-ends, and follows fewer arcs before detecting dead-ends than the DAWG algorithm.

224

a faster scrabble move generation algorithm

Figure 3. The GADDAG move generation algorithm

Computing cross sets Appel and Jacobson’s DAWG implementation uses and maintains a structure for keeping track of which squares are potential anchor squares (horizontally and/or vertically), and for each such anchor square, the set of letters that can form valid crosswords (cross sets). Whenever a play is made, only the squares directly affected by the play need to be updated. The GADDAG implementation uses and maintains the same structure. Computing a right cross set (i.e. the set of letters for the square to the right of a word or single letter) is easy with a DAWG—start in the initial state and follow the arcs associated with the letters of the word. Computing the left cross set of a word is equivalent to generating the set of one-letter prefixes, and thus exhibits the same non-determinism as prefix generation. For each letter of the alphabet, one must follow the arc for that letter from the initial state of the DAWG, and then follow the arcs associated with each letter of the word to see if they lead to acceptance. A GADDAG supports the deterministic and simultaneous computation of left and right cross sets. Just start in the initial state and follow arcs for each letter in the word (reading from right to left). The left cross set is the letter set on the last arc and the right cross set is the letter set on the e arc from the state that the last arc led to. There is one rare case where the computation of a cross set is not deterministic. When a square is left of one word and right of another, then one must follow one word through the GADDAG, and then for each letter of the alphabet, follow that letter and then the letters in the other word to see if they lead to acceptance. For example, if PA and ABLE were separated by just one square, this computation would allow a word to be played perpendicular to them if it placed an R or a Y between them.

s. a. gordon

225

Partial and full minimization For all strings x, y, and z, REV(x)eyz is a path through the GADDAG if and only if xyz is a word. So, if xy =vw, then {z u REV (x)eyz is a path} = {z u REV(v)ewz is a path}. Standard minimization13 of the GADDAG as a finite automaton would therefore merge the node that REV(x)ey leads to with the node that REV(v)ew leads to. For example, in the instance of CARE, the node that CeA leads to would be merged with the node that ACe leads to, and the nodes that CeAR, ACeR, and RACe each lead to would also be merged into a single node. The algorithm given in Figure 4 merges all such states during the initial construction of the GADDAG. The resulting automaton is still not fully minimized, but the comparatively slow, standard minimization process receives a much smaller automaton to finish minimizing. Figure 5 is the subgraph of the semi-minimized GADDAG produced by this algorithm that contains the representation of the word CARE. Figure 6 is the subgraph containing the representation of the word CAREEN. (Table I lists the letter sets for Figures 5 and 6). The longer the word and the more duplicate letters, the more states this algorithm eliminates. Replacing final states with letter sets on arcs eliminates an explicit arc and state for the last letter in each path of each word. Letter sets also allow many states to be merged in minimization that otherwise would not be. For example, both WOUND and ZAGG can only be followed by the multi-letter strings ED and ING. Even though WOUND is a word and ZAGG is not, the node that WeOUND, OWeUND, UOWeND, NUOWeD, and DNUOWe all lead to can be merged with the node

Figure 4. The GADDAG construction algorithm

226

a faster scrabble move generation algorithm

Figure 5. Subgraph of semi-minimized GADDAG for ‘CARE’ (see Table I for letter sets)

Figure 6. Subgraph of semi-minimized GADDAG for ‘CAREEN’ (see Table I for letter sets)

that ZeAGG, AZeGG, GAZeG, and GGAZe all lead to. Each arc leading to the former node has the letter set {S}, whereas each arc leading to the latter node has a null letter set. After merging, those arcs will all lead to the same node, but their letter sets will remain distinct. Incidentally, the node that DNUOW leads to cannot be merged with the node that GGAZ lead to, since these strings can be completed by different strings (e.g. the path DNUOWER for REWOUND and the path GGAZGIZeED for ZIGZAGGED). The e precludes this. Compression A GADDAG (or DAWG) could be represented in a various expanded or compressed forms. The simplest expanded form is a 2-dimensional array of arcs indexed

s. a. gordon

227

by state and letter. In the current lexicon, the number of distinct letter sets, 2575, and distinct states, 89,031, are less than 212 and 217, respectively. So, each arc can encode the indices of a letter set and a destination state within a 32-bit word. The array of letter sets takes just over 10 kilobytes. Non-existent arcs or states are just encoded with a 0. The simplest compressed representation is a single array of 32-bit words. States are a bit map indicating which letters have arcs. Each arc is encoded in 32-bit words as in the expanded representation. In this combined array, arcs directly follow the states they originate from in alphabetical order. Compression has two disadvantages. The first is that encoding destination states in arcs limits the potential size of the lexicon. Under expanded representation, a state is just the first index of a two-dimensional array, whereas, under compression, a state is the index of its bit-map in a single array of both arcs and states. This second index grows much faster, so that a larger lexicon would allow arcs to be encoded in 32 bits under expanded representation than under compression. The second disadvantage is the time it takes to find arcs. Each arc is addressed directly under expanded representation. Under compression, the bit map in a state indicates if a given letter has an associated arc. One must count how many of the preceding bits are set and then skip that many 32-bit words to actually find the arc. Although the actual number of preceding bits that are set is usually small, each preceding bit must be examined to compute the correct offset of the associated arc. The advantage of compression is, of course, a saving in space. The histogram of states by number of arcs in Table II indicates that most states have only a few arcs. Avoiding the explicit storage of non-existent arcs thus saves a great amount of space. A compressed GADDAG requires S + A 32-bit words, compared to 27S for an expanded GADDAG, where S is the number of states and A is the number of arcs. Nevertheless, the automata in Reference 7 had fewer states with more than 10 arcs, since a lexicon of commonly-used words is less dense than a Scrabble lexicon. Table III compares the sizes of DAWG and GADDAG structures built from a lexicon of 74,988 words from the OSPD211. Word lengths vary from 2 to 8 letters, averaging 6·7 letters per word. Since people rarely play words longer than 8 letters, the OSPD2 only lists base words from 2 to 8 letters in length and their forms. The longer words that are listed are not representative, having, for example, a disproportionate number of ING endings. Comparing DAWG and GADDAG structures on a lexicon with just these longer words could be misleading, so longer words were omitted. The minimized and compressed DAWG structure represents the lexicon with an impressive 4·3 bits per character, less than half the number of bits required to represent the lexicon as text (including one byte per word for a delimiter). Appel and Jacobson achieved an even better ratio by encoding many arcs in 24 bits. The GADDAG minimized more effectively than the DAWG, being just less than 5 times larger rather than the 6·7 times larger expected with equally effective minimization. PERFORMANCE Table IV compares the performances of the DAWG and GADDAG move generation algorithms implemented within identical shells in Pascal playing 1000 randomly generated games on a VAX4300 using both compressed and expanded representations.

228

a faster scrabble move generation algorithm Table II. Histogram of states by number of outgoing arcs in minimized DAWG and GADDAG structures Number of arcs per state

DAWG States %

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

0 5708 5510 2904 1385 775 445 299 189 148 98 84 53 52 41 30 20 23 16 14 10 18 12 8 6 4 4 –

0·0 32·0 30·9 16·3 7·8 4·3 2·5 1·7 1·1 0·8 0·5 0·5 0·3 0·3 0·2 0·2 0·1 0·1 0·1 0·1 0·1 0·1 0·1 0·0 0·0 0·0 0·0 –

GADDAG States % 0 35,103 24,350 11,291 5927 3654 2187 1435 1009 821 641 493 389 325 254 198 165 150 128 102 81 87 65 59 52 26 34 5

0·0 39·4 27·4 12·7 6·7 4·1 2·5 1·6 1·1 0·9 0·7 0·6 0·4 0·4 0·3 0·2 0·2 0·2 0·1 0·1 0·1 0·1 0·1 0·1 0·1 0·0 0·0 0·0

Table III. Relative sizes of DAWG and GADDAG structures DAWG Unminimized Minimized States Arcs Letter sets Expanded Bytes Bits/char Compressed Bytes Bits/char

GADDAG Semi-minimized Minimized

Ratio (G/D) minimized)

55,503 91,901 908

17,856 49,341 908

250,924 413,887 2575

89,031 244,117 2575

4·99 4·95 2·84

5,775,944 91·6

1,860,656 29·5

27,110,092 429·8

9,625,648 152·6

5·17

593,248 9·4

272,420 4·3

2,669,544 42·3

1,342,892 21·3

4·93

229

s. a. gordon

Table IV. Relative performance of DAWG and GADDAG algorithms playing both sides of 1000 random games on a VAX4300

CPU time Expanded Compressed Page faults Expanded Compressed Arcs Traversed Per sec (compressed) Anchors used Number of moves Average score

DAWG overall per move

GADDAG overall per move

9:32:44 8:11:47

3:38:59 3:26:51

6063 1011 668,214,539 22,646 3,222,746 25,569 389·58

1·344s 1·154s

26,134 126·04

32,305 3120 265,070,715 21,372 1,946,163 25,363 388·75

Ratio (D/G)

0·518s 0·489s

2·60 2·36

10,451

0·19 0·32 2·50

76·73

1·64

The VAX had 64M of memory, a light, mostly interactive work load, and effectively unlimited image sizes, so performance was not significantly affected by either memory management or competing work load. The DAWG algorithm traversed 2·5 times as many arcs in its structure as the GADDAG algorithm did in its. CPU times reflect a slightly smaller ratio. In other words, both algorithms traverse about 22,000 arcs/s, but the GADDAG algorithm traverses the same number of arcs to generate five moves as the DAWG algorithm traverses to generate two moves. The shell used the greedy evaluation function (i.e. play the highest scoring move). Ties for high score were broken by using the move found first. Since ties occurred frequently and the algorithms do not generate moves in exactly the same order, the actual games played diverged quickly. Each expanded implementation ran slightly slower than the respective compressed implementation. The page faults due to the larger memory demands of the expanded structures evidently take more time to process on the VAX than searching for arcs in the compressed structures. On a dedicated machine with enough memory, the expanded implementations might run faster. Some additional speed-up could be expected from reordering bit maps and arcs into letter frequency order (i.e. e, E, A, I, $) to reduce the average number of bits preceding a letter.4 In practice, reordering had little effect on CPU times. This may be because A and E are already near the beginning and the GADDAG implementation already placed e before all the letters, so most of the advantage of reordering had already been achieved. Performance with blanks Appel and Jacobson note that their program hesitates noticeably when it has a blank. A blank can stand for any letter, so many more words can usually be made from a rack with a blank that a rack without one. Table V presents a logarithmic histogram of the number of arcs traversed per move with and without a blank for both algorithms. Most plays are generated faster than average, but some plays take much longer

230

a faster scrabble move generation algorithm

Table V. Logarithmic histogram of arcs traversed per move with and without blanks in 1000 random games Arc range

With blank

DAWG Without

Total

With blank

GADDAG Without

Total

0 0 0 0 0 7 29 107 201 569 774 349 23 13 2

24 896 877 1075 1809 4654 8348 5209 602 1 0 0 0 0 0

24 896 877 1075 1809 4661 8357 5316 803 570 774 349 23 13 2

0 0 0 0 8 45 126 353 734 559 183 18 7 2 0

45 847 1618 2680 6260 7935 3605 337 1 0 0 0 0 0 0

45 847 1618 2680 6268 7980 3731 690 735 559 183 18 7 2 0

23,495 11,961

2074 179,850

25,569 26,134

23,328 5467

2035 67,586

25,363 10,451

0–255 256–511 512–1K 1K–2K 2K–4K 4K–8K 8K–16K 16K–32K 32K–64K 64K–128K 128K–256K 256K–512K 512K–1M 1M–2M 2M–4M Total Average arcs

than average. The worst case requires about 1 minute for the GADDAG algorithm versus about 2 minutes for the DAWG algorithm (the frequency of this case, twice in both sides of 1000 games, suggests these racks contain both blanks). As measured by arcs traversed, the GADDAG algorithm is 2·19 times faster on racks without blanks, whereas it is 2·66 times faster on racks with blanks. The GADDAG algorithm still hesitates when it encounters blanks, but a little less. Performance under heuristics The performance advantage of GADDAG algorithm on racks with blanks suggests that the better the rack, the greater the advantage. Gordon2 presents three heuristics that improve the average quality of racks by considering the utility of the tiles left on the rack when choosing a move. Rack Heuristic1, from Reference 14, estimates the utility of each letter in the alphabet. Rack Heuristic2 has an additional factor to discourage keeping duplicate letters. Rack Heuristic3 includes another factor to encourage a balance between vowels and consonants. Table VI shows that when Table VI. Relative performance of DAWG and GADDAG algorithms aided by rack evaluation heuristics

Greedy vs. Greedy RackH1 vs. Greedy RackH2 vs. Greedy RackH3 vs. Greedy

DAWG Secs/ Arcs/ Page move move faults

GADDAG Secs/ Arcs/ Page move move faults

Ratio (D/G) Secs/ Arcs/ Page move move faults

1·154 1·390 1·448 1·507

0·489 0·606 0·630 0·655

2·36 2·29 2·30 2·30

26,134 32,213 33,573 35,281

1011 1013 1016 1017

10,451 12,612 13,060 13,783

3120 3136 3139 3141

2·50 2·55 2·57 2·56

0·32 0·32 0·32 0·32

s. a. gordon

231

measured by arcs traversed per move, the performance advantage of GADDAG algorithm over the DAWG algorithm increases slightly under these heuristics. However, the ratio of seconds per move actually decreases slightly. Each heuristic is computed for each move found by the move generation algorithms. Heuristic processing time is therefore a function of the number of moves generated rather than the number of arcs traversed generating those moves. Heuristic processing time per move is bounded by the increase in average CPU times for the GADDAG algorithm. The fact that average CPU times increased by twice as much for the DAWG algorithm suggests that at least half of this increase was due to poorer performance generating moves from the better racks that resulted from using the heuristics. APPLICATIONS AND GENERALIZATIONS Most text processing applications outside of Scrabble and crossword puzzles are well-suited to left-to-right processing, and would not seem to benefit from the GADDAG structure. However, the algorithm could be applied outside of text processing. Ignoring the textual details, the GADDAG algorithm fits objects into an environment. The environment must be represented by a grid and the lexicon of potential objects must be described by strings in a finite alphabet. The constraints for ‘hooking’ one object onto another must be expressed in terms of symbols being on the same or adjacent squares. Objects could be represented in two or more dimensions by encoding strings with multiple delimiters. Consider the following oversimplified, biochemical application along the lines of algorithms than can be found in Reference 15. The GADDAG structure could encode a lexicon of molecules from the point of view of each atom in each molecule that can be ‘hooked’ onto atoms in other molecules. Then, given an environment consisting of already existing molecules and their locations, the GADDAG algorithm would find every location that a molecule, composed from a given collection of atoms, can be ‘hooked’ onto another molecule in the environment without conflicting with any of the other molecules. FUTURE WORK The lexicon should be expanded to include all nine-letter words, and the effect on the relative size of the data structures and the performance of the algorithms should be remeasured. Even longer words, up to 15 letters, could also be added. Research into improving the modeling of Scrabble strategy continues on three fronts: weighted heuristics for the evaluation of possible moves, the use of simulation to select the most appropriate candidate move in a given position, and exhaustive search for the optimal move in end games. CONCLUSION Although the Appel and Jacobson algorithm generates every possible move in any given Scrabble position with any given rack very quickly using a deterministic finite automaton, the algorithm itself is not deterministic. The algorithm presented here

232

a faster scrabble move generation algorithm

achieves greater determinism by encoding bidirectional paths for each word starting at each letter. The resulting program generates moves more than twice as fast, but takes up five times as much memory for a typical lexicon. In spite of the memory usage, a faster algorithm makes the construction of a program that plays intelligently within competitive time constraints a more feasible project. REFERENCES 1. A. Appel and G. Jacobson, ‘The world’s fastest Scrabble program’, Commun. ACM, 31,(5), 572–578, 585 (1988). 2. S. Gordon, ‘A comparison between probabilistic search and weighted heuristics in a game with incomplete information’, Technical Report, Department of Mathematics, East Carolina University, August 1993. Also, to appear in AAAI Fall Symposium Series, Raliegh, NC (October 1993). 3. E. Fredkin, ‘Trie memory’, Commun. ACM, 3,(9), 490–500 (1960). 4. D. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, Addison-Wesley, Reading, MA, 1973, pp.481–500, 681. 5. M. Dunlavey, ‘On spelling correction and beyond’, Commun. ACM, 24(9), 608 (1981). 6. K. Kukich, ‘Techniques for automatically correcting words in text’, ACM Comput. Surv., 24(4), 382– 383, 395 (1992). 7. C. Lucchesi and T. Kowaltowski, ‘Applications of finite automata representing large vocabularies’, Software—Practice and Experience, 23, 15–30 (1993). 8. A. Apostolico and R. Giancarlo, ‘The Boyer–Moore–Galil string searching strategies revisited’, SIAM J. Comput., 15,(1), 98–105 (1986). 9. S. Baase, Computer Algorithms: Introduction to Design and Analysis, 2nd edn, Addison-Wesley, Reading, MA, 1988, pp.209–230. 10. R. Boyer and J. Moore, ‘A fast string searching algorithm’, Commun. ACM, 20,(10), 762–772 (1977). 11. Milton Bradley Company, The Official Scrabble Players Dictionary, 2nd edn, Merriam-Webster Inc., Springfield,MA, 1990. 12. R.Tarjan, ‘Depth-first search and linear graph algorithms’, SIAM J. Comput., 1,(2), 146–160 (1972). 13. A. Nerode, ‘Linear automaton transformations’, Proc. AMS, 9, 541–544 (1958). 14. N. Ballard, ‘Renaissance of Scrabble theory 2’, Games Medleys, 17, 4–7 (1992). 15. D. Sankoff and J. B. Kruskal, Time Warps, String Edits, and Macromolecules: The Theory and Practice of Sequence Comparison, Addison-Wesley, Reading, MA, 1983.