|
| 1 | +// "static void main" must be defined in a public class. |
| 2 | +public class Main { |
| 3 | + |
| 4 | + //Number of men and women |
| 5 | + static int N = 4; |
| 6 | + public static void main(String[] args) { |
| 7 | + int prefer[][] = new int[][]{ |
| 8 | + {7, 5, 6, 4}, |
| 9 | + {5, 4, 6, 7}, |
| 10 | + {4, 5, 6, 7}, |
| 11 | + {4, 5, 6, 7}, |
| 12 | + {0, 1, 2, 3}, |
| 13 | + {0, 1, 2, 3}, |
| 14 | + {0, 1, 2, 3}, |
| 15 | + {0, 1, 2, 3}}; |
| 16 | + stableMarriage(prefer); |
| 17 | + } |
| 18 | + |
| 19 | + static void stableMarriage(int[][] prefer){ |
| 20 | + |
| 21 | + // Hold the partner for every woman |
| 22 | + int partner[] = new int[N]; |
| 23 | + |
| 24 | + // Hold flag if the man is engaged or not |
| 25 | + boolean engaged[] = new boolean[N]; |
| 26 | + |
| 27 | + //No partner intially |
| 28 | + Arrays.fill(partner,-1); |
| 29 | + |
| 30 | + //Intially there are N free Men |
| 31 | + int freeMen = N; |
| 32 | + |
| 33 | + //Loop till there are no free man left |
| 34 | + while(freeMen > 0){ |
| 35 | + |
| 36 | + //Find the first free man |
| 37 | + int man = 0; |
| 38 | + for(;man<N;man++) |
| 39 | + if(engaged[man] == false) |
| 40 | + break; |
| 41 | + |
| 42 | + //loop on all the women for that man |
| 43 | + for(int i=0; i < N; i++ ){ |
| 44 | + |
| 45 | + //Finding the priority for the woman |
| 46 | + int womenPrefer = prefer[man][i]; |
| 47 | + |
| 48 | + // If the selected woman has no partner than enagage |
| 49 | + // man and womenPrefer |
| 50 | + if(partner[womenPrefer-N] == -1){ |
| 51 | + partner[womenPrefer-N] = man; |
| 52 | + engaged[man] = true; |
| 53 | + freeMen--; |
| 54 | + } |
| 55 | + // If the woman has partner already than, |
| 56 | + // find if the priority of man is less than current partner |
| 57 | + // If yes, change the partner of women to man and |
| 58 | + // update the enagaged status of current man to false |
| 59 | + else{ |
| 60 | + int currentPartner = partner[womenPrefer-N]; if(doesWomenPreferMenOverCurrentPartner(prefer,womenPrefer,man,currentPartner)){ |
| 61 | + partner[womenPrefer-N] = man; |
| 62 | + engaged[man]=true; |
| 63 | + engaged[currentPartner]=false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // if the man is enagaged than break the loop |
| 68 | + if(engaged[man] == true ){ |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + //Print the woman and man relationship |
| 75 | + System.out.println("Women ---> Men"); |
| 76 | + for(int i=0;i<N;i++){ |
| 77 | + System.out.println(N+i+" ---> "+partner[i]); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Find if the womean prefer new man to current partner or not. |
| 82 | + static boolean doesWomenPreferMenOverCurrentPartner(int[][] prefer, int women, int newMan, int currentMan){ |
| 83 | + for(int i=0;i<N;i++){ |
| 84 | + if(prefer[women][i] == currentMan){ |
| 85 | + return false; |
| 86 | + } |
| 87 | + if(prefer[women][i] == newMan){ |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + return true; |
| 92 | + } |
| 93 | +} |
0 commit comments