Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Feb 18, 2018
1 parent dc6d7f3 commit 2e69939
Show file tree
Hide file tree
Showing 22 changed files with 1,908 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.idea/

/vendor/

composer.lock
21 changes: 21 additions & 0 deletions composer.json
@@ -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"
}
}
17 changes: 17 additions & 0 deletions phpunit.xml
@@ -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>
125 changes: 125 additions & 0 deletions src/Graph/Graph.php
@@ -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;
}


}
56 changes: 56 additions & 0 deletions src/Graph/Node.php
@@ -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;
}
}
51 changes: 51 additions & 0 deletions src/LinkedLists/Addition.php
@@ -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;
}

}

0 comments on commit 2e69939

Please sign in to comment.