Algorithms and Data Structures
Algorithms and Data Structures
Algorithms and Data Structures
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 109<br />
The obvious way to reduce the problem of covering n 2 fields is to consider the problem of either<br />
performing a next move or finding out that none is possible. Let us define the corresponding algorithm. A<br />
first approach is to employ a linear search in order to find the next move from which the tour can be<br />
completed:<br />
PROCEDURE TryNextMove;<br />
BEGIN<br />
IF board is not full THEN<br />
initialize selection of c<strong>and</strong>idates for the next move <strong>and</strong> select first one;<br />
WHILE ~(no more c<strong>and</strong>idates) & ~(tour can be completed from this c<strong>and</strong>idate) DO<br />
select next c<strong>and</strong>idate<br />
END;<br />
h<strong>and</strong>le search results<br />
ELSE<br />
h<strong>and</strong>le the case of full board<br />
END<br />
END TryNextMove;<br />
Notice the IF encompassing the procedure body: it ensures that the degenerate case of a full board is<br />
h<strong>and</strong>led correctly. This is a general device that is similar to how arithmetic operations are defined to h<strong>and</strong>le<br />
the zero case: the purpose is convenience <strong>and</strong> robustness. If such checks are performed outside of the<br />
procedure (as is often done for optimization) then each call must be accompanied by such a check — or<br />
its absense must be properly justified in each case. Introducing such complications is best postponed until a<br />
correct algorithm is constructed <strong>and</strong> their necessity is seen.<br />
The predicate tour can be completed from this c<strong>and</strong>idate is conveniently expressed as a functionprocedure<br />
that returns a logical value. Since it is necessary to record the sequence of moves being<br />
generated, the function-procedure is a proper place both for recording the next move <strong>and</strong> for its rejection<br />
because it is in this procedure that the success of completion of the tour is determined.<br />
PROCEDURE CanBeDone ( move ): BOOLEAN;<br />
BEGIN<br />
record move;<br />
TryNextMove;<br />
IF failed to complete the tour THEN<br />
erase move<br />
END;<br />
RETURN tour has been completed<br />
END CanBeDone<br />
The recursion scheme is already evident here.<br />
If we wish to be more precise in describing this algorithm, we are forced to make some decisions on<br />
data representation. We wish to keep track of the history of successive board occupations. Then each<br />
move in the tour sequence can be characterized by an integer i in addition to its two coordinates on the<br />
board x, y.<br />
At this point we can make decisions about the appropriate parameters for the two procedures<br />
TryNextMove <strong>and</strong> CanBeDone.<br />
The parameters of TryNextMove are to determine the starting conditions for the next move <strong>and</strong> also to<br />
report on its success. The former task is adequately solved by specifying the coordinates x, y from which<br />
the move is to be made, <strong>and</strong> by specifying the number i of the move (for recording purposes). The latter<br />
task requires a Boolean result parameter with the meaning the move was successful. The resulting<br />
signature is