-
Notifications
You must be signed in to change notification settings - Fork 0
/
GS.java
220 lines (206 loc) · 7.53 KB
/
GS.java
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
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author mihirkavatkar
*/
public class GS {
public void reverse(String m[],String newm[],String manpref[][])
{
int j=0,i,a=0,b=0;
for(i = m.length-1; i>=0;i--)
{
newm[j] = m[i];
//System.out.println(newm[j]);
j++;
}
for (i = 0; i < manpref.length/2; i++)
{
for (j = 0; j < manpref[i].length; j++)
{
String tmp = manpref[i][j];
manpref[i][j] = manpref[manpref.length - i -1][j];
manpref[manpref.length - i -1][j] = tmp;
}
}
}
public int getmanpos(String man[],String str,int N)
{
int i;
for(i=0;i<N;i++)
if(man[i].equals(str))
return i;
return -1;
}
public int getwomanpos(String woman[],String str,int N)
{ int i;
for(i=0;i<N;i++)
if(woman[i].equals(str))
return i;
return -1;
}
public boolean nextpref(String womanpref[][],String currentP,String newP,int pos,int N)
{
int i;
for(i=0;i<N;i++)
{
if(womanpref[pos][i].equals(newP))
{
return true;
}
if(womanpref[pos][i].equals(currentP))
{
return false;
}
}
return false;
}
public void match(String m[],String w[],String manpref[][],String womanpref[][])
{
int engagecount = 0,i=0,j=0,f;
int CN = m.length;
int N = m.length;
String man[] = m;
String woman[] = w;
String manpreflist[][] = manpref;
String womanpreflist[][] = womanpref;
boolean manEngagelist[] = new boolean[CN];
String womanEngagelist[] = new String[CN];
boolean first = false;
while(engagecount<N)
{
for(f=0;f<N;f++)
{
if(manEngagelist[f]==false)
{ break;}
}
for(j=0;j<N && manEngagelist[f]==false;j++)
{
int pos=getwomanpos(woman,manpreflist[f][j],N);
if(womanEngagelist[pos]==null)
{
womanEngagelist[pos] = man[f];
manEngagelist[f] = true;
engagecount++;
}
else
{
String currentP = womanEngagelist[pos];
if(nextpref(womanpreflist,currentP,man[f],pos,N))
{
int nP=0;
womanEngagelist[pos]=man[f];
manEngagelist[f] = true;
manEngagelist[getmanpos(man,currentP,N)] = false;
}
}
}
}
System.out.println(" ");
System.out.println("Output:");
System.out.println("Engaged Couples:");
System.out.println(" ");
for(i=0;i<N;i++)
{
System.out.println(womanEngagelist[i]+" "+woman[i]);
}
}
public void woman_to_man(String nm[], String nw[],String nmanpref[][], String nwomanpref[][])
{
GS g = new GS();
String w[] = nm;
String m[] = nw;
String manpref[][] = nwomanpref;
String womanpref[][] = nmanpref;
g.match(m,w,manpref,womanpref);
}
public static void main(String args[])
{
GS g = new GS();
int r;
Scanner sc = new Scanner(System.in);
System.out.println("Gale-Shapley Algorithm");
String m[] = {"Victor", "Wyatt", "Xavier", "Yancey", "Zeus"};
String newm[];
newm = new String[5];
String new_manpref[][] = new String[5][5];
String w[] = {"Amy", "Bertha", "Clare", "Diane", "Erika"};
String manpref[][] = {{"Bertha", "Amy", "Diane", "Erika", "Clare"},
{"Diane", "Bertha", "Amy", "Clare", "Erika"},
{"Bertha", "Erika", "Clare", "Diane", "Amy"},
{"Amy", "Diane", "Clare", "Bertha", "Erika"},
{"Bertha", "Diane", "Amy", "Erika", "Clare"}
};
String womanpref[][] = {{"Zeus", "Victor", "Wyatt", "Yancey", "Xavier"},
{"Xavier", "Wyatt", "Yancey", "Victor", "Zeus"},
{"Wyatt", "Xavier", "Yancey", "Zeus", "Victor"},
{"Victor", "Zeus", "Yancey", "Xavier", "Wyatt"},
{"Yancey", "Wyatt", "Zeus", "Xavier", "Victor"}
};
System.out.println("Execution Options:");
System.out.println("Press 1: Start from Victor to Zeus");
System.out.println("Press 2: Start from Zeus to Victor");
System.out.println("Press 3: Woman proposing mens: Amy to Erika");
System.out.println("Press 4: Execute All the above options together using Threads Efficiently!!!");
r = sc.nextInt();
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
g.reverse(m,newm,manpref);
g.match(newm,w,manpref,womanpref);
}
});
Thread thread1 = new Thread(new Runnable()
{
@Override
public void run()
{
g.woman_to_man(m, w, manpref, womanpref);
}
});
switch(r)
{
case 1:
g.match(m,w,manpref,womanpref);
break;
case 2:
g.reverse(m,newm,manpref);
g.match(newm,w,manpref,womanpref);
break;
case 3:
g.woman_to_man(m, w, manpref, womanpref);
break;
case 4:
System.out.println("Victor to Zeus");
g.match(m,w,manpref,womanpref);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(GS.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" ");
System.out.println("Zeus to Victor");
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(GS.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" ");
System.out.println("Amy to Erika");
thread1.start();
break;
default:
System.out.println("Wrong Choice");
break;
}
}
}