经历过才能真的感受,做一个靠谱的人!
分类: Java
2013-11-08 16:46:55
Stable Match:Gale-Shapley algorithm(Implementation)
Programing:
Implement Gale-Shapley algorithm of the Stable Matching Problem in your favorite language, and give the matching result of attached ranking data (boys rankings.txt and girls rankings.txt), supposing that the ranking is sorted from high to low.
这是一道经典的算法题,下面是该算法的伪代码:
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 first 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
根据该伪代码,使用java做如下实现,对stable match问题感兴趣的同学可以试试,挺好玩的。
点击(此处)折叠或打开