-
-
Notifications
You must be signed in to change notification settings - Fork 359
Implementation Stable marriage problem in PHP #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
eaaed03
6d0d05e
330eeab
8157121
7da77b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
abstract class Person | ||
{ | ||
private $name; | ||
protected $preferences = []; | ||
protected $match; | ||
|
||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setPreferences(array $preferences): void | ||
{ | ||
shuffle($preferences); | ||
$this->preferences = $preferences; | ||
} | ||
|
||
public function getMatch(): Person | ||
{ | ||
return $this->match; | ||
} | ||
|
||
public function getPreferences(): array | ||
{ | ||
return $this->preferences; | ||
} | ||
|
||
public function isSingle(): bool | ||
{ | ||
return $this->match === null; | ||
} | ||
|
||
public function unmatch(): void | ||
{ | ||
$this->match = null; | ||
} | ||
|
||
public function setMatch(Person $match): void | ||
{ | ||
if ($this->match !== $match) { | ||
if ($this->match !== null) { | ||
$this->match->unmatch(); | ||
} | ||
$this->match = $match; | ||
if ($match !== null) $match->setMatch($this); | ||
} | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
|
||
class Man extends Person | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few months ago, we had a discussion on Discord about whether we'd actually need 2 subclasses for |
||
{ | ||
public function propose(): void | ||
{ | ||
if (!empty($this->preferences)) { | ||
$fiance = array_shift($this->preferences); | ||
$fiance->receiveProposal($this); | ||
} | ||
} | ||
} | ||
|
||
class Woman extends Person | ||
{ | ||
private $suitors = []; | ||
|
||
public function receiveProposal(Man $man): void | ||
{ | ||
$this->suitors[] = $man; | ||
} | ||
|
||
public function chooseMatch(): void | ||
{ | ||
foreach ($this->preferences as $preference) { | ||
if ($preference === $this->match || in_array($preference, $this->suitors)) { | ||
$this->setMatch($preference); | ||
break; | ||
} | ||
} | ||
Butt4cak3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
function stable_marriage(array $men, array $women): void | ||
{ | ||
$smallerGroup = count($men) < count($women) ? $men : $women; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both arrays are the same size by definition. You don't have to check this. |
||
do { | ||
foreach ($men as $man) | ||
if ($man->isSingle()) $man->propose(); | ||
|
||
foreach ($women as $woman) | ||
$woman->chooseMatch(); | ||
|
||
if (empty(array_filter($smallerGroup, function (Person $person) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loops over all the objects even if the result is already determined. If the first person is single, then there is no need to loop over all the others. I recommend changing this to a |
||
return $person->isSingle(); | ||
}))) break; | ||
|
||
} while (true); | ||
|
||
foreach ($women as $woman) printf('%s is married to %s%s', $woman, $woman->getMatch(), PHP_EOL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the output out of the algorithm's function. |
||
} | ||
|
||
$groupSize = 10; | ||
$men = []; | ||
$women = []; | ||
|
||
for ($i = 1; $i <= $groupSize; $i++) { | ||
$men[] = new Man("M${i}"); | ||
$women[] = new Woman("W${i}"); | ||
} | ||
|
||
foreach ($men as $man) { | ||
$man->setPreferences($women); | ||
printf('%s\'s choices:%s', $man->getName(), PHP_EOL); | ||
printf('%s%s', implode(',', $man->getPreferences()), PHP_EOL); | ||
} | ||
echo PHP_EOL; | ||
foreach ($women as $woman) { | ||
$woman->setPreferences($men); | ||
printf('%s\'s choices:%s', $woman->getName(), PHP_EOL); | ||
printf('%s%s', implode(',', $woman->getPreferences()), PHP_EOL); | ||
} | ||
echo PHP_EOL; | ||
stable_marriage($men, $women); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this outside the
setPreferences
method.