-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMaximum Matching - Gale–Shapley algorithm
93 lines (79 loc) · 3.06 KB
/
Maximum Matching - Gale–Shapley algorithm
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
// "static void main" must be defined in a public class.
public class Main {
//Number of men and women
static int N = 4;
public static void main(String[] args) {
int prefer[][] = new int[][]{
{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}};
stableMarriage(prefer);
}
static void stableMarriage(int[][] prefer){
// Hold the partner for every woman
int partner[] = new int[N];
// Hold flag if the man is engaged or not
boolean engaged[] = new boolean[N];
//No partner intially
Arrays.fill(partner,-1);
//Intially there are N free Men
int freeMen = N;
//Loop till there are no free man left
while(freeMen > 0){
//Find the first free man
int man = 0;
for(;man<N;man++)
if(engaged[man] == false)
break;
//loop on all the women for that man
for(int i=0; i < N; i++ ){
//Finding the priority for the woman
int womenPrefer = prefer[man][i];
// If the selected woman has no partner than enagage
// man and womenPrefer
if(partner[womenPrefer-N] == -1){
partner[womenPrefer-N] = man;
engaged[man] = true;
freeMen--;
}
// If the woman has partner already than,
// find if the priority of man is less than current partner
// If yes, change the partner of women to man and
// update the enagaged status of current man to false
else{
int currentPartner = partner[womenPrefer-N]; if(doesWomenPreferMenOverCurrentPartner(prefer,womenPrefer,man,currentPartner)){
partner[womenPrefer-N] = man;
engaged[man]=true;
engaged[currentPartner]=false;
}
}
// if the man is enagaged than break the loop
if(engaged[man] == true ){
break;
}
}
}
//Print the woman and man relationship
System.out.println("Women ---> Men");
for(int i=0;i<N;i++){
System.out.println(N+i+" ---> "+partner[i]);
}
}
// Find if the womean prefer new man to current partner or not.
static boolean doesWomenPreferMenOverCurrentPartner(int[][] prefer, int women, int newMan, int currentMan){
for(int i=0;i<N;i++){
if(prefer[women][i] == currentMan){
return false;
}
if(prefer[women][i] == newMan){
return true;
}
}
return true;
}
}