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

husb<strong>and</strong>s, <strong>and</strong> the sums of the ranks of all men in the preference lists of their wives. These are the values<br />

rm = Sm: 0 ≤ m < n: rmw m,x[m]<br />

rw = Sm: 0 ≤ m < n: rwm x[m],m<br />

x0 x1 x2 x3 x4 x5 x6 x7 rm rw c<br />

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

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

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

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

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

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

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

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

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

c = number of evaluations of stability.<br />

Solution 0 = male optimal solution; solution 8 = female optimal solution.<br />

Table 3.4. Result of the Stable Marriage Problem.<br />

The solution with the least value rm is called the male-optimal stable solution; the one with the smallest<br />

rw is the female-optimal stable solution. It lies in the nature of the chosen search strategy that good<br />

solutions from the men's point of view are generated first <strong>and</strong> the good solutions from the women's<br />

perspective appear toward the end. In this sense, the algorithm is based toward the male population. This<br />

can quickly be changed by systematically interchanging the role of men <strong>and</strong> women, i.e., by merely<br />

interchanging mwr with wmr <strong>and</strong> interchanging rmw with rwm.<br />

We refrain from extending this program further <strong>and</strong> leave the incorporation of a search for an optimal<br />

solution to the next <strong>and</strong> last example of a backtracking algorithm.<br />

3.7 The Optimal Selection Problem<br />

The last example of a backtracking algorithm is a logical extension of the previous two examples<br />

represented by the general schema. First we were using the principle of backtracking to find a single<br />

solution to a given problem. This was exemplified by the knight's tour <strong>and</strong> the eight queens. Then we<br />

tackled the goal of finding all solutions to a given problem; the examples were those of the eight queens<br />

<strong>and</strong> the stable marriages. Now we wish to find an optimal solution.<br />

To this end, it is necessary to generate all possible solutions, <strong>and</strong> in the course of generating them to<br />

retain the one that is optimal in some specific sense. Assuming that optimality is defined in terms of some<br />

positive valued function f(s), the algorithm is derived from the general schema of Try by replacing the<br />

statement print solution by the statement<br />

IF f(solution) > f(optimum) THEN optimum := solution END<br />

The variable optimum records the best solution so far encountered. Naturally, it has to be properly<br />

initialized; morever, it is customary to record to value f(optimum) by another variable in order to avoid its<br />

frequent recomputation.<br />

An example of the general problem of finding an optimal solution to a given problem follows: We choose<br />

the important <strong>and</strong> frequently encountered problem of finding an optimal selection out of a given set of<br />

objects subject to constraints. Selections that constitute acceptable solutions are gradually built up by<br />

investigating individual objects from the base set. A procedure Try describes the process of investigating<br />

the suitability of one individual object, <strong>and</strong> it is called recursively (to investigate the next object) until all

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

Saved successfully!

Ooh no, something went wrong!