25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

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.

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

m, <strong>and</strong> let this search proceed in the order of the man's list of stated preferences. The first version based on<br />

these assumptions is:<br />

PROCEDURE Try (m: man);<br />

VAR r: rank;<br />

BEGIN<br />

IF m < n THEN<br />

FOR r := 0 TO n-1 DO<br />

pick the r-th preference of man m;<br />

IF acceptable THEN<br />

record the marriage;<br />

Try(successor(m));<br />

cancel the marriage<br />

END<br />

END<br />

ELSE<br />

record the stable set<br />

END<br />

END Try<br />

The initial data are represented by two matrices that indicate the men's <strong>and</strong> women's preferences.<br />

VAR wmr: ARRAY n, n OF woman;<br />

mwr: ARRAY n, n OF man<br />

Accordingly, wmr m denotes the preference list of man m, i.e., wmr m,r is the woman who occupies the r-th<br />

rank in the list of man m. Similarly, mwr w is the preference list of woman w, <strong>and</strong> mwr w,r is her r-th choice.<br />

A sample data set is shown in Table 3.3.<br />

The result is represented by an array of women x, such that x m denotes the partner of man m. In order<br />

to maintain symmetry between men <strong>and</strong> women, an additional array y, such that y w denotes the partner of<br />

woman w:<br />

VAR x, y: ARRAY n OF INTEGER<br />

r = 0 1 2 3 4 5 6 7 r = 0 1 2 3 4 5 6 7<br />

m = 0 6 1 5 4 0 2 7 3 w = 0 3 5 1 4 7 0 2 6<br />

1 3 2 1 5 7 0 6 4 1 7 4 2 0 5 6 3 1<br />

2 2 1 3 0 7 4 6 5 2 5 7 0 1 2 3 6 4<br />

3 2 7 3 1 4 5 6 0 3 2 1 3 6 5 7 4 0<br />

4 7 2 3 4 5 0 6 1 4 5 2 0 3 4 6 1 7<br />

5 7 6 4 1 3 2 0 5 5 1 0 2 7 6 3 5 4<br />

6 1 3 5 2 0 6 4 7 6 2 4 6 1 3 0 7 5<br />

7 5 0 3 1 6 4 2 7 7 6 1 7 3 4 5 2 0<br />

Table 3.3. Sample Input <strong>Data</strong> for wmr <strong>and</strong> mwr.<br />

Actually, y is redundant, since it represents information that is already present through the existence of x. In<br />

fact, the relations<br />

x[y[w]] = w,<br />

y[x[m]] = m<br />

hold for all m <strong>and</strong> w who are married. Thus, the value y w could be determined by a simple search of x; the<br />

array y, however, clearly improves the efficiency of the algorithm. The information represented by x <strong>and</strong> y<br />

is needed to determine stability of a proposed set of marriages. Since this set is constructed stepwise by

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

Saved successfully!

Ooh no, something went wrong!