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 120<br />
marrying individuals <strong>and</strong> testing stability after each proposed marriage, x <strong>and</strong> y are needed even before all<br />
their components are defined. In order to keep track of defined components, we may introduce Boolean<br />
arrays<br />
singlem, singlew: ARRAY n OF BOOLEAN<br />
with the meaning that singlem m implies that x m is undefined, <strong>and</strong> singlew w implies that y w is undefined. An<br />
inspection of the proposed algorithm, however, quickly reveals that the marital status of a man k is<br />
determined by the value mthrough the relation<br />
~singlem[k] = k < m<br />
This suggests that the array singlem be omitted; accordingly, we will simplify the name singlew to single.<br />
These conventions lead to the refinement shown by the following procedure Try. The predicate<br />
acceptable can be refined into the conjunction of single <strong>and</strong> stable, where stable is a function to be still<br />
further elaborated.<br />
PROCEDURE Try (m: man);<br />
VAR r: rank; w: woman;<br />
BEGIN<br />
IF m < n THEN<br />
FOR r := 0 TO n-1 DO<br />
w := wmr[m,r];<br />
IF single[w] & stable THEN<br />
x[m] := w; y[w] := m; single[w] := FALSE;<br />
Try(m+1);<br />
single[w] := TRUE<br />
END<br />
END<br />
ELSE<br />
record the solution<br />
END<br />
END Try<br />
At this point, the strong similarity of this solution with procedure AllQueens is still noticeable. The crucial<br />
task is now the refinement of the algorithm to determine stability. Unfortunately, it is not possible to<br />
represent stability by such a simple expression as the safety of a queen's position. The first detail that<br />
should be kept in mind is that stability follows by definition from comparisons of ranks. The ranks of men<br />
or women, however, are nowhere explicitly available in our collection of data established so far. Surely, the<br />
rank of woman w in the mind of man m can be computed, but only by a costly search of w in wmr m . Since<br />
the computation of stability is a very frequent operation, it is advisable to make this information more<br />
directly accessible. To this end, we introduce the two matrices<br />
rmw: ARRAY man, woman OF rank;<br />
rwm: ARRAY woman, man OF rank<br />
such that rmw m,w denotes woman w's rank in the preference list of man m, <strong>and</strong> rwm w,m denotes the rank<br />
of man m in the list of w. It is plain that the values of these auxiliary arrays are constant <strong>and</strong> can initially be<br />
determined from the values of wmr <strong>and</strong> mwr.<br />
The process of determining the predicate stable now proceeds strictly according to its original<br />
definition. Recall that we are trying the feasibility of marrying m <strong>and</strong> w, where w = wmr m,r , i.e., w is man<br />
m's r-th choice. Being optimistic, we first presume that stability still prevails, <strong>and</strong> then we set out to find<br />
possible sources of trouble. Where could they be hidden? There are two symmetrical possibilities:<br />
1. There might be a women pw, preferred to w by m, who herself prefers m over her husb<strong>and</strong>.