Skip to content

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

Merged
merged 5 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions contents/stable_marriage_problem/code/php/stable_marriage.php
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);
Copy link
Contributor

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.

$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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Person and we agreed that we don't. So I need to you move the subclass code up to the Person class.

{
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;
}
}
}
}

function stable_marriage(array $men, array $women): void
{
$smallerGroup = count($men) < count($women) ? $men : $women;
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 foreach loop with a break statement.

return $person->isSingle();
}))) break;

} while (true);

foreach ($women as $woman) printf('%s is married to %s%s', $woman, $woman->getMatch(), PHP_EOL);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
2 changes: 2 additions & 0 deletions contents/stable_marriage_problem/stable_marriage_problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ I am incredibly interested to see what you guys do and how you implement the alg
[import, lang:"csharp"](code/csharp/ListExtensions.cs)
{% sample lang="java" %}
[import, lang:"java"](code/java/stable-marriage.java)
{% sample lang="php" %}
[import, lang:"php"](code/php/stable_marriage.php)
{% endmethod %}

<script>
Expand Down