-
-
Notifications
You must be signed in to change notification settings - Fork 360
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 4 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,144 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
class Person | ||
{ | ||
private $name; | ||
private $suitors = []; | ||
private $preferences = []; | ||
private $match; | ||
|
||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setPreferences(array $preferences): void | ||
{ | ||
$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; | ||
$match->setMatch($this); | ||
} | ||
} | ||
|
||
public function propose(): void | ||
{ | ||
if (!empty($this->preferences)) { | ||
$fiance = array_shift($this->preferences); | ||
$fiance->receiveProposal($this); | ||
} | ||
} | ||
|
||
public function receiveProposal(Person $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; | ||
} | ||
} | ||
|
||
$this->suitors = []; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
|
||
function stable_marriage(array $men, array $women): array | ||
{ | ||
do { | ||
foreach ($men as $man) { | ||
if ($man->isSingle()) { | ||
$man->propose(); | ||
} | ||
} | ||
|
||
foreach ($women as $woman) { | ||
$woman->chooseMatch(); | ||
} | ||
|
||
$unmarried = false; | ||
foreach ($women as $woman) { | ||
if ($woman->isSingle()) { | ||
$unmarried = true; | ||
break; | ||
} | ||
} | ||
|
||
} while ($unmarried); | ||
|
||
return $women; | ||
} | ||
|
||
$groupSize = 10; | ||
$men = []; | ||
$women = []; | ||
|
||
for ($i = 1; $i <= $groupSize; $i++) { | ||
$men[] = new Person("M${i}"); | ||
$women[] = new Person("W${i}"); | ||
} | ||
|
||
foreach ($men as $man) { | ||
$preferences = $women; | ||
shuffle($preferences); | ||
$man->setPreferences($preferences); | ||
printf('%s\'s choices: %s', $man->getName(), implode(',', $man->getPreferences())); | ||
echo PHP_EOL; | ||
} | ||
echo PHP_EOL; | ||
foreach ($women as $woman) { | ||
$preferences = $men; | ||
shuffle($preferences); | ||
$woman->setPreferences($preferences); | ||
printf('%s\'s choices: %s', $woman->getName(), implode(',', $woman->getPreferences())); | ||
echo PHP_EOL; | ||
} | ||
echo PHP_EOL; | ||
|
||
$married = stable_marriage($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. I don't understand why you changed 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. yes, you are right. I forgot that those are objects, which goes by reference and treated it like arrays which are copies. |
||
foreach ($married as $woman) { | ||
printf('%s is married to %s', $woman, $woman->getMatch()); | ||
echo PHP_EOL; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.