C Program to Solve Stable Marriage Problem

This is a C Program to solve a matching problem. 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”.

Here is source code of the C Program to Solve a Matching Problem for a Given Specific Case. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2.  
  3. int verbose = 0;
  4. enum {
  5.     clown = -1, abe, bob, col, dan, ed, fred, gav, hal, ian, jon, abi, bea, cath, dee, eve, fay, gay, hope, ivy, jan,};
  6. const char *name[] = { "Abe", "Bob", "Col", "Dan", "Ed", "Fred", "Gav", "Hal",
  7.                        "Ian", "Jon", "Abi", "Bea", "Cath", "Dee", "Eve", "Fay",
  8.                        "Gay", "Hope", "Ivy", "Jan" };
  9. int pref[jan + 1][jon + 1] = {  { abi, eve, cath, ivy, jan, dee, fay, bea, hope, gay },
  10.                                 { cath, hope, abi, dee, eve, fay, bea, jan, ivy, gay },
  11.                                 { hope, eve, abi, dee, bea, fay, ivy, gay, cath, jan },
  12.                                 { ivy, fay, dee, gay, hope, eve, jan, bea, cath, abi },
  13.                                 { jan, dee, bea, cath, fay, eve, abi, ivy, hope, gay },
  14.                                 { bea, abi, dee, gay, eve, ivy, cath, jan, hope, fay },
  15.                                 { gay, eve, ivy, bea, cath, abi, dee, hope, jan, fay },
  16.                                 { abi, eve, hope, fay, ivy, cath, jan, bea, gay, dee },
  17.                                 { hope, cath, dee, gay, bea, abi, fay, ivy, jan, eve },
  18.                                 { abi, fay, jan, gay, eve, bea, dee, cath, ivy, hope },
  19.                                 { bob, fred, jon, gav, ian, abe, dan, ed, col, hal },
  20.                                 { bob, abe, col, fred, gav, dan, ian, ed, jon, hal },
  21.                                 { fred, bob, ed, gav, hal, col, ian, abe, dan, jon },
  22.                                 { fred, jon, col, abe, ian, hal, gav, dan, bob, ed },
  23.                                 { jon, hal, fred, dan, abe, gav, col, ed, ian, bob },
  24.                                 { bob, abe, ed, ian, jon, dan, fred, gav, col, hal },
  25.                                 { jon, gav, hal, fred, bob, abe, col, ed, dan, ian },
  26.                                 { gav, jon, bob, abe, ian, dan, hal, ed, col, fred },
  27.                                 { ian, col, hal, gav, fred, bob, abe, ed, jon, dan },
  28.                                 { ed, hal, gav, abe, bob, jon, col, ian, fred, dan },
  29.                               };
  30. int pairs[jan + 1], proposed[jan + 1];
  31.  
  32. void engage(int man, int woman) {
  33.     pairs[man] = woman;
  34.     pairs[woman] = man;
  35.     if (verbose)
  36.         printf("%4s is engaged to %4s\n", name[man], name[woman]);
  37. }
  38.  
  39. void dump(int woman, int man) {
  40.     pairs[man] = pairs[woman] = clown;
  41.     if (verbose)
  42.         printf("%4s dumps %4s\n", name[woman], name[man]);
  43. }
  44.  
  45. /* how high this person ranks that: lower is more preferred */
  46. int rank(int this, int that) {
  47.     int i;
  48.     for (i = abe; i <= jon && pref[this][i] != that; i++)
  49.         ;
  50.     return i;
  51. }
  52.  
  53. void propose(int man, int woman) {
  54.     int fiance = pairs[woman];
  55.     if (verbose)
  56.         printf("%4s proposes to %4s\n", name[man], name[woman]);
  57.  
  58.     if (fiance == clown) {
  59.         engage(man, woman);
  60.     } else if (rank(woman, man) < rank(woman, fiance)) {
  61.         dump(woman, fiance);
  62.         engage(man, woman);
  63.     }
  64. }
  65.  
  66. int covet(int man1, int wife2) {
  67.     if (rank(man1, wife2) < rank(man1, pairs[man1]) && rank(wife2, man1)
  68.             < rank(wife2, pairs[wife2])) {
  69.         printf("    %4s (w/ %4s) and %4s (w/ %4s) prefer each other"
  70.             " over current pairing.\n", name[man1], name[pairs[man1]],
  71.                 name[wife2], name[pairs[wife2]]);
  72.         return 1;
  73.     }
  74.     return 0;
  75. }
  76.  
  77. int thy_neighbors_wife(int man1, int man2) { /* +: force checking all pairs; "||" would shortcircuit */
  78.     return covet(man1, pairs[man2]) + covet(man2, pairs[man1]);
  79. }
  80.  
  81. int unstable() {
  82.     int i, j, bad = 0;
  83.     for (i = abe; i < jon; i++) {
  84.         for (j = i + 1; j <= jon; j++)
  85.             if (thy_neighbors_wife(i, j))
  86.                 bad = 1;
  87.     }
  88.     return bad;
  89. }
  90.  
  91. int main() {
  92.     int i, unengaged;
  93.     /* init: everyone marries the clown */
  94.     for (i = abe; i <= jan; i++)
  95.         pairs[i] = proposed[i] = clown;
  96.  
  97.     /* rounds */
  98.     do {
  99.         unengaged = 0;
  100.         for (i = abe; i <= jon; i++) {
  101.             //for (i = abi; i <= jan; i++) { /* could let women propose */
  102.             if (pairs[i] != clown)
  103.                 continue;
  104.             unengaged = 1;
  105.             propose(i, pref[i][++proposed[i]]);
  106.         }
  107.     } while (unengaged);
  108.  
  109.     printf("Pairing:\n");
  110.     for (i = abe; i <= jon; i++)
  111.         printf("  %4s - %s\n", name[i],
  112.                 pairs[i] == clown ? "clown" : name[pairs[i]]);
  113.  
  114.     printf(unstable() ? "Marriages not stable\n" /* draw sad face here */
  115.     : "Stable matchup\n");
  116.  
  117.     printf("\nBut if Bob and Fred were to swap:\n");
  118.     i = pairs[bob];
  119.     engage(bob, pairs[fred]);
  120.     engage(fred, i);
  121.     printf(unstable() ? "Marriages not stable\n" : "Stable matchup\n");
  122.  
  123.     return 0;
  124. }

Output:

$ gcc StableMatching.c
$ ./a.out
 
Pairing:
   Abe - Ivy
   Bob - Cath
   Col - Dee
   Dan - Fay
    Ed - Jan
  Fred - Bea
   Gav - Gay
   Hal - Eve
   Ian - Hope
   Jon - Abi
Stable matchup
 
But if Bob and Fred were to swap:
    Fred (w/ Cath) and  Ivy (w/  Abe) prefer each other over current pairing.
     Bob (w/  Bea) and  Fay (w/  Dan) prefer each other over current pairing.
     Bob (w/  Bea) and Hope (w/  Ian) prefer each other over current pairing.
     Bob (w/  Bea) and  Abi (w/  Jon) prefer each other over current pairing.
    Fred (w/ Cath) and  Dee (w/  Col) prefer each other over current pairing.
    Fred (w/ Cath) and  Abi (w/  Jon) prefer each other over current pairing.
Marriages not stable

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.