forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stable_matching.java
121 lines (116 loc) · 2.2 KB
/
Stable_matching.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
import java.util.*;
public class stable{
int [][] men= new int[50][50];
int [][] women= new int[50][50];
int [] mMark=new int[50];
int [] wc=new int[50];
int [] mc=new int[50];
Scanner o=new Scanner(System.in);
public void womenpref(int n)
{
System.out.println(" enter the women preferne list");
for(int i=1;i<=n;i++)
{
System.out.println("enter the prefreence of"+i+"women");
for(int j=1;j<=n;j++)
{
System.out.println("enter the"+j+" prefreence of"+i+"women");
women[i][j]=o.nextInt();
}
}
}
public void printwomen(int n)
{
System.out.println("women prefference list");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
System.out.print("\t"+women[i][j]);
}
System.out.println("\n");
}
}
public void menpref(int n)
{
System.out.println(" enter the men preferne list");
for(int i=1;i<=n;i++)
{
System.out.println("enter the prefreence of"+i+"men");
for(int j=1;j<=n;j++)
{
System.out.println("enter the"+j+" prefreence of"+i+"men");
men[i][j]=o.nextInt();
}
}
}
public void printmen(int n)
{
System.out.println("men prefference list");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
System.out.print("\t"+men[i][j]);
}
System.out.println("\n");
}
}
public boolean wPrefersMOverM1(int w,int m,int m1,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(women[w][i]==m)
return true;
else if(women[w][i]==m1)
return false;
}
return false;
}
public void stable(int n)
{
int i,fc;
for(i=1;i<=n;i++)
{
mMark[i]=0;
wc[i]=-1;
}
fc=n;
while(fc>0)
{
int m;
for(m=1;m<=n;m++)
if(mMark[m]==0)
break;
for(i=1;i<=n&& mMark[m]==0;i++)
{
int w=men[m][i];
if(wc[w]==-1)
{
wc[w]=m;
mMark[m]=1;
fc--;
}
else
{
int m1=wc[w];
if(wPrefersMOverM1(w,m,m1,n))
{
wc[w]=m; mMark[m]=1;
mMark[m1]=0;
}
}
}
}
}
public void afterstable(int n)
{
for(int i=1;i<=n;i++)
{
mc[wc[i]]=i;
}
for(int i=1;i<=n;i++)
System.out.println(" "+i+" is paired with"+mc[i]);
}
}