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 118<br />

END<br />

ELSE<br />

write;<br />

m := m+1 (*solutions count*)<br />

END<br />

END Try;<br />

PROCEDURE AllQueens*;<br />

VAR i, j: INTEGER;<br />

BEGIN<br />

FOR i := 0 TO 7 DO a[i] := TRUE; x[i] := -1 END;<br />

FOR i := 0 TO 14 DO b[i] := TRUE; c[i] := TRUE END;<br />

m := 0;<br />

Try(0);<br />

Log.String('no. of solutions: '); Log.Int(m); Log.Ln<br />

END AllQueens.<br />

x0 x1 x2 x3 x4 x5 x6 x7 n<br />

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

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

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

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

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

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

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

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

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

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

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

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

Table 3.2. Twelve Solutions to the Eight Queens Problem.<br />

3.6 The Stable Marriage Problem<br />

Assume that two disjoint sets A <strong>and</strong> B of equal size n are given. Find a set of n pairs such that a<br />

in A <strong>and</strong> b in B satisfy some constrains. Many different criteria for such pairs exist; one of them is the rule<br />

called the stable marriage rule.<br />

Assume that A is a set of men <strong>and</strong> B is a set of women. Each man <strong>and</strong> each women has stated distinct<br />

preferences for their possible partners. If the n couples are chosen such that there exists a man <strong>and</strong> a<br />

woman who are not married, but who would both prefer each other to their actual marriage partners, then<br />

the assignment is unstable. If no such pair exists, it is called stable. This situation characterizes many related<br />

problems in which assignments have to be made according to preferences such as, for example, the choice<br />

of a school by students, the choice of recruits by different branches of the armed services, etc. The<br />

example of marriages is particularly intuitive; note, however, that the stated list of preferences is invariant<br />

<strong>and</strong> does not change after a particular assignment has been made. This assumption simplifies the problem,<br />

but it also represents a grave distortion of reality (called abstraction).<br />

One way to search for a solution is to try pairing off members of the two sets one after the other until the<br />

two sets are exhausted. Setting out to find all stable assignments, we can readily sketch a solution by using<br />

the program schema of AllQueens as a template. Let Try(m) denote the algorithm to find a partner for man

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

Saved successfully!

Ooh no, something went wrong!