Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,908 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
.idea/ | ||
|
||
/vendor/ | ||
|
||
composer.lock |
This file contains 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,21 @@ | ||
{ | ||
"name": "doganoo/php-algorithms", | ||
"description": "A collection of common algorithms implemented in PHP. The collection is based on \"Cracking the Coding Interview\" by Gayle Laakmann McDowell", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Dogan Ucar", | ||
"email": "dogan@dogan-ucar.de" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"doganoo\\PHPAlgorithms\\": "src/" | ||
} | ||
}, | ||
"minimum-stability": "stable", | ||
"require-dev": { | ||
"phpunit/phpunit": "6.5" | ||
} | ||
} |
This file contains 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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false"> | ||
<testsuites> | ||
<testsuite name="PHPAlgorithms Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains 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,125 @@ | ||
<?php | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Dogan Ucar | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
namespace doganoo\PHPAlgorithms\Graph; | ||
|
||
class Graph | ||
{ | ||
private $nodes = []; | ||
|
||
private function getNode($id) | ||
{ | ||
if (array_key_exists($id, $this->nodes)) { | ||
return $this->nodes[$id]; | ||
} | ||
return null; | ||
} | ||
|
||
public function addEdge(int $source, int $destination) | ||
{ | ||
$sourceNode = $this->getNode($source); | ||
if ($sourceNode == null) { | ||
$sourceNode = new Node($source); | ||
} | ||
$destinationNode = $this->getNode($destination); | ||
if ($destinationNode == null) { | ||
$destinationNode = new Node($destination); | ||
} | ||
$sourceNode->addAdjacent($destinationNode); | ||
$this->nodes[$source] = $sourceNode; | ||
// $destinationNode->addAdjacent($sourceNode); | ||
$this->nodes[$destination] = $destinationNode; | ||
} | ||
|
||
public function hasDfs(int $source, int $destination) | ||
{ | ||
$sourceNode = $this->getNode($source); | ||
$destinationNode = $this->getNode($destination); | ||
if ($destinationNode == null) { | ||
$destinationNode = new Node($destination); | ||
} | ||
if ($sourceNode === null) { | ||
return false; | ||
} | ||
return $this->hasDfsInternal($sourceNode, $destinationNode, []); | ||
} | ||
|
||
private function hasDfsInternal(Node $source, Node $destination, array $visited) | ||
{ | ||
if (in_array($source->getValue(), $visited)) { | ||
return false; | ||
} | ||
$visited[] = $source->getValue(); | ||
if ($source->getValue() == $destination->getValue()) { | ||
return true; | ||
} | ||
|
||
/** @var Node $adjacent */ | ||
foreach ($source->getAdjacents() as $adjacent) { | ||
if ($this->hasDfsInternal($adjacent, $destination, $visited)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function hasBfs(int $s, int $d) | ||
{ | ||
$source = $this->getNode($s); | ||
$destination = $this->getNode($d); | ||
$nextToVisit = []; | ||
$visited = []; | ||
|
||
$nextToVisit[] = $source; | ||
|
||
while (count($nextToVisit) !== 0) { | ||
reset($nextToVisit); | ||
$key = key($nextToVisit); | ||
$node = $nextToVisit[$key]; | ||
unset($nextToVisit[$key]); | ||
|
||
if ($node == null) { | ||
return false; | ||
} | ||
if ($destination == null) { | ||
return false; | ||
} | ||
if ($node->getValue() == $destination->getValue()) { | ||
return true; | ||
} | ||
if (in_array($node->getValue(), $visited)) { | ||
return false; | ||
} | ||
$visited[] = $node->getValue(); | ||
|
||
foreach ($node->getAdjacents() as $child) { | ||
$nextToVisit[] = $child; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
} |
This file contains 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,56 @@ | ||
<?php | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Dogan Ucar | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
namespace doganoo\PHPAlgorithms\Graph; | ||
class Node | ||
{ | ||
private $value = 0; | ||
private $adjacent = []; | ||
|
||
public function __construct(int $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function addAdjacent(Node $node) | ||
{ | ||
$this->adjacent[] = $node; | ||
} | ||
|
||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function getAdjacents() | ||
{ | ||
return $this->adjacent; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return "" . $this->value; | ||
} | ||
} |
This file contains 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,51 @@ | ||
<?php | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Dogan Ucar | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
namespace doganoo\PHPAlgorithms\LinkedLists; | ||
|
||
|
||
class Addition | ||
{ | ||
public function addLists(?Node $node1, ?Node $node2, int $carry = 0) | ||
{ | ||
if ($node1 == null || $node2 == null) { | ||
return null; | ||
} | ||
$resultNode = new Node(); | ||
|
||
$value = $carry; | ||
|
||
$value += $node1->getValue(); | ||
$value += $node2->getValue(); | ||
|
||
$resultNode->setValue($value % 10); | ||
|
||
$more = $this->addLists($node1->getNext(), $node2->getNext(), $value > 10 ? 1 : 0); | ||
$resultNode->setNext($more); | ||
|
||
return $resultNode; | ||
} | ||
|
||
} |
Oops, something went wrong.