-
Notifications
You must be signed in to change notification settings - Fork 1k
/
stable_marriage_problem.py
60 lines (52 loc) · 1.47 KB
/
stable_marriage_problem.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
import numpy as np
def wPrefersM1OverM(prefer, w, m, m1):
for i in range(N):
if (prefer[w][i] == m1):
return True
if (prefer[w][i] == m):
return False
def stableMarriage(prefer):
wPartner = [-1 for i in range(N)]
mFree = [False for i in range(N)]
freeCount = N
while (freeCount > 0):
m = 0
while (m < N):
if mFree[m] == False:
break
m += 1
i = 0
while i < N and mFree[m] == False:
w = prefer[m][i]
if (wPartner[w - N] == -1):
wPartner[w - N] = m
mFree[m] = True
freeCount -= 1
else:
m1 = wPartner[w - N]
if (wPrefersM1OverM(prefer, w, m, m1) == False):
wPartner[w - N] = m
mFree[m] = True
mFree[m1] = False
i += 1
print("Woman ", " Man")
for i in range(N):
print(i + N, "\t", wPartner[i])
N = int(input("Enter the number of men/women: "))
print("Enter preferences:")
entries = list(map(int, input().split()))
prefer = np.array(entries).reshape(2*N, N)
stableMarriage(prefer)
"""
Time Complexity:O(n2)
Sample Input:
Enter the number of men/women: 4
Enter preferences: 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
Output:
Woman Man
4 2
5 1
6 3
7 0
"""