稳定的匹配算法–Gale-Shapley

时间:2022-12-09 09:53:14

Implement Gale-Shapley algorithm of the Stable Matching Problem in yourfavorite language, and give the matching result of attached ranking data(boys rankings.txt and girls rankings.txt), supposing that the ranking is sortedfrom high to low. (C++ source file gs make ranking.cc which generates thedata is also supplied here.)

1: for m = 1 to M do
2: partner[m] = NULL
3: end for
4: for w = 1 to W do
5: partner[w] = NULL
6: end for
7: while TRUE do
8: if there is no man m such that partner[m] = NULL then
9: return;
10: end if
11: select such a man m arbitrarily;
12: w = the rst woman on m
′s list to whom m have not yet proposed;
13: if partner[w] == NULL then
14: partner[w] = m; partner[m] = w;
15: else if w prefers m to state[w] then
16: partner[partner[w]] = NULL; partner[w] = m; partner[m] = w;
17: else
18: ; //do nothing means simply rejecting m;
19: end if
20: end while

分析:

男士优先,选取第一个男士开始,向自己的最优选择提出求婚,女士进行选择,如果女士之前没有结婚,就暂时答应求婚,如果女士已经结婚,就比较一下当前求婚人与已经结婚人谁更好,选择好的重新结婚。之前被离婚的男士就继续向下一个选择求婚。

此过程一直持续,直到所有人都找到了匹配为止。

代码实现:(C#)

<span style="font-size:14px;">using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Text.RegularExpressions;



namespace pipei

{

class Program

{

public const int N=200;

public static int[,] m = new int[N + 1, N + 1] ;

public static int[,] w = new int[N + 1, N + 1] ;

public static int mi = 0;

public static int mj = 0;

public static int wi = 0;

public static int wj = 0;

public static int havemarry = 0;

public static int ok = 0;

public static int flag = 0;



public static int w_select()

{

wi=m[mi,mj];

//还没有结婚

if(w[wi,0]==0)

{

w[wi,0]=mi;

m[mi,0]=wi;

m[mi,mj]=0;

ok=1;

}

//已经结婚

else

{

havemarry = w[wi,0];

for(wj = 1;wj<=N; wj++)

{

//新求婚者不合适

if(w[wi,wj] == havemarry)

{

ok = 0;

m[mi,mj] = 0;

break;

}

//新求婚者更合适

if(w[wi,wj] == mi)

{

ok = 1;

w[wi,0] = mi;

m[mi,0] = wi;

m[havemarry,0] = 0;

for(int tmp = 1;tmp<=N;tmp++)

{

if(m[havemarry,tmp] == wi)

m[havemarry,tmp] = 0;

}

break;

}

}

}

return ok;

}



public static void m_propose()

{

for(mj=1;mj<=N;mj++)

{

if(m[mi,mj]==0) continue;

ok=w_select();

if(ok==1) break;

}

}

static void Main(string[] args)

{

String texta = File.ReadAllText(@”boys_rankings.txt”, Encoding.Default);

String[] sa =new String[(N+1)*N];

sa= texta.Split(‘ ‘);

int[] ma = new int[(N + 1) * N];

for (int i = 0; i < ((N + 1) * N ); i++)

{

sa[i] = Regex.Replace(sa[i], @”[^0-9.]”, string.Empty);

ma[i] = Convert.ToInt32(sa[i]);

}

String textb = File.ReadAllText(@”girls_rankings.txt”, Encoding.Default);

String[] sb = new String[(N + 1) * N];

sb = textb.Split(‘ ‘);

int[] mb = new int[(N + 1) * N];

for (int i = 0; i < ((N + 1) * N ); i++)

{

sb[i] = Regex.Replace(sb[i], @”[^0-9.]”, string.Empty);

mb[i] = Convert.ToInt32(sb[i]);

}

int fi = 0;

int fj = 0;

for (int i = 1; i <= N; i++)

{

for (int j = 0; j <= N; j++)

{

m[i,j] = ma[fi++];

w[i, j] = mb[fj++];

}

}

for (int i = 1; i <= N; i++)

{

m[i, 0] = 0;

w[i, 0] = 0;

}

for (int i = 1; i <= N; i++)

{

for (int j = 1; j <= N; j++)

{

m[i, j] = m[i,j]+1;

w[i, j] = w[i,j]+1;

}

}

while (flag == 0)

{

flag = 1;

for (mi = 1; mi <= N; mi++)

{

if (m[mi, 0] == 0)

{

flag = 0;

m_propose();

}

}

}

for(int i=1;i<=N;i++)

{

Console.Write(“(B{0} : G{1}) “,i-1,(m[i,0]-1));

}

Console.WriteLine();

Console.Read();

}

}

}</span>