|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +class Person |
| 5 | +{ |
| 6 | + private $name; |
| 7 | + private $suitors = []; |
| 8 | + private $preferences = []; |
| 9 | + private $match; |
| 10 | + |
| 11 | + public function __construct($name) |
| 12 | + { |
| 13 | + $this->name = $name; |
| 14 | + } |
| 15 | + |
| 16 | + public function getName(): string |
| 17 | + { |
| 18 | + return $this->name; |
| 19 | + } |
| 20 | + |
| 21 | + public function setPreferences(array $preferences): void |
| 22 | + { |
| 23 | + $this->preferences = $preferences; |
| 24 | + } |
| 25 | + |
| 26 | + public function getMatch(): ?Person |
| 27 | + { |
| 28 | + return $this->match; |
| 29 | + } |
| 30 | + |
| 31 | + public function getPreferences(): array |
| 32 | + { |
| 33 | + return $this->preferences; |
| 34 | + } |
| 35 | + |
| 36 | + public function isSingle(): bool |
| 37 | + { |
| 38 | + return $this->match === null; |
| 39 | + } |
| 40 | + |
| 41 | + public function unmatch(): void |
| 42 | + { |
| 43 | + $this->match = null; |
| 44 | + } |
| 45 | + |
| 46 | + public function setMatch(Person $match): void |
| 47 | + { |
| 48 | + if ($this->match !== $match) { |
| 49 | + if ($this->match !== null) { |
| 50 | + $this->match->unmatch(); |
| 51 | + } |
| 52 | + $this->match = $match; |
| 53 | + $match->setMatch($this); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function propose(): void |
| 58 | + { |
| 59 | + if (!empty($this->preferences)) { |
| 60 | + $fiance = array_shift($this->preferences); |
| 61 | + $fiance->receiveProposal($this); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public function receiveProposal(Person $man): void |
| 66 | + { |
| 67 | + $this->suitors[] = $man; |
| 68 | + } |
| 69 | + |
| 70 | + public function chooseMatch(): void |
| 71 | + { |
| 72 | + foreach ($this->preferences as $preference) { |
| 73 | + if ($preference === $this->match || in_array($preference, $this->suitors)) { |
| 74 | + $this->setMatch($preference); |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $this->suitors = []; |
| 80 | + } |
| 81 | + |
| 82 | + public function __toString(): string |
| 83 | + { |
| 84 | + return $this->name; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function stable_marriage(array $men, array $women): void |
| 89 | +{ |
| 90 | + do { |
| 91 | + foreach ($men as $man) { |
| 92 | + if ($man->isSingle()) { |
| 93 | + $man->propose(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + foreach ($women as $woman) { |
| 98 | + $woman->chooseMatch(); |
| 99 | + } |
| 100 | + |
| 101 | + $unmarried = false; |
| 102 | + foreach ($women as $woman) { |
| 103 | + if ($woman->isSingle()) { |
| 104 | + $unmarried = true; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + } while ($unmarried); |
| 110 | +} |
| 111 | + |
| 112 | +$groupSize = 10; |
| 113 | +$men = []; |
| 114 | +$women = []; |
| 115 | + |
| 116 | +for ($i = 1; $i <= $groupSize; $i++) { |
| 117 | + $men[] = new Person("M${i}"); |
| 118 | + $women[] = new Person("W${i}"); |
| 119 | +} |
| 120 | + |
| 121 | +foreach ($men as $man) { |
| 122 | + $preferences = $women; |
| 123 | + shuffle($preferences); |
| 124 | + $man->setPreferences($preferences); |
| 125 | + printf('%s\'s choices: %s', $man->getName(), implode(',', $man->getPreferences())); |
| 126 | + echo PHP_EOL; |
| 127 | +} |
| 128 | +echo PHP_EOL; |
| 129 | +foreach ($women as $woman) { |
| 130 | + $preferences = $men; |
| 131 | + shuffle($preferences); |
| 132 | + $woman->setPreferences($preferences); |
| 133 | + printf('%s\'s choices: %s', $woman->getName(), implode(',', $woman->getPreferences())); |
| 134 | + echo PHP_EOL; |
| 135 | +} |
| 136 | +echo PHP_EOL; |
| 137 | + |
| 138 | +stable_marriage($men, $women); |
| 139 | +foreach ($women as $woman) { |
| 140 | + printf('%s is married to %s', $woman, $woman->getMatch()); |
| 141 | + echo PHP_EOL; |
| 142 | +} |
0 commit comments