Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8680879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:21:42+00:00 2026-06-12T21:21:42+00:00

Yesterday I was doing my homework from algorithm class, an implementation of Gale-Shapley Algorithm,

  • 0

Yesterday I was doing my homework from algorithm class, an implementation of Gale-Shapley Algorithm, it’s an algorithm to solve matching problem between the same amount of males&females who have their own priorities for matching. Then after coding I found it didn’t function well as inputs varied, but I just can’t figure out why, maybe it had sth. to do with memory overflow.
It would help me a lot if someone can point up my errors. Great thanks!
Here’s my codes and pseudo code from my textbook:

#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;
}

PSEUDO code:

Initially all m belongs to M and w belongs to W are free
While there is a man m who is free and hasn’t proposed to every woman
    Choose such a man m
    Let w be the highest-ranked woman in m’s preference list to whom m has not yet proposed
    If w is free then
        (m, w) become engaged
    Else w is currently engaged to m’
        If w prefers m’ to m then
            m remains free
        Else w prefers m to m’
            (m,w) become engaged
            m’becomes free
        Endif
    Endif
Endwhile
Return the set S of engaged pairs

Here’s the sample inputs&outputs:
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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:21:44+00:00Added an answer on June 12, 2026 at 9:21 pm

    From a quick glance at your code, one problem that I see is that the function

    int* Matching(int n,int**MP,int**WP)
    

    is returning the address of a local variable, which is a big no-no.

    I’d change the code as follows:

    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
    

    and after you are done with it free it:

    int*match=Matching(n,MP,WP);
    for(int i=0;i<n;i++)
    {
        cout<<*(match+i)+1<<" ";    // output: matching result
    }
    delete [] match;
    return 0;
    

    Also you should free the memory for

    int **MP, and int **WP

    I believe this is the problem and it happens because the memory for a local variable like int match[n]; is valid only inside the function in which it is allocated.

    There is a more “scientific” explanation for this if you are interested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was propted by apple to upgrade my debugger settings yesterday, In doing so
Hey, I'm totally behind this topic. Yesterday I was doing profiling using Python profiler
Yesterday, I posted a question on some tips doing this Remote Client-Server Application in
I started studying jQuery yesterday by doing tutorials well I'm on tutorial 28 /200
Yesterday I ran into a g++ (3.4.6) compiler problem for code that I have
This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary (It's
I was doing some things with a viewport yesterday in XNA, and couldn't figure
Yesterday I was doing some research on dynamic loading of shared objects and about
I have been playing around with this problem since yesterday. I can't seem to
I was working with quality yesterday doing some formal testing. In their procedure they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.