|
| 1 | +import java.util.List; |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Collections; |
| 4 | + |
| 5 | +class StableMarriage { |
| 6 | + |
| 7 | + /* |
| 8 | + * Use the stable marriage algorithm to find stable pairs from the |
| 9 | + * lists of men and women. |
| 10 | + */ |
| 11 | + public static void findStableMarriages(List<Woman> women, List<Man> men) { |
| 12 | + // We might have more men/women than women/men. In this case, not everybody can |
| 13 | + // get a mate. We should aim to give every member of the less numerous gender a mate, |
| 14 | + // as this is always possible. |
| 15 | + List<? extends Person> leastCommonGender = women.size() <= men.size() ? women : men; |
| 16 | + do { |
| 17 | + // Every single man proposes to a woman. |
| 18 | + for (Man man : men) |
| 19 | + if (man.isLonely()) |
| 20 | + man.propose(); |
| 21 | + |
| 22 | + // The women pick their favorite suitor. |
| 23 | + for (Woman woman : women) |
| 24 | + woman.chooseMate(); |
| 25 | + |
| 26 | + // End the process if everybody has a mate. |
| 27 | + if (!leastCommonGender.stream().anyMatch(Person::isLonely)) |
| 28 | + break; |
| 29 | + |
| 30 | + } while (true); |
| 31 | + |
| 32 | + women.forEach(w -> System.out.println(w + " married to " + w.getMate())); |
| 33 | + } |
| 34 | + |
| 35 | + public static void main(String[] args) { |
| 36 | + int nPairs = 5; |
| 37 | + List<Woman> women = new ArrayList<>(); |
| 38 | + List<Man> men = new ArrayList<>(); |
| 39 | + for (char i = 'A'; i < 'A' + nPairs; ++i) { |
| 40 | + women.add(new Woman("" + i)); |
| 41 | + men.add(new Man("" + i)); |
| 42 | + } |
| 43 | + // Make the genders unbalanced: |
| 44 | + women.add(new Woman("X")); |
| 45 | + |
| 46 | + women.forEach(w -> { |
| 47 | + w.receiveOptions(men); |
| 48 | + System.out.println(w + " prefers " + w.getPreferredMates()); |
| 49 | + }); |
| 50 | + men.forEach(m -> { |
| 51 | + m.receiveOptions(women); |
| 52 | + System.out.println(m + " prefers " + m.getPreferredMates()); |
| 53 | + }); |
| 54 | + |
| 55 | + findStableMarriages(women, men); |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +class Person { |
| 61 | + private final String name; |
| 62 | + protected Person mate; |
| 63 | + protected List<Person> preferredMates; |
| 64 | + |
| 65 | + public Person(String name) { |
| 66 | + this.name = name; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean isLonely() { |
| 70 | + return mate == null; |
| 71 | + } |
| 72 | + |
| 73 | + public void setMate(Person mate) { |
| 74 | + // Only set mates if there is a change. |
| 75 | + if (this.mate != mate) { |
| 76 | + // Remove old mates mate. |
| 77 | + if (this.mate != null) |
| 78 | + this.mate.mate = null; |
| 79 | + |
| 80 | + // Set the new mate. |
| 81 | + this.mate = mate; |
| 82 | + |
| 83 | + // If new mate is someone, update their mate. |
| 84 | + if (mate != null) |
| 85 | + mate.mate = this; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public Person getMate() { |
| 90 | + return mate; |
| 91 | + } |
| 92 | + |
| 93 | + public void receiveOptions(List<? extends Person> mates) { |
| 94 | + // Preferences are subjective. |
| 95 | + preferredMates = new ArrayList<>(mates); |
| 96 | + Collections.shuffle(preferredMates); |
| 97 | + } |
| 98 | + |
| 99 | + public List<Person> getPreferredMates() { |
| 100 | + return preferredMates; |
| 101 | + } |
| 102 | + |
| 103 | + public String toString() { |
| 104 | + return getClass().getName() + "(" + name + ")"; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class Woman extends Person { |
| 109 | + private List<Man> suitors = new ArrayList<>(); |
| 110 | + |
| 111 | + public Woman(String name) { |
| 112 | + super(name); |
| 113 | + } |
| 114 | + |
| 115 | + public void recieveProposal(Man suitor) { |
| 116 | + suitors.add(suitor); |
| 117 | + } |
| 118 | + |
| 119 | + public void chooseMate() { |
| 120 | + for (Person mostDesired : preferredMates) { |
| 121 | + if (mostDesired == mate || suitors.contains(mostDesired)) { |
| 122 | + setMate(mostDesired); |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +class Man extends Person { |
| 130 | + public Man(String name) { |
| 131 | + super(name); |
| 132 | + } |
| 133 | + |
| 134 | + public void propose() { |
| 135 | + if (!preferredMates.isEmpty()) { |
| 136 | + Woman fiance = (Woman) preferredMates.remove(0); |
| 137 | + fiance.recieveProposal(this); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments