# PROGRAM TO SOLVE STABLE MARRIAGE PROBLEM.

Stable Marriage Problem is commonly stated as:

Given N men and N women, where each person has ranked all members of the opposite sex with a unique number between 1 to N in order of preference. Marry the men and women off 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”.

```#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<process.h>
#include<conio.h>

int func(char ch,char *array,int max)
{
for(int i=0;i<max;i++)
if(ch==array[i])
return i;
return -1;
}
void main()
{
int N;
fstream fin1,fin2;
char ch;
int i,j,w,t,k,m;
fin1.open("male.txt",ios::in);
fin2.open("female.txt",ios::in);
fin1>>N;
char *male_name=new char[N];
char *female_name=new char[N];
int **prefer=new int*[N];
int    **rank =new int*[N];
for(i=0;i<N;i++)
{
prefer[i]=new int[N];
rank[i]=new int[N];
}
int *fiance =new int[N];
int *next=new int[N];
for(i=0;i<N;i++)
{
next[i]=-1;
fiance[i]=-1;
}
for(i=0;i<N;i++)
fin1>>male_name[i];
for(i=0;i<N;i++)
fin2>>female_name[i];
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
fin1>>ch;
prefer[j][i]=func(ch,female_name,N);
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
fin2>>ch;
rank[j][func(ch,male_name,N)]=i;
}
fin1.close();
fin2.close();
system("cls");
for(i=0;i<N;i++)
for(j=i;j>=0;)
{
next[j]++;
w=prefer[j][next[j]];
if(fiance[w]==-1)
{
fiance[w]=j;
system("cls");
for(k=0;k<N;k++)
{
printf("%c\t",male_name[k]);
for(m=0;m<=next[k];m++)
printf("%c\t",female_name[prefer[k][m]]);
printf("\n");
}
getch();
break;
}
if(rank[w][j]<rank[w][fiance[w]])
{
t=fiance[w];
fiance[w]=j;
j=t;
}
system("cls");
for(k=0;k<N;k++)
{
printf("%c\t",male_name[k]);
for(m=0;m<=next[k];m++)
printf("%c\t",female_name[prefer[k][m]]);
printf("\n");
}
getch();
}
cout<<"\n  FINAL MATCH :\n";
for(k=0;k<N;k++)
cout<<female_name[k]<<'\t'<<male_name[fiance[k]]<<endl;
}```