-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstableMatch.cpp
More file actions
227 lines (190 loc) · 4.5 KB
/
stableMatch.cpp
File metadata and controls
227 lines (190 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <random>
#include <chrono>
#include <map>
using namespace std;
// Trim whitespaces from a string
void trim(string& s)
{
const char* ws = " :\t\n\r\f\v";
s.erase(s.find_last_not_of(ws) + 1);
s.erase(0, s.find_first_not_of(ws));
}
// Print preferences string vectors
void printPref(vector<vector<string> > &matrix)
{
for(int i=0; i<matrix.size(); i++)
{
for(int j=0; j<matrix[i].size(); j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
// Print all candidate's name
void printCandidates(vector<vector<string> > &men, vector<vector<string> > &women)
{
cout<<"Men Candidates: ";
for (int i = 0; i < men.size(); ++i)
{
cout<<men[i][0];
if(i != men.size()-1)
cout<<", ";
}
cout<<"\nWomen Candidates: ";
for (int i = 0; i < women.size(); ++i)
{
cout<<women[i][0];
if(i != women.size()-1)
cout<<", ";
}
cout<<endl;
}
// Read Preferences from file
void readPref(vector<vector<string> > &Pref, ifstream &Fin)
{
string temp;
while(getline(Fin,temp,'\n'))
{
stringstream ss;
ss<<temp;
string name;
getline(ss,name,':');
trim(name);
vector<string> tempVec;
tempVec.push_back(name);
while(getline(ss,name,','))
{
trim(name);
tempVec.push_back(name);
}
Pref.push_back(tempVec);
}
}
// Checking if a women will change her current matching to newMan
bool willWomanChange(vector<vector<string> > &womenPref, string currWoman, string prevMan, string newMan)
{
int i=0;
while(womenPref[i][0] != currWoman)
{
i++;
}
for(int j=1;j<womenPref[i].size();j++)
{
if(womenPref[i][j] == prevMan)
break;
else if(womenPref[i][j] == newMan)
return true;
}
return false;
}
// Function to find a stable matching
void findMatching(vector<vector<string> > &menPref, vector<vector<string> > &womenPref, vector<vector<string> > &result)
{
map<string, string> menMatches;
map<string, string> womenMatches;
int n = menPref.size(); // also equal to womenPref.size()
for(int i=0; i<n; i++)
{
menMatches[menPref[i][0]] = "NULL";
womenMatches[womenPref[i][0]] = "NULL";
}
int freeMenCount = menPref.size();
while(freeMenCount > 0)
{
int freeMenIndex=0;
for(; freeMenIndex<menPref.size(); freeMenIndex++)
{
if(menMatches[menPref[freeMenIndex][0]] == "NULL")
break;
}
int itr = 1;
string currMan = menPref[freeMenIndex][0];
while( itr < menPref[freeMenIndex].size() )
{
string currWoman = menPref[freeMenIndex][itr];
if( womenMatches[currWoman] == "NULL")
{
womenMatches[currWoman] = currMan;
menMatches[currMan] = currWoman;
menPref[freeMenIndex].erase( menPref[freeMenIndex].begin() + 1);
freeMenCount -- ;
break;
}
else
{
string prevMan = womenMatches[currWoman] ;
if( willWomanChange(womenPref, currWoman, prevMan, currMan) )
{
menMatches[prevMan] = "NULL";
menMatches[currMan] = currWoman;
womenMatches[currWoman] = currMan;
menPref[freeMenIndex].erase( menPref[freeMenIndex].begin() + 1);
break;
}
else
{
menPref[freeMenIndex].erase( menPref[freeMenIndex].begin() + 1);
}
}
}
}
for(int i=0;i<n;i++)
{
vector<string> tempVec;
tempVec.push_back(menPref[i][0]);
tempVec.push_back(menMatches[menPref[i][0]]);
result.push_back(tempVec);
}
}
// Write a Stable Matching to file
void writePref(vector<vector<string> > &Match, ofstream &Fout)
{
for(int i=0; i<Match.size(); i++)
{
Fout<<Match[i][0]<<" - "<<Match[i][1]<<"\n";
}
}
// Main Function
int main(int argc, char const *argv[])
{
ifstream menFin,womenFin;
menFin.open("menPref.txt");
womenFin.open("womenPref.txt");
vector<vector<string> > menPref;
readPref(menPref, menFin);
vector<vector<string> > womenPref;
readPref(womenPref, womenFin);
// printPref(menPref);
// printPref(womenPref);
printCandidates(menPref,womenPref);
if(menPref.size() != womenPref.size())
{
cout<<"ERROR! Number of Men and Women should be equal\n";
}
else
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
default_random_engine rng(seed);
shuffle(begin(menPref), end(menPref), rng);
shuffle(begin(womenPref), end(womenPref), rng);
vector<vector<string> > result;
findMatching(menPref, womenPref, result);
cout<<"\nStable Matching:\n";
for(int i=0; i<result.size(); i++)
{
cout<<"\t"<<result[i][0]<<" - "<<result[i][1]<<endl;
}
ofstream fout;
fout.open("stableMatching.txt");
writePref(result,fout);
}
menFin.close();
womenFin.close();
return 0;
}