Stable Matching (Gale Sharpley Algorithm)
稳定婚配问题:n个男生n个女生。当中每一个人都有自己心仪的列表。
问怎样达成稳定的匹配(比方, b想B求婚,可是B已有的对象的优先级高于b,此时b的魅力不足以拆散B所处的那一对,即达到稳定状态。)
(Gale_Shapley algorithm)整体策略:男士负责求婚,女士负责接受或者拒绝。
题目原型: HDUOJ 1914 The Stable Marriage Problem
下面为数据生成函数,生成boys_rankings.txt 和 girls_rankings.txt
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <sstream>
- #include <algorithm>
- #include <vector>
- #include <ctime>
- #include <fstream>
- using namespace std;
- vector<string> gen_boys(int number)
- {
- vector<string> result;
- for(int i = 0; i < number; i++){
- string boy = "B";
- string s;
- stringstream ss;
- ss << i;
- ss >> s;
- boy.append(s);
- result.push_back(boy);
- }
- return result;
- }
- vector<string> gen_girls(int number)
- {
- vector<string> result;
- for(int i = 0; i < number; i++){
- string boy = "G";
- string s;
- stringstream ss;
- ss << i;
- ss >> s;
- boy.append(s);
- result.push_back(boy);
- }
- return result;
- }
- vector<string> gen_ranking(vector<string> candidate)
- {
- random_shuffle(candidate.begin(), candidate.end());
- return candidate;
- }
- void write_rankings(vector<string> people, vector<vector<string> > rankings, string out_file)
- {
- ofstream ofs(out_file.c_str());
- if(ofs.fail()){
- cerr << "Error: " << out_file << endl;
- return;
- }
- for(int i = 0; i < people.size(); i++){
- ofs << people[i] << ":";
- for(int j = 0; j < rankings[i].size(); j++){
- ofs << " " << rankings.at(i).at(j);
- }
- ofs << endl;
- }
- ofs.close();
- }
- int main(int argc, char ** argv)
- {
- srand(time(NULL));
- int n = 200;
- vector<string> boys = gen_boys(n);
- vector<string> girls = gen_girls(n);
- vector<vector<string> > boys_rankings;
- vector<vector<string> > girls_rankings;
- for(int i = 0; i < n; i++){
- boys_rankings.push_back(gen_ranking(girls));
- girls_rankings.push_back(gen_ranking(boys));
- }
- write_rankings(boys, boys_rankings, "boys_rankings.txt");
- write_rankings(girls, girls_rankings, "girls_rankings.txt");
- return 1;
- }
下面为Gale_Sharpley算法实现:
- /******************************************************
- * Projects : Implementation of Gale_Shapley algorithm
- * Author : johnsondu
- * Time : 2014-10-20 21:18
- *******************************************************/
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <fstream>
- #include <map>
- #include <vector>
- #include <cstring>
- #include <string>
- using namespace std;
- // The max size of the number of the boy.
- const int MAXN = 1000;
- // STL map, map boy's name to integer ID
- map<string, int> boy;
- // STL map, map girl's name to integer ID
- map<string, int> girl;
- // STL map, map corresponding boy's integer id to original string name
- map<int, string> boyName;
- // STL map, map corresponding girl's integer id to original string name
- map<int, string> girlName;
- // boy's preference ranking list
- int boysRankings[MAXN][MAXN];
- // girl's preference ranking list
- int girlsRankings[MAXN][MAXN];
- // the partner that boy matched. if no, marked as -1
- int boysPartner[MAXN];
- // the partner that girl matched. if no, marked as -1
- int girlsPartner[MAXN];
- /*******************************************************
- * function description:
- * let every boy and girl have it's own integer identity.
- * key variable: <string, int>(map string name to integer
- * id), <int, string>(map integer id to original string
- * name)
- ********************************************************/
- void SerialNumber(int &n)
- {
- ifstream in1("boys_rankings.txt");
- ifstream in2("girls_rankings.txt");
- string str;
- int idb = 0; // boy's id
- int idg = 0; // girl's id
- while(getline(in1, str)){
- string name = "";
- int i = 0;
- // get boys's name
- while(str[i] != ':') name += str[i ++];
- // map string to int.
- boy[name] = idb ;
- boyName[idb] = name;
- idb ++;
- // get the number of boys.
- n ++;
- }
- while(getline(in2, str)){
- string name = "";
- int i = 0;
- // get boys's name
- while(str[i] != ':') name += str[i ++];
- // map string to int.
- girl[name] = idg;
- girlName[idg] = name;
- idg ++;
- }
- return;
- }
- /*********************************************
- * function description:
- * Rereading two files, and get corresponding
- * preference list of boys and girls. Save it
- * in two dimension array: girlsRankings and
- * boysRankings.
- *******************************************/
- void GetRankings(const int n)
- {
- ifstream in1("boys_rankings.txt");
- ifstream in2("girls_rankings.txt");
- string str;
- // boy's id.
- int id = 0;
- while(getline(in1, str)){
- string gname;
- int i = 0;
- int cnt = 0;
- while(str[i] != ' ') i ++;
- if(str[i] == ' ') i ++;
- for(; i < str.size();)
- {
- gname = "";
- while(str[i] != ' ' && i < str.size()) {
- gname += str[i];
- i ++;
- }
- boysRankings[id][cnt ++] = girl[gname];
- if(str[i] == ' ') i ++;
- }
- id ++;
- }
- // girls id;
- id = 0;
- while(getline(in2, str)){
- string bname;
- // string size index.
- int i = 0;
- // the rankings in the girl's list.
- int cnt = 0;
- // prefix guarantee
- while(str[i] != ' ') i ++;
- if(str[i] == ' ') i ++;
- for(; i < str.size();)
- {
- bname = "";
- while(str[i] != ' ' && i < str.size()) {
- bname += str[i];
- i ++;
- }
- girlsRankings[id][cnt ++] = boy[bname];
- if(str[i] == ' ') i ++;
- }
- id ++;
- }
- return ;
- }
- /*************************************
- * function description:
- * set status for boys and girls,
- * marked -1, no partner.
- **************************************/
- void InitStatus(const int n)
- {
- for(int i = 0; i < n; i ++)
- {
- boysPartner[i] = -1;
- girlsPartner[i] = -1;
- }
- }
- /****************************************
- * function description:
- * when one boy propose to one girl, and
- * that girl already have a partner, this
- * function is used for get ratings of
- * two boys in that girl's preference list.
- *****************************************/
- int GetId(int boyId, int gId, const int n)
- {
- for(int i = 0; i < n; i ++)
- {
- if(girlsRankings[gId][i] == boyId) return i;
- }
- return -1;
- }
- /****************************************
- * function description:
- * once a man have dumped by a girl, set
- * his status to no partner, and set his
- * preference list of that girl as -1,
- * stand for have been proposed before.
- *****************************************/
- void SetStatus(int boyId, int girlId, const int n)
- {
- boysPartner[boyId] = -1;
- for(int i = 0; i < n; i ++)
- if(boysRankings[boyId][i] == girlId){
- boysRankings[boyId][i] = -1;
- return;
- }
- }
- /****************************************
- * function description:
- * Implementation of Gale_Shapley algorithm
- *****************************************/
- void StableMatch(const int n)
- {
- InitStatus(n);
- while(true)
- {
- bool flag = false;
- bool endOfFor = false;
- // guarantee all the boys have a partner.
- for(int i = 0; i < n; i ++){
- if(boysPartner[i] == -1 || girlsPartner[i] == -1)
- {
- flag = true;
- break;
- }
- }
- if(!flag) break;
- // for boy who have no partner.
- for(int i = 0; i < n; i ++)
- {
- if(boysPartner[i] == -1){
- for(int j = 0; j < n; j ++){
- // Since in the list of preference, j had rejected before.
- if(boysRankings[i][j] == -1) continue;
- // if j have no partner, then accept.
- if(girlsPartner[boysRankings[i][j]] == -1){
- boysPartner[i] = boysRankings[i][j];
- girlsPartner[boysRankings[i][j]] = i;
- endOfFor = true;
- break;
- }
- else{ // judge whether match is stable or not
- int useId = GetId(girlsPartner[boysRankings[i][j]], boysRankings[i][j], n);
- int curId = GetId(i, boysRankings[i][j], n);
- // if not stable
- if(curId < useId){
- // girl's partner's list, set -1 to mean the girl have rejected.
- SetStatus(girlsPartner[boysRankings[i][j]], boysRankings[i][j], n);
- boysPartner[i] = boysRankings[i][j];
- girlsPartner[boysRankings[i][j]] = i;
- endOfFor = true;
- break;
- }
- }
- }
- }
- // find a partner, break out of the loop.
- if(endOfFor) break;
- }
- }
- }
- /****************************************
- * function description:
- * print out result.
- *****************************************/
- void Print(const int n)
- {
- freopen("result.txt", "w", stdout);
- cout << "The Matching Results are follows:" << endl;
- for(int i = 0; i < n; i ++){
- cout << boyName[i]
- << "--" << girlName[boysPartner[i]] << " " << endl;
- }
- }
- /****************************************
- * function description:
- * Main function
- *****************************************/
- int main()
- {
- int n = 0;
- // let every boy and girl have it's own identity.
- // and get integer ranking array list.
- SerialNumber(n);
- // get rankings.S
- GetRankings(n);
- // Stable match
- StableMatch(n);
- // Print solution
- Print(n);
- return 0;
- }
Stable Matching (Gale Sharpley Algorithm)的更多相关文章
- 稳定匹配 - Stable Matching
这篇文章将会对稳定匹配算法进行介绍及Python代码的实现,第一部分会针对稳定匹配的Gale-Shapley算法进行解析,第二部分就是用Python对该算法进行实现. 一.稳定匹配算法原理 1.1 介 ...
- xshell SSH 连接出现 outgoing encryption ,或者no matching host key algorithm found错误的解决
首先看看xshell的使用版本,如果是xshell 4,提示的信息为:no matching host key algorithm found 如果是xshell 5,提示的是: outgoing e ...
- Maximum Cardinality Bipartite Matching: Augmenting Path Algorithm
http://www.csie.ntnu.edu.tw/~u91029/Matching.html int nx,ny; int mx[N],my[N]; bool vy[N]; bool g[N][ ...
- XShell 无法匹配的outgoing encryption算法 ,No matching outgoing encryption algorithm found
在链接的属性(SSH -> 安全性) 的加密算法列表中选择 aes256-ctr, mac加密列表中选择hmac-sha2-256,保存即可 To enable hmac-sha2-256 an ...
- Stable Matching 稳定匹配 婚姻算法 shapley 算法
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051286.html 稳定匹配问题:有N男N女,每个人对于异性都一个排名,先需要得到一种稳 ...
- [algorithm] My rookie plan to start
若干年后,经验有一些,但根基不牢靠.[algorithm] series 借助学习Standard Template Library: Algorithms的这段时期,在自己的算法和c++基础方面加些 ...
- Shine we together: A innovative dating site using 2012 Nobel Laureate Roth's algorithm
Abstract Our dating site introduced scoring and its related functionalities innovatively, conforming ...
- Algorithm | Sort
Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algo ...
- 近年Recsys论文
2015年~2017年SIGIR,SIGKDD,ICML三大会议的Recsys论文: [转载请注明出处:https://www.cnblogs.com/shenxiaolin/p/8321722.ht ...
随机推荐
- 学习参考《TensorFlow深度学习》高清中文版PDF+英文版PDF+源代码
我们知道,TensorFlow是比较流行的深度学习框架,除了看手册文档外,推荐大家看看<Tensorflow深度学习>,共分5方面内容:基础知识.关键模块.算法模型.内核揭秘.生态发展.前 ...
- oracle 正序 逆序 排序查询
正序:从小到大 order by t.id asc 逆序:从大到小 order by t.id desc
- 【CS Round #37 (Div. 2 only) A】Boring Number
[Link]:https://csacademy.com/contest/round-37/task/boring-number/ [Description] 让你找离平均数最近的一个数的下标; [S ...
- C# async/await异步编程深入理解
异步函数简介 一般指 async 修饰符声明得.可包含await表达式得方法或匿名函数. 声明方式 异步方法的声明语法与其他方法完全一样, 只是需要包含 async 关键字.async可以出现在返回值 ...
- 云计算时代告别phpMyAdmin
云计算时代告别phpMyAdmin phpMyAdmin是一款很经典的MySQL数据库管理工具,在云计算快速发展的今天,phpMyAdmin交互老旧.已经不能适应时代步伐.因此有很多人開始选择一些更高 ...
- cocos2d-x的声音控制
声音控制SimpleAudioEngine是单例.下面是其方法. [cpp] view plaincopy //获得SimpleAudioEngine的实例 static SimpleAudioEng ...
- ajax ---- json 和 xml 区别
2.XML和JSON优缺点 (1).XML的优缺点<1>.XML的优点 A.格式统一,符合标准: B.容易与其他系统进行远程交互,数据共享比较方便.<2>.XML的缺点 A.X ...
- 网页中插入javascript的几种方法
网页中插入javascript的方法常见的有两种: 一.直接使用html标记 JavaScript 可以出现在 html的任意地方.使用标记<script>…</script> ...
- Cisco交换机SPAN&RSPAN调试实录
Cisco交换机SPAN&RSPAN设置实录 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang.blog.51cto.com/3 ...
- Server.MapPath()的用法
http://blog.csdn.net/qiuhaifeng_csu/article/details/19416407 Server.MapPath(string path)作用是返回与Web服务器 ...