• No se han encontrado resultados

2.4 EL DESARROLLO AFECTIVO

In document 1 AZ Y DES EN LA ADOLESCENCIA completo (página 50-68)

Assume that two disjoint sets A and B of equal size n are given. Find a set of n pairs <a, b> such that a in A and b in B satisfy some constrains. Many different criteria for such pairs exist; one of them is the rule called the stable marriage rule.

Assume that A is a set of men and B is a set of women. Each man and each women has stated distinct preferences for their possible partners. If the n couples are chosen such that there exists a man and a woman who are not married, but who would both prefer each other to their actual marriage partners, then the assignment is unstable. If no such pair exists, it is called stable. This situation characterizes many related problems in which assignments have to be made according to preferences such as, for example, the choice of a school by students, the choice of recruits by different branches of the armed services, etc. The example of marriages is particularly intuitive; note, however, that the stated list of preferences is invariant and does not change after a particular assignment has been made. This assumption simplifies the problem, but it also represents a grave distortion of reality (called abstraction).

One way to search for a solution is to try pairing off members of the two sets one after the other until the two sets are exhausted. Setting out to find all stable assignments, we can readily sketch a solution by using the program schema of AllQueens as a template. Let Try(m) denote the algorithm to find a partner for man m, and let this search proceed in the order of the man's list of stated preferences. The first version based on these assumptions is:

PROCEDURE Try(m: man); VAR r: rank;

BEGIN

FOR r := 0 TO n-1 DO

pick the r th preference of man m; IF acceptable THEN record the marriage; IF m is not last man THEN Try(successor(m)) ELSE record the stable set

END ;

cancel the marriage END

END END Try

The initial data are represented by two matrices that indicate the men's and women's preferences. VAR wmr: ARRAY n, n OF woman

mwr: ARRAY n, n OF man

Accordingly, wmrm denotes the preference list of man m, i.e., wmrm,r is the woman who occupies the r th rank in the list of man m. Similarly, mwrw is the preference list of woman w, and mwrw,r is her r th choice.

A sample data set is shown in Table 3.3.

r = 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 m = 1 7 2 6 5 1 3 8 4 w = 1 4 6 2 5 8 1 3 7 2 4 3 2 6 8 1 7 5 2 8 5 3 1 6 7 4 2 3 3 2 4 1 8 5 7 6 3 6 8 1 2 3 4 7 5 4 3 8 4 2 5 6 7 1 4 3 2 4 7 6 8 5 1 5 8 3 4 5 6 1 7 2 5 6 3 1 4 5 7 2 8 6 8 7 5 2 4 3 1 6 6 2 1 3 8 7 4 6 5

7 2 4 6 3 1 7 5 8 7 3 5 7 2 4 1 8 6 8 6 1 4 2 7 5 3 8 8 7 2 8 4 5 6 3 1

Table 3.3 Sample Input Data for wmr and mwr

The result is represented by an array of women x, such that xm denotes the partner of man m. In order to maintain symmetry between men and women, an additional array y is introduced, such that yw denotes the partner of woman w.

VAR x, y: ARRAY n OF INTEGER

Actually, y is redundant, since it represents information that is already present through the existence of x. In fact, the relations

x[y[w]] = w, y[x[m]] = m

hold for all m and w who are married. Thus, the value yw could be determined by a simple search of x; the array y, however, clearly improves the efficiency of the algorithm. The information represented by x and y is needed to determine stability of a proposed set of marriages. Since this set is constructed stepwise by marrying individuals and testing stability after each proposed marriage, x and y are needed even before all their components are defined. In order to keep track of defined components, we may introduce Boolean arrays

singlem, singlew: ARRAY n OF BOOLEAN

with the meaning that singlemm implies that xm is defined, and singleww implies that yw is defined. An

inspection of the proposed algorithm, however, quickly reveals that the marital status of a man is determined by the value m through the relation

~singlem[k] = k < m

This suggests that the array singlem be omitted; accordingly, we will simplify the name singlew to single. These conventions lead to the refinement shown by the following procedure Try. The predicate acceptable can be refined into the conjunction of single and stable, where stable is a function to be still further elaborated.

PROCEDURE Try(m: man); VAR r: rank; w: woman; BEGIN

FOR r := 0 TO n-1 DO w := wmr[m,r];

IF single[w] & stable THEN

x[m] := w; y[w] := m; single[w] := FALSE; IF m < n THEN Try(m+1) ELSE record set END ; single[w] := TRUE

END END END Try

At this point, the strong similarity of this solution with procedure AllQueens is still noticeable. The crucial task is now the refinement of the algorithm to determine stability. Unfortunately, it is not possible to represent stability by such a simple expression as the safety of a queen's position. The first detail that should be kept in mind is that stability follows by definition from comparisons of ranks. The ranks of men or women, however, are nowhere explicitly available in our collection of data established so far. Surely, the rank of woman w in the mind of man m can be computed, but only by a costly search of w in wmrm. Since the computation of stability is a very frequent operation, it is advisable to make this information more directly accessible. To this end, we introduce the two matrices

rmw: ARRAY man, woman OF rank; rwm: ARRAY woman, man OF rank

such that rmwm,w denotes woman w's rank in the preference list of man m, and rwmw,m denotes the rank of man m in the list of w. It is plain that the values of these auxiliary arrays are constant and can initially be determined from the values of wmr and mwr.

The process of determining the predicate stable now proceeds strictly according to its original definition. Recall that we are trying the feasibility of marrying m and w, where w = wmrm,r , i.e., w is man m's r th choice. Being optimistic, we first presume that stability still prevails, and then we set out to find possible sources of trouble. Where could they be hidden? There are two symmetrical possibilities:

