Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gal-Shapley算法的C++实现_C++_Algorithm - Fatal编程技术网

Gal-Shapley算法的C++实现

Gal-Shapley算法的C++实现,c++,algorithm,C++,Algorithm,昨天我在做算法课的作业,这是Gale-Shapley算法的一个实现,它是一个解决相同数量的男性和女性之间匹配问题的算法,他们有自己的匹配优先级。然后在编码后,我发现它的功能不好,因为输入不同,但我就是不明白为什么,也许它与内存溢出有关。 如果有人能指出我的错误,对我会有很大帮助。非常感谢! 这是我课本上的代码和伪代码: #include <iostream> using namespace std; bool isOneFree(int n,bool*P) // test if

昨天我在做算法课的作业,这是Gale-Shapley算法的一个实现,它是一个解决相同数量的男性和女性之间匹配问题的算法,他们有自己的匹配优先级。然后在编码后,我发现它的功能不好,因为输入不同,但我就是不明白为什么,也许它与内存溢出有关。 如果有人能指出我的错误,对我会有很大帮助。非常感谢! 这是我课本上的代码和伪代码:

#include <iostream>
using namespace std;
bool isOneFree(int n,bool*P)    // test if there's at least one man Free.
{
    for(int i=0;i<n;i++)
    {
        if(*(P+i)==true)    return true;
    }
    return false;
}

int* Matching(int n,int**MP,int**WP) // numbers, priority lists of males and females.
{
    bool isManFree[n],isWomanFree[n],isManProposed[n][n];   // to represent matching states.
    int match[n];   // index-value(man-woman) pair

    for(int i=0;i<n;i++)    // initialize values.
    {
        isManFree[i]=true;
        isWomanFree[i]=true;
        for(int j=0;j<n;j++){   isManProposed[i][j]=false;    }
        match[i]=-1;
    }

    while(isOneFree(n,isManFree))   // all matching completed if it returns false.
    {
        int indexM;
        for(int i=0;i<n;i++)
        {
            if(isManFree[i]==true)  { indexM=i; break; }    // we'll try matching this guy with a girl!
        }

        int indexWo;
        for(int i=0;i<n;i++)
        {
            int w=MP[indexM][i];
            if(isManProposed[indexM][w]==false)  { indexWo=w;   break;}  // current priority
        }
        isManProposed[indexM][indexWo]=true;

        if(isWomanFree[indexWo])
        {
            isManFree[indexM]=false;
            isWomanFree[indexWo]=false;
            match[indexM]=indexWo; // they're engaged!
        }
        else    // she's engaged to someone already.
        {
            int indexRival; // find the competitor's index.
            for(int i=0;i<n;i++)
            {
                if(match[i]==indexWo){ indexRival=i;    break; }
            }

            int pM,pRival;
            for(int i=0;i<n;i++)
            {
                if(WP[indexWo][i]==indexM)  pM=i;
                if(WP[indexWo][i]==indexRival)  pRival=i;
            }
            if(pM<pRival)   // the girl's decision
            {
                isManFree[indexM]=false;
                isManFree[indexRival]=true;
                isWomanFree[indexWo]=false;
                match[indexM]=indexWo;  // change the match
            }
        }
    }
    return match;
}

int main()
{
    int n;
    cin>>n;
    int**MP,**WP;
    MP=new int*[n];
    for(int i=0;i<n;i++)    // a lot of inputs
    {
        MP[i]=new int[n];
        for(int j=0;j<n;j++)
        {
            cin>>MP[i][j];
        }
    }
    WP=new int*[n];
    for(int i=0;i<n;i++)
    {
        WP[i]=new int[n];
        for(int j=0;j<n;j++)
        {
            cin>>WP[i][j];
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            MP[i][j]--; // inputs are 1~n, get indexes 0~n-1
            WP[i][j]--;
        }
    }

    int*match=Matching(n,MP,WP);
    for(int i=0;i<n;i++)
    {
        cout<<*(match+i)+1<<" ";    // output: matching result
    }
    return 0;
}
以下是示例输入和输出: 5. 2 1 4 5 3 4 2 1 3 5 2 5 3 4 1 1 4 3 2 5 2 4 1 5 3 5 1 2 4 3 3 2 4 1 5 2 3 4 5 1 1 5 4 3 2 4 2 5 3 1


1 3 2 5 4

快速浏览一下您的代码,我发现一个问题是函数

int* Matching(int n,int**MP,int**WP)
返回一个局部变量的地址,这是一个很大的禁忌

我将更改代码如下:

int* Matching(int n,int**MP,int**WP) // numbers, priority lists of males and females.
{
    bool isManFree[n],isWomanFree[n],isManProposed[n][n];   // to represent matching states.
    int *match = new int[n];   // index-value(man-woman) pair
完成后,请将其释放:

int*match=Matching(n,MP,WP);
for(int i=0;i<n;i++)
{
    cout<<*(match+i)+1<<" ";    // output: matching result
}
delete [] match;
return 0;
此外,您还应该释放内存,以便

int**MP和int**WP

我相信这就是问题所在,它的出现是因为像int match[n]这样的局部变量的内存;仅在分配它的函数内有效


如果你感兴趣,有一个更科学的解释

快速浏览一下您的代码,我发现一个问题是函数

int* Matching(int n,int**MP,int**WP)
返回一个局部变量的地址,这是一个很大的禁忌

我将更改代码如下:

int* Matching(int n,int**MP,int**WP) // numbers, priority lists of males and females.
{
    bool isManFree[n],isWomanFree[n],isManProposed[n][n];   // to represent matching states.
    int *match = new int[n];   // index-value(man-woman) pair
完成后,请将其释放:

int*match=Matching(n,MP,WP);
for(int i=0;i<n;i++)
{
    cout<<*(match+i)+1<<" ";    // output: matching result
}
delete [] match;
return 0;
此外,您还应该释放内存,以便

int**MP和int**WP

我相信这就是问题所在,它的出现是因为像int match[n]这样的局部变量的内存;仅在分配它的函数内有效


如果你感兴趣,有一个更科学的解释

我已经纠正了我的代码,它的工作!事实证明,我没有注意动态内存分配机制,这正是您为我指出的问题。非常感谢~~@行中的泛齿龈炎forint i=0;i@mk1我不是很确定,因为我不太熟悉这个特定的算法。问题和答案是关于C++实现的。然而,从我的头脑开始,我会使用一种更有效的数据结构来更改isOneFree函数,也许是哈希表?为了降低算法的复杂度,我已经修改了我的代码,它可以工作了!事实证明,我没有注意动态内存分配机制,这正是您为我指出的问题。非常感谢~~@行中的泛齿龈炎forint i=0;i@mk1我不是很确定,因为我不太熟悉这个特定的算法。问题和答案是关于C++实现的。然而,从我的头脑开始,我会使用一种更有效的数据结构来更改isOneFree函数,也许是哈希表?为了降低算法的复杂度。