稳定的匹配算法–Gale-Shapley

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>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
稳定匹配问题是一个经典的算法问题,其中Gale-Shapley算法是一种常用的解决方案。下面我简要介绍如何使用C语言编写程序来求解稳定匹配问题。 首先,我们需要定义一个结构体来表示每个人的偏好列表和当前匹配状态。偏好列表可以用整数数组来表示,数组的索引表示其他人的编号,数组的值表示对这个人的偏好等级。例如,假设有n个人,我们可以定义一个结构体如下: ```c typedef struct { int n; // 人数 int **preference; // 偏好列表 int *matching; // 当前匹配 } MatchingProblem; ``` 然后,我们可以使用如下的Gale-Shapley算法来求解稳定匹配问题: 1. 初始化所有人的匹配状态为空。 2. 循环直到没有人再能够改变匹配状态为止: a. 对于每个未匹配的人,选择他偏好列表中的下一个候选人,记为当前候选人。 b. 如果当前候选人未匹配,则直接将其与该人匹配。 c. 如果当前候选人已经匹配,判断当前候选人是否更优于当前匹配。如果是,将当前匹配与当前候选人匹配,将原先与当前候选人匹配的人重新放回未匹配状态。 3. 输出每个人的匹配结果。 在C语言中,我们可以使用指针和动态内存分配来操作偏好列表和当前匹配状态。具体实现可以参考下面的伪代码: ```c void gale_shapley(MatchingProblem *problem) { int *proposals = malloc(sizeof(int) * problem->n); // 存储每个人的偏好索引 int *acceptances = malloc(sizeof(int) * problem->n); // 存储每个人的匹配状态 // 初始化匹配状态和偏好索引 for (int i = 0; i < problem->n; i++) { acceptances[i] = -1; // -1表示未匹配状态 proposals[i] = 0; // 初始化偏好索引为0 } while (1) { // 查找未匹配的人 int unmatched = -1; for (int i = 0; i < problem->n; i++) { if (acceptances[i] == -1) { unmatched = i; break; } } if (unmatched == -1) { // 所有人都已匹配 break; } // 获取当前候选人 int current = problem->preference[unmatched][proposals[unmatched]]; proposals[unmatched]++; if (acceptances[current] == -1) { // 当前候选人未匹配 acceptances[current] = unmatched; acceptances[unmatched] = current; } else if (problem->preference[current][unmatched] < problem->preference[current][acceptances[current]]) { // 当前候选人更优 acceptances[acceptances[current]] = -1; // 原来的匹配放回未匹配状态 acceptances[current] = unmatched; acceptances[unmatched] = current; } } // 输出匹配结果 for (int i = 0; i < problem->n; i++) { printf("Person %d is matched with Person %d\n", i, acceptances[i]); } free(proposals); free(acceptances); } ``` 通过以上的程序,我们可以使用Gale-Shapley算法来求解稳定匹配问题。该算法具有良好的时间复杂度,并且可以保证返回的匹配结果是稳定的。希望这个回答可以帮助你理解如何使用C语言编写程序来求解稳定匹配问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值