C++ Program to Implement Stable Marriage Problem

«
»
This C++ Program demonstrates the implementation of Stable Marriage Problem.

Here is source code of the C++ Program to demonstrate the implementation of Stable Marriage Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Implement Stable Marriage Problem
  3.  */
  4. #include <iostream>
  5. #include <cstring>
  6. #include <cstdio>
  7. #define N  4
  8. using namespace std;
  9. /*
  10.  * returns true if woman 'w' prefers man 'm1' over man 'm'
  11.  */
  12. bool wPrefersM1OverM(int prefer[2*N][N], int w, int m, int m1)
  13. {
  14.     for (int i = 0; i < N; i++)
  15.     {
  16.         if (prefer[w][i] == m1)
  17.             return true;
  18.         if (prefer[w][i] == m)
  19.            return false;
  20.     }
  21. }
  22. /*
  23.  * Prints stable matching for N boys and N girls. 
  24.  */
  25. void stableMarriage(int prefer[2*N][N])
  26. {
  27.     int wPartner[N];
  28.     bool mFree[N];
  29.     memset(wPartner, -1, sizeof(wPartner));
  30.     memset(mFree, false, sizeof(mFree));
  31.     int freeCount = N;
  32.     while (freeCount > 0)
  33.     {
  34.         int m;
  35.         for ( m = 0; m < N; m++)
  36.             if (mFree[m] == false)
  37.                 break;
  38.         for (int i = 0; i < N && mFree[m] == false; i++)
  39.         {
  40.             int w = prefer[m][i];
  41.             if (wPartner[w - N] == -1)
  42.             {
  43.                 wPartner[w - N] = m;
  44.                 mFree[m] = true;
  45.                 freeCount--;
  46.             }
  47.  
  48.             else
  49.             {
  50.                 int m1 = wPartner[w-N];
  51.                 if (wPrefersM1OverM(prefer, w, m, m1) == false)
  52.                 {
  53.                     wPartner[w-N] = m;
  54.                     mFree[m] = true;
  55.                     mFree[m1] = false;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     cout << "Woman   Man" << endl;
  61.     for (int i = 0; i < N; i++)
  62.         cout << " " << i + N << "\t" << wPartner[i] << endl;
  63. }
  64.  
  65. /*
  66.  * Main 
  67.  */
  68. int main()
  69. {
  70.     int prefer[2*N][N] = {  {7, 5, 6, 4},
  71.                             {5, 4, 6, 7},
  72.                             {4, 5, 6, 7},
  73.                             {4, 5, 6, 7},
  74.                             {0, 1, 2, 3},
  75.                             {0, 1, 2, 3},
  76.                             {0, 1, 2, 3},
  77.                             {0, 1, 2, 3},
  78.                             };
  79.     stableMarriage(prefer);
  80.     return 0;
  81. }

advertisement
$ g++ stable_marriage.cpp
$ a.out
 
Woman   Man
 4      2
 5      1
 6      3
 7      0
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn