Skip to content

Commit eeb6665

Browse files
committed
some minor improvements
1 parent 85d836f commit eeb6665

File tree

11 files changed

+236
-25
lines changed

11 files changed

+236
-25
lines changed

src/Algorithm/Search/BreadthFirstSearch.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function search(AbstractGraph $graph) {
5555
* @return mixed|void
5656
*/
5757
public function searchByNode(?Node $node) {
58+
if (null === $node) return;
5859
$queue = new Queue();
5960
$this->visited->add($node);
6061
$queue->enqueue($node);

src/Common/Abstracts/AbstractGraph.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,34 @@ public function getRoot(): ?Node {
6666
? null : $this->nodeList->get(0);
6767
}
6868

69+
/**
70+
* @param $value
71+
* @return bool
72+
*/
73+
public function createNode($value): bool {
74+
$node = new Node($value);
75+
return $this->addNode($node);
76+
}
77+
6978
/**
7079
* @param Node $node
7180
* @return bool
7281
*/
73-
public abstract function addNode(Node $node): bool;
82+
public function addNode(Node $node): bool {
83+
return $this->nodeList->add($node);
84+
}
7485

7586
/**
7687
* @param Node $startNode
7788
* @param Node $endNode
7889
* @return bool
7990
*/
8091
public abstract function addEdge(Node $startNode, Node $endNode): bool;
92+
93+
/**
94+
* @return ArrayList|null
95+
*/
96+
public function getNodes(): ?ArrayList {
97+
return $this->nodeList;
98+
}
8199
}

src/Common/Abstracts/AbstractGraphSearch.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ abstract class AbstractGraphSearch {
4242

4343
public function __construct() {
4444
$this->visited = new ArrayList();
45-
$this->callable = function ($value) {
46-
echo $value;
45+
$this->callable = function (Node $value) {
46+
echo $value->getValue();
47+
echo "\n";
4748
};
4849
}
4950

@@ -56,12 +57,13 @@ public abstract function search(AbstractGraph $graph);
5657
/**
5758
* @param $value
5859
*/
59-
public function visit($value) {
60+
public function visit(Node $value) {
6061
$callable = $this->callable;
6162
if (null === $this->callable
6263
&& !\is_callable($this->callable)) {
63-
$callable = function ($otherValue) {
64-
echo $otherValue;
64+
$callable = function (Node $otherValue) {
65+
echo $otherValue->getValue();
66+
echo "\n";
6567
};
6668
}
6769
$callable($value);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPAlgorithms\Common\Exception;
27+
28+
/**
29+
* Class NodeNotFoundException
30+
*
31+
* @package doganoo\PHPAlgorithms\Common\Exception
32+
*/
33+
class NodeNotFoundException extends \Exception {
34+
35+
}

src/Common/Util/Comparator.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ public static function lessThan($that, $other): bool {
8989
return $that < $other;
9090
}
9191

92+
/**
93+
* @param $that
94+
* @param $other
95+
* @return bool
96+
*/
97+
public static function lessThanEqual($that, $other): bool {
98+
if ($that instanceof Comparable) {
99+
return $that->compareTo($other) == -1;
100+
}
101+
if (\is_object($that)) {
102+
if (\is_object($other)) {
103+
return $that <= $other;
104+
}
105+
return false;
106+
}
107+
if (\is_object($other)) {
108+
if (\is_object($that)) {
109+
return $other <= $that;
110+
}
111+
return false;
112+
}
113+
return $that <= $other;
114+
}
115+
92116
/**
93117
* @param $that
94118
* @param $other
@@ -110,7 +134,31 @@ public static function greaterThan($that, $other): bool {
110134
}
111135
return false;
112136
}
113-
return ($that > $other);
137+
return $that > $other;
138+
}
139+
140+
/**
141+
* @param $that
142+
* @param $other
143+
* @return bool
144+
*/
145+
public static function greaterThanEqual($that, $other): bool {
146+
if ($that instanceof Comparable) {
147+
return $that->compareTo($other) == 1;
148+
}
149+
if (\is_object($that)) {
150+
if (\is_object($other)) {
151+
return $that >= $other;
152+
}
153+
return false;
154+
}
155+
if (\is_object($other)) {
156+
if (\is_object($that)) {
157+
return $other >= $that;
158+
}
159+
return false;
160+
}
161+
return $that >= $other;
114162
}
115163

116164
}

src/Datastructure/Graph/Graph/DirectedGraph.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ public function __construct() {
4343
parent::__construct(self::DIRECTED_GRAPH);
4444
}
4545

46-
47-
public function addNode(Node $node): bool {
48-
return $this->nodeList->add($node);
49-
}
50-
5146
/**
5247
* @param Node $startNode
5348
* @param Node $endNode
@@ -81,6 +76,7 @@ public function addEdge(Node $startNode, Node $endNode): bool {
8176
return false;
8277
}
8378
$startNode->addAdjacent($endNode);
79+
$endNode->incrementInbound();
8480

8581
$this->nodeList->set($indexOfStartNode, $startNode);
8682
return true;

src/Datastructure/Graph/Graph/Node.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
use doganoo\PHPAlgorithms\Common\Interfaces\Comparable;
2929
use doganoo\PHPAlgorithms\Common\Interfaces\INode;
30-
use doganoo\PHPAlgorithms\Common\Util\Comparator;
3130
use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists\ArrayList;
3231

3332
/**
@@ -36,18 +35,51 @@
3635
* @package doganoo\PHPAlgorithms\Graph
3736
*/
3837
class Node implements Comparable, INode {
38+
/** @var mixed $value */
3939
private $value;
40+
/** @var ArrayList|null $adjacent */
4041
private $adjacent = null;
42+
/** @var int $inbound */
43+
private $inbound = 0;
4144

45+
46+
/**
47+
* Node constructor.
48+
*
49+
* @param $value
50+
*/
4251
public function __construct($value) {
4352
$this->value = $value;
4453
$this->adjacent = new ArrayList();
54+
$this->inbound = 0;
4555
}
4656

57+
/**
58+
* @param Node $node
59+
* @return bool
60+
*/
4761
public function addAdjacent(Node $node): bool {
4862
return $this->adjacent->add($node);
4963
}
5064

65+
/**
66+
* @return void
67+
*/
68+
public function incrementInbound(): void {
69+
$this->inbound++;
70+
}
71+
72+
/**
73+
* @return void
74+
*/
75+
public function decrementInbound(): void {
76+
$this->inbound--;
77+
}
78+
79+
/**
80+
* @param Node $node
81+
* @return bool
82+
*/
5183
public function hasAdjacent(Node $node) {
5284
/**
5385
* @var $key
@@ -61,16 +93,19 @@ public function hasAdjacent(Node $node) {
6193
return false;
6294
}
6395

96+
/**
97+
* @return mixed
98+
*/
6499
public function getValue() {
65100
return $this->value;
66101
}
67102

103+
/**
104+
* @param Node $node
105+
* @return bool
106+
*/
68107
public function equals(Node $node): bool {
69-
return Comparator::equals($this->value, $node->getValue());
70-
}
71-
72-
public function getAdjacents(): ?ArrayList {
73-
return $this->adjacent;
108+
return $this->compareTo($node) === 0;
74109
}
75110

76111
/**
@@ -92,4 +127,18 @@ public function compareTo($object): int {
92127
}
93128
return -1;
94129
}
130+
131+
/**
132+
* @return ArrayList|null
133+
*/
134+
public function getAdjacents(): ?ArrayList {
135+
return $this->adjacent;
136+
}
137+
138+
/**
139+
* @return int
140+
*/
141+
public function countInbound(): int {
142+
return $this->inbound;
143+
}
95144
}

src/Datastructure/Graph/Graph/UndirectedGraph.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace doganoo\PHPAlgorithms\Datastructure\Graph\Graph;
2727

2828
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractGraph;
29+
use doganoo\PHPAlgorithms\Common\Exception\NodeNotFoundException;
2930

3031
/**
3132
* Class Graph
@@ -43,7 +44,10 @@ public function __construct() {
4344
parent::__construct(self::UNDIRECTED_GRAPH);
4445
}
4546

46-
47+
/**
48+
* @param Node $node
49+
* @return bool
50+
*/
4751
public function addNode(Node $node): bool {
4852
return $this->nodeList->add($node);
4953
}
@@ -53,17 +57,16 @@ public function addNode(Node $node): bool {
5357
* @param Node $endNode
5458
* @return bool
5559
* @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException
60+
* @throws NodeNotFoundException
5661
*/
5762
public function addEdge(Node $startNode, Node $endNode): bool {
5863
$hasStart = $this->nodeList->containsValue($startNode);
5964
$hasEnd = $this->nodeList->containsValue($endNode);
6065
if (false === $hasStart) {
61-
//TODO notify caller
62-
return false;
66+
throw new NodeNotFoundException();
6367
}
6468
if (false === $hasEnd) {
65-
//TODO notify caller
66-
return false;
69+
throw new NodeNotFoundException();
6770
}
6871
$indexOfStartNode = $this->nodeList->indexOf($startNode);
6972
/** @var Node $startNode */
@@ -72,11 +75,12 @@ public function addEdge(Node $startNode, Node $endNode): bool {
7275
/** @var Node $endNode */
7376
$endNode = $this->nodeList->get($indexOfEndNode);
7477

75-
if ($startNode->hasAdjacent($endNode)) {
78+
if (!$startNode->hasAdjacent($endNode)) {
7679
//TODO notify caller
7780
return false;
7881
}
7982
$startNode->addAdjacent($endNode);
83+
$endNode->incrementInbound();
8084

8185
$this->nodeList->set($indexOfStartNode, $startNode);
8286
return true;

0 commit comments

Comments
 (0)