1. There might be a women pw, preferred to w by m, who herself prefers m over her husband. 2. There might be a man pm, preferred to m by w, who himself prefers w over his wife.

Pursuing trouble source 1, we compare ranks rwmpw,m and rwmpw,y[pw] for all women preferred to w by m, i.e. for all pw = wmrm,i such that i < r. We happen to know that all these candidate women are already married because, were anyone of them still single, m would have picked her beforehand. The described process can be formulated by a simple linear search; s denotes stability.

s := TRUE; i := 0; WHILE (i < r) & s DO pw := wmr[m,i]; INC(i);

IF ~single[pw] THEN s := rwm[pw,m] > rwm[pw, y[pw]] END END

Hunting for trouble source 2, we must investigate all candidates pm who are preferred by w to their current assignation m, i.e., all preferred men pm = mwrw,i such that i < rwmw,m. In analogy to tracing trouble source 1, comparison between ranks rmwpm,w and rmwpm,x[pm] is necessary. We must be careful, however, to omit comparisons involving xpm where pm is still single. The necessary safeguard is a test pm < m, since we know that all men preceding m are already married.

The complete algorithm is shown in module Marriage. Table 3.4 specifies the nine computed stable solutions from input data wmr and mwr given in Table 3.3.

PROCEDURE write; (*global writer W*) VAR m: man; rm, rw: INTEGER; BEGIN rm := 0; rw := 0;

FOR m := 0 TO n-1 DO Texts.WriteInt(W, x[m], 4);

rm := rmw[m, x[m]] + rm; rw := rwm[x[m], m] + rw END ;

Texts.WriteInt(W, rm, 8); Texts.WriteInt(W, rw, 4); Texts.WriteLn(W) END write;

PROCEDURE stable(m, w, r: INTEGER): BOOLEAN; VAR pm, pw, rank, i, lim: INTEGER;

S: BOOLEAN; BEGIN S := TRUE; i := 0; WHILE (i < r) & S DO pw := wmr[m,i]; INC(i);

IF ~single[pw] THEN S := rwm[pw,m] > rwm[pw, y[pw]] END END ;

i := 0; lim := rwm[w,m]; WHILE (i < lim) & S DO pm := mwr[w,i]; INC(i);

IF pm < m THEN S := rmw[pm,w] > rmw[pm, x[pm]] END END ;

RETURN S END stable;

PROCEDURE Try(m: INTEGER); VAR w, r: INTEGER;

BEGIN

FOR r := 0 TO n-1 DO w := wmr[m,r]; IF single[w] & stable(m,w,r) THEN

x[m] := w; y[w] := m; single[w] := FALSE; IF m < n-1 THEN Try(m+1) ELSE write END ; single[w] := TRUE

END END END Try;

PROCEDURE FindStableMarriages(VAR S: Texts.Scanner); VAR m, w, r: INTEGER;

BEGIN

FOR m := 0 TO n-1 DO

FOR r := 0 TO n-1 DO Texts.Scan(S); wmr[m,r] := S.i; rmw[m, wmr[m,r]] := r END END ;

FOR w := 0 TO n-1 DO single[w] := TRUE;

FOR r := 0 TO n-1 DO Texts.Scan(S); mwr[w,r] := S.i; rwm[w, mwr[w,r]] := r END END ;

Try(0)

END FindStableMarriages END Marriage

This algorithm is based on a straightforward backtracking scheme. Its efficiency primarily depends on the sophistication of the solution tree pruning scheme. A somewhat faster, but more complex and less transparent algorithm has been presented by McVitie and Wilson [3-1 and 3-2], who also have extended it to the case of sets (of men and women) of unequal size.

Algorithms of the kind of the last two examples, which generate all possible solutions to a problem (given certain constraints), are often used to select one or several of the solutions that are optimal in some sense. In the present example, one might, for instance, be interested in the solution that on the average best satisfies the men, or the women, or everyone.

Notice that Table 3.4 indicates the sums of the ranks of all women in the preference lists of their husbands, and the sums of the ranks of all men in the preference lists of their wives. These are the values

rm =

S

m: 1 ≤ m ≤ n: rmwm,x[m] rw =

S

m: 1 ≤ m ≤ n: rwmx[m],m x1 x2 x3 x4 x5 x6 x7 x8 rm rw c 1 7 4 3 8 1 5 2 6 16 32 21 2 2 4 3 8 1 5 7 6 22 27 449 3 2 4 3 1 7 5 8 6 31 20 59 4 6 4 3 8 1 5 7 2 26 22 62 5 6 4 3 1 7 5 8 2 35 15 47 6 6 3 4 8 1 5 7 2 29 20 143 7 6 3 4 1 7 5 8 2 38 13 47 8 3 6 4 8 1 5 7 2 34 18 758 9 3 6 4 1 7 5 8 2 43 11 34

c = number of evaluations of stability.

Solution 1 = male optimal solution; solution 9 = female optimal solution. Table 3.4 Result of the Stable Marriage Problem.

The solution with the least value rm is called the male-optimal stable solution; the one with the smallest rw is the female-optimal stable solution. It lies in the nature of the chosen search strategy that good

solutions from the men's point of view are generated first and the good solutions from the women's perspective appear toward the end. In this sense, the algorithm is based toward the male population. This can quickly be changed by systematically interchanging the role of men and women, i.e., by merely interchanging mwr with wmr and interchanging rmw with rwm.

We refrain from extending this program further and leave the incorporation of a search for an optimal solution to the next and last example of a backtracking algorithm.

In document 1 AZ Y DES EN LA ADOLESCENCIA completo (página 50-68)