Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Stack;
- public class StableMatching implements StableMatchingInterface {
- public int[][] M;
- StableMatching() {
- this.M = null;
- }
- StableMatching(int[] menGroupCount, int[] womenGroupCount, int[][] menPrefs, int[][] womenPrefs) {
- this.M = constructStableMatching(menGroupCount, womenGroupCount, menPrefs, womenPrefs);
- }
- /*
- * public int[][] constructStableMatchingC(int[] menGroupCount, int[]
- * womenGroupCount, int[][] menPrefs, int[][] womenPrefs) { int m =
- * menGroupCount.length; int w = womenGroupCount.length; int[][] M = new
- * int[m][w]; int nb_men = 0; for (int i = 0; i < m; i++) nb_men +=
- * menGroupCount[i]; int[] indexWomen = new int[nb_men]; LinkedList<Integer> men
- * = new LinkedList<Integer>(); int[] WomenGroom = new int[nb_men]; for (int i =
- * 0; i < nb_men; i++) { men.add(i); indexWomen[i] = 0; WomenGroom[i] = -1; }
- *
- * int[][] positionGroom = new int[w][m]; for (int i = 0; i < w; i++) { for (int
- * j = 0; j < m; j++) { positionGroom[i][womenPrefs[i][j]] = j; } } while
- * (!(men.isEmpty())) { int g = men.poll(); int f = menPrefs[g][indexWomen[g]];
- * indexWomen[g] += 1; if (WomenGroom[f] == -1) { M[g][f] = 1; WomenGroom[f] =
- * g; } else { int g2 = WomenGroom[f]; if (positionGroom[f][g] <
- * positionGroom[f][g2]) { men.addFirst(g2); WomenGroom[f] = g; M[g][f] = 1;
- * M[g2][f] = 0; } else { men.addFirst(g); }
- *
- * }
- *
- * } return M;
- *
- * }
- *
- * public int[][] constructStableMatchingB(int[] menGroupCount, int[]
- * womenGroupCount, int[][] menPrefs, int[][] womenPrefs) { int m =
- * menGroupCount.length; int w = womenGroupCount.length; if (m == 0) return new
- * int[0][0]; int nb_men = 0; for (int i = 0; i < m; i++) nb_men +=
- * menGroupCount[i]; int[] menGroupCount2 = new int[nb_men]; int[]
- * womenGroupCount2 = new int[nb_men]; int[][] womenPrefs2 = new
- * int[nb_men][nb_men]; int[][] menPrefs2 = new int[nb_men][nb_men]; for (int i
- * = 0; i < nb_men; i++) { womenGroupCount2[i] = 1; menGroupCount2[i] = 1; }
- * int[] indices_h = new int[m]; // tableau donnant les indices des groupes
- * d'hommes int[] indices_f = new int[w]; indices_h[0] = 0; indices_f[0] = 0;
- * for (int i = 1; i < m; i++) { indices_h[i] = menGroupCount[i - 1] +
- * indices_h[i - 1]; } for (int i = 1; i < w; i++) { indices_f[i] =
- * womenGroupCount[i - 1] + indices_f[i - 1]; } for (int i = 0; i < w; i++) {
- * for (int j = 0; j < womenGroupCount[i]; j++) { int b = 0; for (int l = 0; l <
- * m; l++) { int a = womenPrefs[i][l]; int indice = indices_h[a]; for (int z =
- * 0; z < menGroupCount[a]; z++) womenPrefs2[j + indices_f[i]][z + b] = indice +
- * z; b += menGroupCount[l]; } } } for (int i = 0; i < w; i++) { for (int j = 0;
- * j < menGroupCount[i]; j++) { int b = 0; for (int l = 0; l < m; l++) { int a =
- * menPrefs[i][l]; int indice = indices_f[a]; for (int z = 0; z <
- * womenGroupCount[a]; z++) menPrefs2[j + indices_h[i]][z + b] = indice + z; b
- * += womenGroupCount[l]; } } } int[][] M =
- * constructStableMatchingC(menGroupCount2, womenGroupCount2, menPrefs2,
- * womenPrefs2); int[][] N = new int[m][w]; for (int i = 0; i < m; i++) { for
- * (int j = 0; j < menGroupCount[i]; j++) { for (int l = 0; l < w; l++) { for
- * (int z = 0; z < womenGroupCount[l]; z++) N[i][l] += M[j + indices_h[i]][z +
- * indices_f[l]]; }
- *
- * } } return N; }
- */
- public int[][] constructStableMatching(int[] menGroupCount, int[] womenGroupCount, int[][] menPrefs,
- int[][] womenPrefs) {
- int m = menGroupCount.length;
- int w = womenGroupCount.length;
- int[][] M = new int[m][w];
- if (w == 0 && m == 0)
- return M;
- int[] indexWomen = new int[m]; // indique l'indice des groupes de femmes auxquels chaque groupe d'homme doit
- // s'intéresser par la suite
- int[][] positionGroom = new int[w][m]; // indique le rang de chaque groupe d'homme dans les préférences de
- // chaque groupe de femmes
- int[] worseMen = new int[w]; // indique le rang du pire mari pour chaque groupe de femmes ; par défaut, m pour des femmes célibataires
- int[][] womenGroom = new int[w][m + 1]; // donne le nombre de femmes du groupe i mariées au groupe j (on ajoute
- // une colonne supplémentaire pour les femmes célibataires)
- int[] menCelibs = new int[m]; // donne le nombre de célibataires restant par groupe d'hommes
- int total_unengaged = 0;
- Stack<Integer> men = new Stack<Integer>();
- for (int i = 0; i < w; i++) {
- worseMen[i] = m;
- womenGroom[i][m] = womenGroupCount[i]; // toutes les femmes sont initialement célibataires
- }
- for (int j = 0; j < m; j++) {
- menCelibs[j] = menGroupCount[j];
- indexWomen[j] = 0;
- total_unengaged += menGroupCount[j];
- }
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < m; j++) {
- positionGroom[i][womenPrefs[i][j]] = j;
- womenGroom[i][j]=0;
- }
- }
- for (int i = 0; i < m; i++) {
- if (menCelibs[i] > total_unengaged / (2 * m))
- men.push(i);
- }
- int tmp = men.pop();
- while (total_unengaged > 0) {
- int groupf = menPrefs[tmp][indexWomen[tmp]];
- int rank = worseMen[groupf];
- int groupMen2 = -1;
- if (rank != m)
- groupMen2 = womenPrefs[groupf][rank];
- else
- groupMen2 = m;
- if (rank > positionGroom[groupf][tmp]) {
- int newMarriages = Math.min(menCelibs[tmp], womenGroom[groupf][groupMen2]);
- womenGroom[groupf][tmp] += newMarriages;
- womenGroom[groupf][groupMen2] -= newMarriages;
- menCelibs[tmp] -= newMarriages;
- if (groupMen2 != m)
- menCelibs[groupMen2] += newMarriages;
- if (groupMen2 == m) {
- total_unengaged -= newMarriages;
- }
- if (womenGroom[groupf][groupMen2] == 0) {
- rank--;
- while (womenGroom[groupf][womenPrefs[groupf][rank]] == 0)// on actualise le pire mari pour groupf
- // // groupf
- rank--;
- worseMen[groupf] = rank;
- }
- } else
- indexWomen[tmp]++;
- if (men.isEmpty() && total_unengaged > 0) { // on remplit la pile avec les hommes dont le groupe possède une
- // fraction suffisante de célibataires
- for (int i = 0; i < m; i++) {
- if (menCelibs[i] > total_unengaged / (2 * m))
- men.push(i);
- }
- }
- if (!(men.isEmpty()))
- tmp = men.pop();
- }
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < w; j++)
- M[i][j] = womenGroom[j][i];
- }
- return M;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement