23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

To solve such a puzzle, we need to assign a unique digit (that is, 0,1,…, 9) to each<br />

letter <strong>in</strong> the equation, <strong>in</strong> order to make the equation true. Typically, we solve such a<br />

puzzle by us<strong>in</strong>g our human observations of the particular puzzle we are try<strong>in</strong>g to<br />

solve to elim<strong>in</strong>ate configurations (that is, possible partial assignments of digits to<br />

letters) until we can work though the feasible configurations left, test<strong>in</strong>g for the<br />

correctness of each one.<br />

If the number of possible configurations is not too large, however, we can use a<br />

computer to simply enumerate all the possibilities <strong>and</strong> test each one, without<br />

employ<strong>in</strong>g any human observations. In addition, such an algorithm can use multiple<br />

recursion to work through the configurations <strong>in</strong> a systematic way. We show<br />

pseudocode for such an algorithm <strong>in</strong> Code Fragment 3.37. To keep the description<br />

general enough to be used with other puzzles, the algorithm enumerates <strong>and</strong> tests all<br />

k-length sequences without repetitions of the elements of a given set U. We build<br />

the sequences of k elements by the follow<strong>in</strong>g steps:<br />

1. Recursively generat<strong>in</strong>g the sequences of k − 1 elements<br />

2. Append<strong>in</strong>g to each such sequence an element not already conta<strong>in</strong>ed <strong>in</strong> it.<br />

Throughout the execution of the algorithm, we use the set U to keep track of the<br />

elements not conta<strong>in</strong>ed <strong>in</strong> the current sequence, so that an element e has not been<br />

used yet if <strong>and</strong> only if e is <strong>in</strong> U.<br />

Another way to look at the algorithm of Code Fragment 3.37 is that it enumerates<br />

every possible size-k ordered subset of U, <strong>and</strong> tests each subset for be<strong>in</strong>g a possible<br />

solution to our puzzle.<br />

For summation puzzles, U = {0,1,2,3,4,5,6,7,8,9} <strong>and</strong> each position <strong>in</strong> the sequence<br />

corresponds to a given letter. For example, the first position could st<strong>and</strong> for b, the<br />

second for o, the third for y, <strong>and</strong> so on.<br />

Code Fragment 3.37: Solv<strong>in</strong>g a comb<strong>in</strong>atorial puzzle<br />

by enumerat<strong>in</strong>g <strong>and</strong> test<strong>in</strong>g all possible configurations.<br />

203

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!