-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
106 lines (95 loc) · 4.45 KB
/
main.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
// Armando and El
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number of Programmers & Companies: ");
int pro = s.nextInt();
int[][] programerPreferences = new int[pro][pro];
int[][] companyPreferences = new int[pro][pro];
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; //these are the lables of the companies
// the input preferences for programmer
for(int i = 0; i < pro; i++){
System.out.println("put pref for programmer " + (i+1) + " (enter " + pro + " values):");
for(int j = 0; j < pro; j++){
programerPreferences[i][j] = s.nextInt();
}
}
// the input preferences for companies
for(int x = 0; x < pro; x++){
System.out.println("put pref for company " + alphabet[x] + " (enter " + pro + " values, where a=0, b=1, c=2, etc.):");
for(int y = 0; y < pro; y++){
companyPreferences[x][y] = s.nextInt();
}
}
s.close();
int[] matches = match(programerPreferences, companyPreferences);
// displaying the prefrences
for (int i = 0; i < pro; i++) {
System.out.println((i+1) + Arrays.toString(programerPreferences[i]) + " " + alphabet[i] + Arrays.toString(companyPreferences[i]));
}
// thiw is the matching results
System.out.println("\nMatching results:");
for (int i = 0; i < matches.length; i++) {
System.out.println("Programmer " + (matches[i] + 1) + " matched with Company " + alphabet[i]);
}
}
// the matching algorithm
static int[] match(int[][] programerPreferences, int[][] companyPreferences) {
int size = programerPreferences.length;
Map<Integer, Integer> matches = new HashMap<>();
boolean[] programersFree = new boolean[size];
boolean[] companiesFree = new boolean[size];
Arrays.fill(programersFree, true);
Arrays.fill(companiesFree, true);
while (matches.size() < size) {
for (int i = 0; i < size; i++) {
if (programersFree[i]) {
//the programmer i ask to their next preferred company
for (int j = 0; j < size; j++) {
int company = programerPreferences[i][j];
if (companiesFree[company]) {
// the company if avalible, they accept the proposal
matches.put(company, i);
programersFree[i] = false;
companiesFree[company] = false;
break;
} else {
// the company already has a programmer, they will see if they prefer the new one
int currentProgrammer = matches.get(company);
boolean preferNew = checkMatch(currentProgrammer, i, company, companyPreferences);
if (preferNew) {
//the company switches to the new programmer if it is a better choice
matches.put(company, i);
programersFree[i] = false;
programersFree[currentProgrammer] = true;
break;
}
}
}
}
}
}
int[] matchedPairs = new int[size];
for (Map.Entry<Integer, Integer> entry : matches.entrySet()) {
matchedPairs[entry.getKey()] = entry.getValue();
}
return matchedPairs;
}
// it will check if the company wants the new programmer over the last pick
static boolean checkMatch(int currentProgrammer, int newProgrammer, int company, int[][] companyPreferences) {
for (int i = 0; i < companyPreferences[company].length; i++) {
if (companyPreferences[company][i] == newProgrammer) {
return true; // new programmer is picked over the last one
}
if (companyPreferences[company][i] == currentProgrammer) {
return false; // if the current programmer is picked
}
}
return false;
}
}