forked from amitKr85/project_student_allocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable marriage algo.py
46 lines (40 loc) · 1.28 KB
/
stable marriage algo.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
def stable_marriage(prefer):
people_count = len(prefer)
print("people count",people_count)
free_men = people_count//2
couples = [-1 for i in range(people_count)]
while free_men > 0:
m = -1
for i in range(people_count//2):
if couples[i] == -1:
m = i
break
for w in prefer[m]:
if couples[w] == -1:
couples[m] = w
couples[w] = m
free_men -= 1
break
else:
for prefer_men in prefer[w]:
if prefer_men == m:
couples[ couples[w] ] = -1
couples[w] = m
couples[m] = w
break
elif prefer_men == couples[w]:
break
for i in range(people_count//2):
print("men",i,"engaged to",couples[i],"women")
def main():
prefer = [[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]]
stable_marriage(prefer)
if __name__ == "__main__":
main()