Skip to content

Commit cbdac0e

Browse files
Merge pull request #529 from AnshShrivastava/master
Implemented Gale Shapley Algorithm in cpp
2 parents 04ca956 + 986ec04 commit cbdac0e

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

Matching/GaleShapley/galeshapley.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Algorithm to find stable matching of companies and students where companies and students both provide their preference list and all matchings are stable
2+
// Implementation of Gale-Shapley Algorithm
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
// Funtion to check if found match is stable or not
6+
bool check_pair(int student, int match, vector<int> stud_list, vector<int> com_list, unordered_map<int, int> &m) {
7+
int current = m[match];
8+
int index_cur;
9+
int index_stud;
10+
for (int i = 0; i < stud_list.size(); i++) {
11+
if (com_list[i] == current)
12+
index_cur = i;
13+
if (com_list[i] == student)
14+
index_stud = i;
15+
}
16+
if (index_stud>index_cur)
17+
return false;
18+
else return true;
19+
}
20+
// Function to find appropriate match for a student
21+
int find_pair(int student, vector<int> stud_list, vector<vector<int>> com_list, unordered_map<int, int> &m) {
22+
int i = 0;
23+
while (i<stud_list.size()){
24+
int match = stud_list[i];
25+
if (m.find(match) == m.end()) {
26+
return match;
27+
} else {
28+
if (check_pair(student, match, stud_list, com_list[match-1], m)) {
29+
m[match] = student;
30+
return match;
31+
} else {
32+
i++;
33+
}
34+
}
35+
}
36+
}
37+
// Function to implement Gale-Shapley Algorithm
38+
vector<pair<int, int>> Matcher(vector<vector<int>> stud_list, vector<vector<int>> com_list) {
39+
unordered_map<int, int> m;
40+
vector<pair<int, int>> matches;
41+
int i =0, j =0;
42+
int match;
43+
while(m.size()<stud_list.size()) {
44+
match = find_pair(i+1, stud_list[i], com_list, m);
45+
//cout << "debug " << i+1 << " " <<match << endl;
46+
m[match] = i+1;
47+
i = (i+1)%stud_list.size();
48+
}
49+
for (auto i = m.begin(); i != m.end(); i++) {
50+
matches.push_back(make_pair(i->second, i->first));
51+
}
52+
return matches;
53+
}
54+
// Comparator Function
55+
bool comp(pair<int, int> i, pair<int, int> j) {
56+
return (i.first<j.first);
57+
}
58+
int main() {
59+
//initializing the preference list
60+
vector<vector<int>> stud_list;
61+
vector<vector<int>> com_list;
62+
vector<int> temp;
63+
int tem;
64+
int no_stud, no_com;
65+
cout << "Enter no. of students and companies\n";
66+
cin >> no_stud >> no_com;
67+
cout << "Enter preference list of students\n";
68+
for (int i = 0; i< no_stud; i++) {
69+
for(int j =0; j<no_com; j++) {
70+
cin >> tem;
71+
temp.push_back(tem);
72+
}
73+
stud_list.push_back(temp);
74+
temp.clear();
75+
}
76+
cout << "Enter preference list of companies\n";
77+
for (int i = 0; i< no_com; i++) {
78+
for(int j =0; j<no_stud; j++) {
79+
cin >> tem;
80+
temp.push_back(tem);
81+
}
82+
com_list.push_back(temp);
83+
temp.clear();
84+
}
85+
cout << "Placing students...\n";
86+
vector<pair<int, int>> matches(no_stud, make_pair(0, 0));
87+
matches = Matcher(stud_list, com_list);
88+
sort(matches.begin(), matches.end(), comp);
89+
// Printing the Matched students
90+
for (auto i = matches.begin(); i != matches.end();i++) {
91+
cout << "Student: " << i->first << " is placed in " << "Company: " <<i->second <<endl;
92+
}
93+
}

0 commit comments

Comments
 (0)