25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 117<br />

of the algorithm corresponds to a search of the c<strong>and</strong>idate tree in a systematic fashion in which every node<br />

is visited exactly once. It allows — once a solution is found <strong>and</strong> duly recorded — merely to proceed to<br />

the next c<strong>and</strong>idate delivered by the systematic selection process. The modification is formally accomplished<br />

by carrying the procedure function CanBeDone from the loop's guard into its body <strong>and</strong> substituting the<br />

procedure body in place of its call. To return a logical value is no longer necessary. The general schema is<br />

as follows:<br />

PROCEDURE Try;<br />

BEGIN<br />

IF solution incomplete THEN<br />

initialize selection of c<strong>and</strong>idate moves <strong>and</strong> select the first one;<br />

WHILE ~(no more moves) DO<br />

record move;<br />

Try;<br />

erase move;<br />

select next move<br />

END<br />

ELSE<br />

print solution<br />

END<br />

END Try<br />

It comes as a surprise that the search for all possible solutions is realized by a simpler program than the<br />

search for a single solution.<br />

In the eight queenn problem another simplification is posssible. Indeed, the somewhat cumbersome<br />

mechanism of enumeration of safe positions which consists of the two procedures First <strong>and</strong> Next, was<br />

used to disentangle the linear search of the next safe position (the loop over j within Next) <strong>and</strong> the linear<br />

search of the first j which yields a complete solution. Now, thanks to the simplification of the latter loop,<br />

such a disentanglement is no longer necessary as the simplest loop over j will suffice, with the safe j filtered<br />

by an IF embedded within the loop, without invoking the additional procedures.<br />

The extended algorithm to determine all 92 solutions of the eight queens problem is shown in the<br />

following program. Actually, there are only 12 significantly differing solutions; our program does not<br />

recognize symmetries. The 12 solutions generated first are listed in Table 3.2. The numbers n to the right<br />

indicate the frequency of execution of the test for safe fields. Its average over all 92 solutions is 161.<br />

PROCEDURE write; (* ADenS35_Queens *)<br />

VAR k: INTEGER;<br />

BEGIN<br />

FOR k := 0 TO 7 DO Texts.WriteInt(W, x[k], 4) END;<br />

Texts.WriteLn(W)<br />

END write;<br />

PROCEDURE Try (i: INTEGER);<br />

VAR j: INTEGER;<br />

BEGIN<br />

IF i < 8 THEN<br />

FOR j := 0 TO 7 DO<br />

IF a[j] & b[i+j] & c[i-j+7] THEN<br />

x[i] := j; a[j] := FALSE; b[i+j] := FALSE; c[i-j+7] := FALSE;<br />

Try(i + 1);<br />

x[i] := -1; a[j] := TRUE; b[i+j] := TRUE; c[i-j+7] := TRUE<br />

END

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

Saved successfully!

Ooh no, something went wrong!