Stable Marriage Problem
- Difficulty Level : Hard
- Last Updated : 13 Jul, 2022
The Stable 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” (Source Wiki).
Consider the following example.
Let there be two men m1 and m2 and two women w1 and w2.
Let m1’s list of preferences be {w1, w2}
Let m2’s list of preferences be {w1, w2}
Let w1’s list of preferences be {m1, m2}
Let w2’s list of preferences be {m1, m2}![]()
The matching { {m1, w2}, {w1, m2} } is not stable because m1 and w1 would prefer each other over their assigned partners.
The matching {m1, w1} and {m2, w2} is stable because there are no two people of opposite sex that would prefer each other
over their assigned partners.It is always possible to form stable marriages from lists of preferences (See references for proof). Following is Gale–Shapley algorithm to find a stable matching:
The idea is to iterate through all free men while there is any free man available. Every free man goes to all women in his preference list according to the order. For every woman he goes to, he checks if the woman is free, if yes, they both become engaged. If the woman is not free, then the woman chooses either says no to him or dumps her current engagement according to her preference list. So an engagement done once can be broken if a woman gets better option. Time Complexity of Gale-Shapley Algorithm is O(n2).Initialize all men and women to free while there exist a free man m who still has a woman w to propose to { w = m's highest ranked such woman to whom he has not yet proposed if w is free (m, w) become engaged else some pair (m', w) already exists if w prefers m to m' (m, w) become engaged m' becomes free else (m', w) remain engaged }Input & Output: Input is a 2D matrix of size (2*N)*N where N is number of women or men. Rows from 0 to N-1 represent preference lists of men and rows from N to 2*N – 1 represent preference lists of women. So men are numbered from 0 to N-1 and women are numbered from N to 2*N – 1. The output is list of married pairs.
Following is the implementation of the above algorithm.
C++
// C++ program for stable marriage problem#include <iostream>#include <string.h>#include <stdio.h>usingnamespacestd;// Number of Men or Women#define N 4// This function returns true if woman 'w' prefers man 'm1' over man 'm'boolwPrefersM1OverM(intprefer[2*N][N],intw,intm,intm1){// Check if w prefers m over her current engagement m1for(inti = 0; i < N; i++){// If m1 comes before m in list of w, then w prefers her// current engagement, don't do anythingif(prefer[w][i] == m1)returntrue;// If m comes before m1 in w's list, then free her current// engagement and engage her with mif(prefer[w][i] == m)returnfalse;}}// Prints stable matching for N boys and N girls.// Boys are numbered as 0 to N-1. Girls are numbered// as N to 2N-1.voidstableMarriage(intprefer[2*N][N]){// Stores partner of women. This is our output array that// stores passing information. The value of wPartner[i]// indicates the partner assigned to woman N+i. Note that// the woman numbers between N and 2*N-1. The value -1// indicates that (N+i)'th woman is freeintwPartner[N];// An array to store availability of men. If mFree[i] is// false, then man 'i' is free, otherwise engaged.boolmFree[N];// Initialize all men and women as freememset(wPartner, -1,sizeof(wPartner));memset(mFree,false,sizeof(mFree));intfreeCount = N;// While there are free menwhile(freeCount > 0){// Pick the first free man (we could pick any)intm;for(m = 0; m < N; m++)if(mFree[m] ==false)break;// One by one go to all women according to m's preferences.// Here m is the picked free manfor(inti = 0; i < N && mFree[m] ==false; i++){intw = prefer[m][i];// The woman of preference is free, w and m become// partners (Note that the partnership maybe changed// later). So we can say they are engaged not marriedif(wPartner[w-N] == -1){wPartner[w-N] = m;mFree[m] =true;freeCount--;}else// If w is not free{// Find current engagement of wintm1 = wPartner[w-N];// If w prefers m over her current engagement m1,// then break the engagement between w and m1 and// engage m with w.if(wPrefersM1OverM(prefer, w, m, m1) ==false){wPartner[w-N] = m;mFree[m] =true;mFree[m1] =false;}}// End of Else}// End of the for loop that goes to all women in m's list}// End of main while loop// Print the solutioncout <<"Woman Man"<< endl;for(inti = 0; i < N; i++)cout <<" "<< i+N <<"\t"<< wPartner[i] << endl;}// Driver program to test above functionsintmain(){intprefer[2*N][N] = { {7, 5, 6, 4},{5, 4, 6, 7},{4, 5, 6, 7},{4, 5, 6, 7},{0, 1, 2, 3},{0, 1, 2, 3},{0, 1, 2, 3},{0, 1, 2, 3},};stableMarriage(prefer);return0;}Java
// Java program for stable marriage problemimportjava.util.*;classGFG{// Number of Men or WomenstaticintN =4;// This function returns true if woman// 'w' prefers man 'm1' over man 'm'staticbooleanwPrefersM1OverM(intprefer[][],intw,intm,intm1){// Check if w prefers m over// her current engagement m1for(inti =0; i < N; i++){// If m1 comes before m in list of w,// then w prefers her current engagement,// don't do anythingif(prefer[w][i] == m1)returntrue;// If m comes before m1 in w's list,// then free her current engagement// and engage her with mif(prefer[w][i] == m)returnfalse;}returnfalse;}// Prints stable matching for N boys and// N girls. Boys are numbered as 0 to// N-1. Girls are numbered as N to 2N-1.staticvoidstableMarriage(intprefer[][]){// Stores partner of women. This is our// output array that stores passing information.// The value of wPartner[i] indicates the partner// assigned to woman N+i. Note that the woman// numbers between N and 2*N-1. The value -1// indicates that (N+i)'th woman is freeintwPartner[] =newint[N];// An array to store availability of men.// If mFree[i] is false, then man 'i' is// free, otherwise engaged.booleanmFree[] =newboolean[N];// Initialize all men and women as freeArrays.fill(wPartner, -1);intfreeCount = N;// While there are free menwhile(freeCount >0){// Pick the first free man// (we could pick any)intm;for(m =0; m < N; m++)if(mFree[m] ==false)break;// One by one go to all women// according to m's preferences.// Here m is the picked free manfor(inti =0; i < N &&mFree[m] ==false; i++){intw = prefer[m][i];// The woman of preference is free,// w and m become partners (Note that// the partnership maybe changed later).// So we can say they are engaged not marriedif(wPartner[w - N] == -1){wPartner[w - N] = m;mFree[m] =true;freeCount--;}else// If w is not free{// Find current engagement of wintm1 = wPartner[w - N];// If w prefers m over her current engagement m1,// then break the engagement between w and m1 and// engage m with w.if(wPrefersM1OverM(prefer, w, m, m1) ==false){wPartner[w - N] = m;mFree[m] =true;mFree[m1] =false;}}// End of Else}// End of the for loop that goes// to all women in m's list}// End of main while loop// Print the solutionSystem.out.println("Woman Man");for(inti =0; i < N; i++){System.out.print(" ");System.out.println(i + N +" "+wPartner[i]);}}// Driver Codepublicstaticvoidmain(String[] args){intprefer[][] =newint[][]{{7,5,6,4},{5,4,6,7},{4,5,6,7},{4,5,6,7},{0,1,2,3},{0,1,2,3},{0,1,2,3},{0,1,2,3}};stableMarriage(prefer);}}// This code is contributed by Prerna SainiPython3
# Python3 program for stable marriage problem# Number of Men or WomenN=4# This function returns true if# woman 'w' prefers man 'm1' over man 'm'defwPrefersM1OverM(prefer, w, m, m1):# Check if w prefers m over her# current engagement m1foriinrange(N):# If m1 comes before m in list of w,# then w prefers her current engagement,# don't do anythingif(prefer[w][i]==m1):returnTrue# If m comes before m1 in w's list,# then free her current engagement# and engage her with mif(prefer[w][i]==m):returnFalse# Prints stable matching for N boys and N girls.# Boys are numbered as 0 to N-1.# Girls are numbered as N to 2N-1.defstableMarriage(prefer):# Stores partner of women. This is our output# array that stores passing information.# The value of wPartner[i] indicates the partner# assigned to woman N+i. Note that the woman numbers# between N and 2*N-1. The value -1 indicates# that (N+i)'th woman is freewPartner=[-1foriinrange(N)]# An array to store availability of men.# If mFree[i] is false, then man 'i' is free,# otherwise engaged.mFree=[Falseforiinrange(N)]freeCount=N# While there are free menwhile(freeCount >0):# Pick the first free man (we could pick any)m=0while(m < N):if(mFree[m]==False):breakm+=1# One by one go to all women according to# m's preferences. Here m is the picked free mani=0whilei < NandmFree[m]==False:w=prefer[m][i]# The woman of preference is free,# w and m become partners (Note that# the partnership maybe changed later).# So we can say they are engaged not marriedif(wPartner[w-N]==-1):wPartner[w-N]=mmFree[m]=TruefreeCount-=1else:# If w is not free# Find current engagement of wm1=wPartner[w-N]# If w prefers m over her current engagement m1,# then break the engagement between w and m1 and# engage m with w.if(wPrefersM1OverM(prefer, w, m, m1)==False):wPartner[w-N]=mmFree[m]=TruemFree[m1]=Falsei+=1# End of Else# End of the for loop that goes# to all women in m's list# End of main while loop# Print solution("Woman "," Man")foriinrange(N):(i+N,"\t", wPartner[i])# Driver Codeprefer=[[7,5,6,4], [5,4,6,7],[4,5,6,7], [4,5,6,7],[0,1,2,3], [0,1,2,3],[0,1,2,3], [0,1,2,3]]stableMarriage(prefer)# This code is contributed by Mohit KumarC#
// C# program for stable marriage problemusingSystem;usingSystem.Collections.Generic;classGFG{// Number of Men or WomenstaticintN = 4;// This function returns true if woman// 'w' prefers man 'm1' over man 'm'staticboolwPrefersM1OverM(int[,]prefer,intw,intm,intm1){// Check if w prefers m over// her current engagement m1for(inti = 0; i < N; i++){// If m1 comes before m in list of w,// then w prefers her current engagement,// don't do anythingif(prefer[w, i] == m1)returntrue;// If m comes before m1 in w's list,// then free her current engagement// and engage her with mif(prefer[w, i] == m)returnfalse;}returnfalse;}// Prints stable matching for N boys and// N girls. Boys are numbered as 0 to// N-1. Girls are numbered as N to 2N-1.staticvoidstableMarriage(int[,]prefer){// Stores partner of women. This is our// output array that stores passing information.// The value of wPartner[i] indicates the partner// assigned to woman N+i. Note that the woman// numbers between N and 2*N-1. The value -1// indicates that (N+i)'th woman is freeint[]wPartner =newint[N];// An array to store availability of men.// If mFree[i] is false, then man 'i' is// free, otherwise engaged.bool[]mFree =newbool[N];// Initialize all men and women as freefor(inti = 0; i < N; i++)wPartner[i] = -1;intfreeCount = N;// While there are free menwhile(freeCount > 0){// Pick the first free man// (we could pick any)intm;for(m = 0; m < N; m++)if(mFree[m] ==false)break;// One by one go to all women// according to m's preferences.// Here m is the picked free manfor(inti = 0; i < N &&mFree[m] ==false; i++){intw = prefer[m,i];// The woman of preference is free,// w and m become partners (Note that// the partnership maybe changed later).// So we can say they are engaged not marriedif(wPartner[w - N] == -1){wPartner[w - N] = m;mFree[m] =true;freeCount--;}else// If w is not free{// Find current engagement of wintm1 = wPartner[w - N];// If w prefers m over her current engagement m1,// then break the engagement between w and m1 and// engage m with w.if(wPrefersM1OverM(prefer, w, m, m1) ==false){wPartner[w - N] = m;mFree[m] =true;mFree[m1] =false;}}// End of Else}// End of the for loop that goes// to all women in m's list}// End of main while loop// Print the solutionConsole.WriteLine("Woman Man");for(inti = 0; i < N; i++){Console.Write(" ");Console.WriteLine(i + N +" "+wPartner[i]);}}// Driver CodepublicstaticvoidMain(String[] args){int[,]prefer =newint[,]{{7, 5, 6, 4},{5, 4, 6, 7},{4, 5, 6, 7},{4, 5, 6, 7},{0, 1, 2, 3},{0, 1, 2, 3},{0, 1, 2, 3},{0, 1, 2, 3}};stableMarriage(prefer);}}// This code is contributed by Rajput-JiJavascript
<script>// Javascript program for stable marriage problem// Number of Men or WomenN = 4;// This function returns true if woman 'w' prefers man 'm1' over man 'm'functionwPrefersM1OverM( prefer, w, m, m1){// Check if w prefers m over her current engagement m1for(vari = 0; i < N; i++){// If m1 comes before m in list of w, then w prefers her// current engagement, don't do anythingif(prefer[w][i] == m1)returntrue;// If m comes before m1 in w's list, then free her current// engagement and engage her with mif(prefer[w][i] == m)returnfalse;}}// Prints stable matching for N boys and N girls. Boys are numbered as 0 to// N-1. Girls are numbered as N to 2N-1.functionstableMarriage( prefer){// Stores partner of women. This is our output array that// stores passing information. The value of wPartner[i]// indicates the partner assigned to woman N+i. Note that// the woman numbers between N and 2*N-1. The value -1// indicates that (N+i)'th woman is freevarwPartner =newArray(N);// An array to store availability of men. If mFree[i] is// false, then man 'i' is free, otherwise engaged.mFree =newArray(N);// Initialize all men and women as freewPartner.fill(-1);mFree.fill(false);varfreeCount = N;// While there are free menwhile(freeCount > 0){// Pick the first free man (we could pick any)varm;for(m = 0; m < N; m++)if(mFree[m] ==false)break;// One by one go to all women according to m's preferences.// Here m is the picked free manfor(vari = 0; i < N && mFree[m] ==false; i++){varw = prefer[m][i];// The woman of preference is free, w and m become// partners (Note that the partnership maybe changed// later). So we can say they are engaged not marriedif(wPartner[w-N] == -1){wPartner[w-N] = m;mFree[m] =true;freeCount--;}else// If w is not free{// Find current engagement of wvarm1 = wPartner[w-N];// If w prefers m over her current engagement m1,// then break the engagement between w and m1 and// engage m with w.if(wPrefersM1OverM(prefer, w, m, m1) ==false){wPartner[w-N] = m;mFree[m] =true;mFree[m1] =false;}}// End of Else}// End of the for loop that goes to all women in m's list}// End of main while loop// Print the solutiondocument.write("Woman Man"+"<br>");for(vari = 0; i < N; i++)document.write(" "+ (i+N) +" "+ wPartner[i] +"<br>");}varprefer = [ [7, 5, 6, 4],[5, 4, 6, 7],[4, 5, 6, 7],[4, 5, 6, 7],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],];stableMarriage(prefer);// This code is contributed by SoumikMondal</script>OutputWoman Man 4 2 5 1 6 3 7 0My Personal Notes arrow_drop_upRecommended ArticlesPage :Article Contributed By :Improved By :Article Tags :Practice Tags :



