GS算法——稳定匹配

题目

The Stable Matching/Marriage Problem

Description

The Stable Matching/Marriage Problem states that given N men and N women, where each person has ranked all members of the opposite sex in order of preference, marry the men and women together such that there are no two people of opposite sex who would both rather have each other than their current partners. If there are no such people, all the marriages are “stable”

Input

the input contains 1+2N Lines,for example:

2 1
1 0
0 1
0 1
0 1

the first line has two numbers,the first number N means we have N men and N women to match,the second number K( 0<= K <= N-1 ) stands for the man'number we choose to check your matching result.the rest 2N lines are the preference list of the n men(count from 0 to n-1) and then the n women(count from 0 to n-1)'sin the example shown above,the correct matching result is:(man 0,women 1) (man 1,women 0)when K=1,the output should be man 1's partner's number ,which is 0

中文说明:输入有1+2N行,第一行有两个数组N和K,N代表有N个男人和N个女人,K用于输出结果,接下来的N行为N个男人(编号为0到N-1)的优先级列表,如1 0 代表该男人的优先级中,woman1优先于woman0再接下来的N行为N个女人人(编号同样为0到N-1)的优先级列表,如0 1代表该女人的优先级中man1优先于man1

输出应为一个数字,代表匹配完成后,编号为K的男人所匹配女人的编号,上面的例子中,由于稳定匹配为:(男人0,女人1) (男人1,女人0)而输入中K=1,则输入应为男人1的匹配对象女人0,输出为一个数字,即:0

Output

should be a number that which woman was match to the Kth man

see the description and output example for more details

Sample Input 1 

4 3
3 1 2 0
1 0 2 3
0 1 2 3
0 1 2 3 
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3

Sample Output 1

2

代码实现

package org.example;

import java.util.Scanner;

public class GS {
    //稳定匹配算法
    public static void main(String[] args) {

        int n,k;//n表示有几个男人或女人,k为输出

        Scanner sc=new Scanner(System.in);

        n=sc.nextInt();
        k=sc.nextInt();

        int[][]manPriority=new int[n][n];//男人对女人的偏爱优先队列

        for(int i=0;i<n;i++){

            for(int j=0;j<n;j++){

                manPriority[i][j]=sc.nextInt();

            }
        }

        int[][]womanPriority=new int[n][n];//女人对男人的偏爱优先队列

        for(int i=0;i<n;i++){

            for(int j=0;j<n;j++){

                womanPriority[i][j]=sc.nextInt();

            }
        }

        stableMatching(n,k,manPriority,womanPriority);

    }


    static void stableMatching(int n, int k, int[][] men, int[][] women){

        int[] man_status=new int[n];
        int[] woman_status=new int[n];
        int i=0;
        for(i=0;i<n;i++){
            man_status[i]=-1;
            woman_status[i]=-1;
        }//初始化男人和女人状态,表示未匹配

        int freeman=0;//自由男人
        int woman,currentman;


        while(freeman!=-1){

            for(i=0;i<n&&man_status[freeman]==-1;i++){
                woman=men[freeman][i];//第freeman个男人,偏爱的第i个女人

                currentman=woman_status[woman];//当前第woman个女人匹配的男人

                if(currentman==-1){//如果为-1则代表第woman个女人没有匹配的男人,则这个woman和freeman相互匹配
                    woman_status[woman]=freeman;
                    man_status[freeman]=woman;
                }else if(women[woman][freeman]<women[woman][currentman]){
                    //如果currentman不为-1,则意味着当前这个女人已经有匹配的男人,则需要将当前匹配的男人与freeman进行匹配
                    //匹配结果如果为freeman的优先度(偏爱度)更高,即所对应的值更小,优先度0>1>2>3


                    woman_status[woman]=freeman;//则该女人所匹配的男人将会替换为freeman
                    man_status[currentman]=-1;//而当前这个男人currentman的状态变为未匹配状态
                    man_status[freeman]=woman;//这个男人freeman与woman相互匹配

                }

            }
            freeman=-1;//初始化freeman
            for(i=0;i<n;i++){
                if(man_status[i]==-1){
                    freeman=i;
                }
            }//遍历寻找还未匹配的男人,如果最后freeman=-1,则所有男人都匹配


        }

        System.out.println(man_status[k]);//输出第K个男人所匹配的女人
    }


}

定理证明

定理:考虑G-S算法的一次执行,他返回一个对的集合S,集合S是一个稳定匹配

算法执行结果是一个稳定的匹配吗?

分析:为证明S是稳定匹配,我们将假设存在一个相对于S的不稳定因素并且得出一个矛盾,

不稳定因素:m和w被匹配在一起,m'和w’匹配在一起,但是m更喜欢w’而不爱w,w'也更喜欢m而不爱m'

那么根据定义,m的最后一次求婚是向w求婚,那么既然m更喜欢w',那么m是否向w'求过婚?

如果有,则m一定被w'拒绝了,并且w'更偏爱其他某个男人;

如果没有,则在m的对女人的偏爱表当中,w一定排在w’前边,

这与我们所假设的m更喜欢w'而不喜欢w相互矛盾

因此根据上述分析论证,不存在这样一个不稳定的因素,从而证得S是一个稳定匹配。

定理:GS算法的每次执行都得到集合S*

首先我们引入有效伴侣和最佳伴侣

有效伴侣:存在一个稳定匹配(m,w),则称w为m的有效伴侣

最佳有效伴侣:如果w是m的有效伴侣,且在m的排名中没有比w更高排名的有效伴侣,则称w为m的最佳有效伴侣

这个算法对于每一个男人而言都得到了其最佳可能得结果,即最佳有效伴侣,又因为最佳有效伴侣对于每个男人而言都是唯一的,所以可以说明这个算法的每次执行都会产生一种情况。

因此,可以利用反证法来证明这个事实。

证:假设在G-S算法执行的某个阶段产生了一个匹配S,并且在这个匹配中男人m没有得到自己的最佳有效伴侣w,假设这个阶段为t,这个男人m按照优先次序,被一个有效伴侣拒绝过一次,考虑算法,此时拒绝m的这个女人w一定是男人m的最佳伴侣,同时,女人w肯定倾心于另一个男人m',根据假设可知,w是m的有效伴侣,存在一个匹配S',其中包含了(m,w)这个配对,那么在这个匹配S'中,m’和谁配对在了一起?

假设与m'匹配在一起的女人为w’,且w'≠w,考虑到阶段t,此时男人m被w拒绝是在执行过程中一个男人被一个有效伴侣的第一次拒绝,并且此过程中w与m'开始约会,所以可以得出,此处m'还未被有效伴侣拒绝过,由于m'是按照自己的偏爱优先列表进行的约会,所以m’更喜欢w而不是w’,并且此时w更喜欢m'而不是m

那么,在稳定匹配S’当中,我们假设的是m'与w'匹配在了一起,然而结果是m'更喜欢w而不是w',w更喜欢m'而不是m,这与我们的假设矛盾,(m',w)为匹配s'中一个不稳定

因此通过上述分析,假设被推翻,可得G-S算法每次执行都可以得到集合S*,即同一个结果

  • 15
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值