-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable marriage.py
220 lines (204 loc) · 9.76 KB
/
stable marriage.py
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
class stable_matching:
def __init__(self):
self.state = None
self.pairs = []
self.boys = []
self.girls = []
self.phase1_ready = False
self.phase2_ready = False
self.unmatched_boys = []
self.unmatched_girls = []
self.is_girl_optimal = False
def __str__(self):
if self.is_girl_optimal:
if self.phase1_ready == False:
return "You should first call .get_boys_girls() function.\n"
elif self.phase2_ready == False:
return "You should call .stable_matcher(is_girl_optimal=False or True) function.\n"
else:
output = str()
output += "\nIn girl optimal mode:\n"
sorted_list = sorted(self.pairs, key=lambda x: x[1])
output += "Matched pairs:\n"
for pair in sorted_list:
output += f"girl{pair[1]} is mathced with boy{pair[0]}.\n"
if self.state == 1:
output += "Unmatched girls:\n"
self.unmatched_girls.sort()
for girl in self.unmatched_girls:
output += f"girl{girl}\n"
elif self.state == 2:
self.unmatched_boys.sort()
output += "Unmatched boys:\n"
for boy in self.unmatched_boys:
output += f"boy{boy}\n"
return output
else:
if self.phase1_ready == False:
return "You should first call .get_boys_girls() function.\n"
elif self.phase2_ready == False:
return "You should call .stable_matcher(is_girl_optimal=False or True) function.\n"
else:
output = str()
output += "\nIn boy optimal mode:\n"
sorted_list = sorted(self.pairs, key=lambda x: x[0])
output += "Matched pairs:\n"
for pair in sorted_list:
output += f"boy{pair[0]} is mathced with girl{pair[1]}.\n"
if self.state == 1:
output += "Unmatched girls:\n"
self.unmatched_girls.sort()
for girl in self.unmatched_girls:
output += f"girl{girl}\n"
elif self.state == 2:
self.unmatched_boys.sort()
output += "Unmatched boys:\n"
for boy in self.unmatched_boys:
output += f"boy{boy}\n"
return output
def get_boys_girls(self):
boys_num = int(input("What is the number of boys? "))
girls_num = int(input("What is the number of girls? "))
for boy in range(boys_num):
boy_prefences = list(
map(
int,
input(
f"Type {girls_num} girls based on the preferences of the boy{boy} from left to right and separate each one with a space: "
).split(),
)
)
self.boys.append(boy_prefences)
for girl in range(girls_num):
girl_prefences = list(
map(
int,
input(
f"Type {boys_num} boys based on the preferences of the girl{girl} from left to right and separate each one with a space: "
).split(),
)
)
self.girls.append(girl_prefences)
if boys_num == girls_num:
self.state = 0
elif boys_num < girls_num:
self.state = 1
else:
self.state = 2
self.phase1_ready = True
self.phase2_ready = False
def find_girl_pair(self, girl):
for pair in self.pairs:
if pair[1] == girl:
return pair[0]
def find_boy_pair(self, boy):
for pair in self.pairs:
if pair[0] == boy:
return pair[1]
def stable_matcher(self, is_girl_optimal=False):
self.is_girl_optimal = is_girl_optimal
self.pairs = []
self.unmatched_boys = [index for index in range(len(self.boys))]
next_index_boys_request = [0 for _ in range(len(self.boys))]
self.unmatched_girls = [index for index in range(len(self.girls))]
next_index_girls_request = [0 for _ in range(len(self.girls))]
if is_girl_optimal:
if self.state == 0 or self.state == 2:
while len(self.unmatched_girls):
current_girl = self.unmatched_girls.pop()
for best_boy in self.girls[current_girl][
next_index_girls_request[current_girl] :
]:
next_index_girls_request[current_girl] += 1
if best_boy in self.unmatched_boys:
self.pairs.append([best_boy, current_girl])
self.unmatched_boys.remove(best_boy)
break
else:
matched_with_best_boy = self.find_boy_pair(best_boy)
if self.boys[best_boy].index(current_girl) < self.boys[
best_boy
].index(matched_with_best_boy):
self.pairs.remove([best_boy, matched_with_best_boy])
self.pairs.append([best_boy, current_girl])
self.unmatched_girls.append(matched_with_best_boy)
break
self.phase2_ready = True
else:
unwanted_girls = []
while len(unwanted_girls) != len(self.girls) - len(self.boys):
current_girl = self.unmatched_girls.pop()
for best_boy in self.girls[current_girl][
next_index_girls_request[current_girl] :
]:
next_index_girls_request[current_girl] += 1
if best_boy in self.unmatched_boys:
self.pairs.append([best_boy, current_girl])
self.unmatched_boys.remove(best_boy)
break
else:
matched_with_best_boy = self.find_boy_pair(best_boy)
if self.boys[best_boy].index(current_girl) < self.boys[
best_boy
].index(matched_with_best_boy):
self.pairs.remove([best_boy, matched_with_best_boy])
self.pairs.append([best_boy, current_girl])
self.unmatched_girls.append(matched_with_best_boy)
break
if best_boy == self.girls[current_girl][-1]:
unwanted_girls.append(current_girl)
self.unmatched_girls = unwanted_girls
self.phase2_ready = True
else:
if self.state == 0 or self.state == 1:
while len(self.unmatched_boys):
current_boy = self.unmatched_boys.pop()
for best_girl in self.boys[current_boy][
next_index_boys_request[current_boy] :
]:
next_index_boys_request[current_boy] += 1
if best_girl in self.unmatched_girls:
self.pairs.append([current_boy, best_girl])
self.unmatched_girls.remove(best_girl)
break
else:
matched_with_best_girl = self.find_girl_pair(best_girl)
if self.girls[best_girl].index(current_boy) < self.girls[
best_girl
].index(matched_with_best_girl):
self.pairs.remove([matched_with_best_girl, best_girl])
self.pairs.append([current_boy, best_girl])
self.unmatched_boys.append(matched_with_best_girl)
break
self.phase2_ready = True
else:
unwanted_boys = []
while len(unwanted_boys) != len(self.boys) - len(self.girls):
current_boy = self.unmatched_boys.pop()
for best_girl in self.boys[current_boy][
next_index_boys_request[current_boy] :
]:
next_index_boys_request[current_boy] += 1
if best_girl in self.unmatched_girls:
self.pairs.append([current_boy, best_girl])
self.unmatched_girls.remove(best_girl)
break
else:
matched_with_best_girl = self.find_girl_pair(best_girl)
if self.girls[best_girl].index(current_boy) < self.girls[
best_girl
].index(matched_with_best_girl):
self.pairs.remove([matched_with_best_girl, best_girl])
self.pairs.append([current_boy, best_girl])
self.unmatched_boys.append(matched_with_best_girl)
break
if best_girl == self.boys[current_boy][-1]:
unwanted_boys.append(current_boy)
self.unmatched_boys = unwanted_boys
self.phase2_ready = True
s1 = stable_matching()
s1.get_boys_girls()
s1.stable_matcher()
print(s1)
s1.stable_matcher(is_girl_optimal=True)
print(s1)