-
-
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
Merged
Butt4cak3
merged 5 commits into
algorithm-archivists:master
from
PudottaPommin:php_stable_marriage_problem
Oct 8, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eaaed03
Implementation Stable marriage problem in PHP
PudottaPommin 6d0d05e
Changed code style
PudottaPommin 330eeab
Update stable_marriage.php
PudottaPommin 8157121
Reworked implementation to one class and fixed some errors from PR
PudottaPommin 7da77b9
Update stable_marriage.php
PudottaPommin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
contents/stable_marriage_problem/code/php/stable_marriage.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?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): void | ||
{ | ||
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); | ||
} | ||
|
||
$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; | ||
|
||
stable_marriage($men, $women); | ||
foreach ($women as $woman) { | ||
printf('%s is married to %s', $woman, $woman->getMatch()); | ||
echo PHP_EOL; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.