Skip to content

Commit e627711

Browse files
committed
Update references to Vertex degree helpers to Algorithm\Degree
1 parent 8dbebe8 commit e627711

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

src/Eulerian.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Graph;
7+
use Fhaculty\Graph\Algorithm\Degree;
78

89
class Eulerian extends BaseGraph
910
{
@@ -12,16 +13,18 @@ class Eulerian extends BaseGraph
1213
*
1314
* @return boolean
1415
* @uses Graph::isConnected()
15-
* @uses Vertex::getDegree()
16+
* @uses Degree::getDegreeVertex()
1617
* @todo isolated vertices should be ignored
1718
* @todo definition is only valid for undirected graphs
1819
*/
1920
public function hasCycle()
2021
{
2122
if ($this->graph->isConnected()) {
23+
$alg = new Degree($this->graph);
24+
2225
foreach ($this->graph->getVertices() as $vertex) {
2326
// uneven degree => fail
24-
if ($vertex->getDegree() & 1) {
27+
if ($alg->getDegreeVertex($vertex) & 1) {
2528
return false;
2629
}
2730
}

src/Tree/Base.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Fhaculty\Graph\Exception\UnderflowException;
99
use Fhaculty\Graph\Exception\UnexpectedValueException;
1010
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
11+
use Fhaculty\Graph\Algorithm\Degree;
1112

1213
/**
1314
* Abstract base class for tree algorithms
@@ -35,6 +36,19 @@
3536
*/
3637
abstract class Base extends BaseGraph
3738
{
39+
/**
40+
*
41+
* @var Degree
42+
*/
43+
protected $degree;
44+
45+
public function __construct(Graph $graph)
46+
{
47+
parent::__construct($graph);
48+
49+
$this->degree = new Degree($graph);
50+
}
51+
3852
/**
3953
* checks whether the given graph is actually a tree
4054
*

src/Tree/InTree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class InTree extends DirectedTree
2525
public function getVerticesChildren(Vertex $vertex)
2626
{
2727
$vertices = $vertex->getVerticesEdgeFrom();
28-
if (count($vertices) !== $vertex->getDegreeIn()) {
28+
if (count($vertices) !== $this->degree->getDegreeInVertex($vertex)) {
2929
throw new UnexpectedValueException();
3030
}
3131

@@ -35,7 +35,7 @@ public function getVerticesChildren(Vertex $vertex)
3535
protected function getVerticesParent(Vertex $vertex)
3636
{
3737
$vertices = $vertex->getVerticesEdgeTo();
38-
if (count($vertices) !== $vertex->getDegreeOut()) {
38+
if (count($vertices) !== $this->degree->getDegreeOutVertex($vertex)) {
3939
throw new UnexpectedValueException();
4040
}
4141

src/Tree/OutTree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class OutTree extends DirectedTree
2525
public function getVerticesChildren(Vertex $vertex)
2626
{
2727
$vertices = $vertex->getVerticesEdgeTo();
28-
if (count($vertices) !== $vertex->getDegreeOut()) {
28+
if (count($vertices) !== $this->degree->getDegreeOutVertex($vertex)) {
2929
throw new UnexpectedValueException();
3030
}
3131

@@ -35,7 +35,7 @@ public function getVerticesChildren(Vertex $vertex)
3535
protected function getVerticesParent(Vertex $vertex)
3636
{
3737
$vertices = $vertex->getVerticesEdgeFrom();
38-
if (count($vertices) !== $vertex->getDegreeIn()) {
38+
if (count($vertices) !== $this->degree->getDegreeInVertex($vertex)) {
3939
throw new UnexpectedValueException();
4040
}
4141

src/Tree/Undirected.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Fhaculty\Graph\Vertex;
1111
use Fhaculty\Graph\Edge\Base as Edge;
1212
use Fhaculty\Graph\Edge\UndirectedId as UndirectedEdge;
13+
use Fhaculty\Graph\Algorithm\Degree;
1314

1415
/**
1516
* Undirected tree implementation
@@ -76,23 +77,23 @@ public function isTree()
7677
*
7778
* @param Vertex $vertex
7879
* @return boolean
79-
* @uses Vertex::getDegree()
80+
* @uses Degree::getDegreeVertex()
8081
*/
8182
public function isVertexLeaf(Vertex $vertex)
8283
{
83-
return ($vertex->getDegree() === 1);
84+
return ($this->degree->getDegreeVertex($vertex) === 1);
8485
}
8586

8687
/**
8788
* checks if the given $vertex is an internal vertex (inner vertex with at least 2 edges)
8889
*
8990
* @param Vertex $vertex
9091
* @return boolean
91-
* @uses Vertex::getDegree()
92+
* @uses Degree::getDegreeVertex()
9293
*/
9394
public function isVertexInternal(Vertex $vertex)
9495
{
95-
return ($vertex->getDegree() >= 2);
96+
return ($this->degree->getDegreeVertex($vertex) >= 2);
9697
}
9798

9899
/**

0 commit comments

Comments
 (0)