Skip to content

Commit dd9f340

Browse files
committed
Base on BaseVertex (and thus rename $startVertex to its $vertex)
1 parent 61f085e commit dd9f340

File tree

7 files changed

+27
-50
lines changed

7 files changed

+27
-50
lines changed

src/Search/Base.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,21 @@
22

33
namespace Fhaculty\Graph\Algorithm\Search;
44

5-
use Fhaculty\Graph\Algorithm\Base as AlgorithmBase;
5+
use Fhaculty\Graph\Algorithm\BaseVertex;
66

77
use Fhaculty\Graph\Exception\DomainException;
88

99
use Fhaculty\Graph\Exception\InvalidArgumentException;
1010
use Fhaculty\Graph\Vertex;
1111

12-
abstract class Base extends AlgorithmBase
12+
abstract class Base extends BaseVertex
1313
{
14-
/**
15-
*
16-
* @var Vertex
17-
*/
18-
protected $startVertex;
19-
2014
const DIRECTION_FORWARD = 0;
2115
const DIRECTION_REVERSE = 1;
2216
const DIRECTION_BOTH = 2;
2317

2418
private $direction = self::DIRECTION_FORWARD;
2519

26-
public function __construct(Vertex $startVertex)
27-
{
28-
$this->startVertex = $startVertex;
29-
}
30-
3120
/**
3221
* set direction in which to follow adjacent vertices
3322
*

src/Search/BreadthFirst.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class BreadthFirst extends Base
1313
*/
1414
public function getVertices()
1515
{
16-
$queue = array($this->startVertex);
16+
$queue = array($this->vertex);
1717
// to not add vertices twice in array visited
18-
$mark = array($this->startVertex->getId() => true);
18+
$mark = array($this->vertex->getId() => true);
1919
// visited vertices
2020
$visited = array();
2121

src/Search/DepthFirst.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ private function iterativeDepthFirstSearch(Vertex $vertex)
5656
*/
5757
public function getVertices()
5858
{
59-
return $this->iterativeDepthFirstSearch($this->startVertex);
59+
return $this->iterativeDepthFirstSearch($this->vertex);
6060

6161
$visitedVertices = array();
62-
$this->recursiveDepthFirstSearch($this->startVertex, $visitedVertices);
62+
$this->recursiveDepthFirstSearch($this->vertex, $visitedVertices);
6363

6464
return $visitedVertices;
6565
}

src/ShortestPath/Base.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Fhaculty\Graph\Algorithm\ShortestPath;
44

5+
use Fhaculty\Graph\Algorithm\BaseVertex;
56
use Fhaculty\Graph\Walk;
67

78
use Fhaculty\Graph\Exception\UnderflowException;
@@ -10,22 +11,9 @@
1011

1112
use Fhaculty\Graph\Vertex;
1213
use Fhaculty\Graph\Edge\Base as Edge;
13-
use Fhaculty\Graph\Algorithm\Base as AlgorithmBase;
1414

15-
abstract class Base extends AlgorithmBase
15+
abstract class Base extends BaseVertex
1616
{
17-
/**
18-
* start vertex to build shortest paths to
19-
*
20-
* @var Vertex
21-
*/
22-
protected $startVertex;
23-
24-
public function __construct(Vertex $startVertex)
25-
{
26-
$this->startVertex = $startVertex;
27-
}
28-
2917
/**
3018
* get walk (path) from start vertex to given end vertex
3119
*
@@ -37,7 +25,7 @@ public function __construct(Vertex $startVertex)
3725
*/
3826
public function getWalkTo(Vertex $endVertex)
3927
{
40-
return Walk::factoryFromEdges($this->getEdgesTo($endVertex), $this->startVertex);
28+
return Walk::factoryFromEdges($this->getEdgesTo($endVertex), $this->vertex);
4129
}
4230

4331
/**
@@ -67,7 +55,7 @@ protected function getEdgesToInternal(Vertex $endVertex, array $edges)
6755
{
6856
$currentVertex = $endVertex;
6957
$path = array();
70-
while ($currentVertex !== $this->startVertex) {
58+
while ($currentVertex !== $this->vertex) {
7159
$pre = NULL;
7260
// check all edges to search for edge that points TO current vertex
7361
foreach ($edges as $edge) {
@@ -115,7 +103,7 @@ public function getVertices()
115103
{
116104
$vertices = array();
117105
$map = $this->getDistanceMap();
118-
foreach ($this->startVertex->getGraph()->getVertices() as $vid => $vertex) {
106+
foreach ($this->vertex->getGraph()->getVertices() as $vid => $vertex) {
119107
if (isset($map[$vid])) {
120108
$vertices[$vid] = $vertex;
121109
}
@@ -147,7 +135,7 @@ public function getDistanceMap()
147135
{
148136
$edges = $this->getEdges();
149137
$ret = array();
150-
foreach ($this->startVertex->getGraph()->getVertices() as $vid => $vertex) {
138+
foreach ($this->vertex->getGraph()->getVertices() as $vid => $vertex) {
151139
try {
152140
$ret[$vid] = $this->sumEdges($this->getEdgesToInternal($vertex, $edges));
153141
} catch (UnderflowException $ignore) {
@@ -180,7 +168,7 @@ public function getDistance(Vertex $endVertex)
180168
*/
181169
public function createGraph()
182170
{
183-
return $this->startVertex->getGraph()->createGraphCloneEdges($this->getEdges());
171+
return $this->vertex->getGraph()->createGraphCloneEdges($this->getEdges());
184172
}
185173

186174
/**
@@ -194,9 +182,9 @@ public function createGraph()
194182
*/
195183
protected function getEdgesCheapestPredecesor(array $predecessor)
196184
{
197-
$vertices = $this->startVertex->getGraph()->getVertices();
185+
$vertices = $this->vertex->getGraph()->getVertices();
198186
// start vertex doesn't have a predecessor
199-
unset($vertices[$this->startVertex->getId()]);
187+
unset($vertices[$this->vertex->getId()]);
200188

201189
$edges = array();
202190
foreach ($vertices as $vid => $vertex) {

src/ShortestPath/BreadthFirst.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getEdgesMap()
3636
$vertexQueue = array();
3737
$edges = array();
3838

39-
$vertexCurrent = $this->startVertex;
39+
$vertexCurrent = $this->vertex;
4040
$edgesCurrent = array();
4141

4242
do {
@@ -62,7 +62,7 @@ public function getEdgesMap()
6262

6363
public function getEdgesTo(Vertex $endVertex)
6464
{
65-
if ($endVertex->getGraph() !== $this->startVertex->getGraph()) {
65+
if ($endVertex->getGraph() !== $this->vertex->getGraph()) {
6666
throw new InvalidArgumentException('Given target vertex does not belong to the same graph instance');
6767
}
6868
$map = $this->getEdgesMap();
@@ -98,7 +98,7 @@ public function getDistanceMap()
9898
*/
9999
public function hasVertex(Vertex $endVertex)
100100
{
101-
if ($endVertex->getGraph() !== $this->startVertex->getGraph()) {
101+
if ($endVertex->getGraph() !== $this->vertex->getGraph()) {
102102
throw new InvalidArgumentException('Given target vertex does not belong to the same graph instance');
103103
}
104104
$map = $this->getEdgesMap();
@@ -115,7 +115,7 @@ public function hasVertex(Vertex $endVertex)
115115
public function getVertices()
116116
{
117117
$ret = array();
118-
$graph = $this->startVertex->getGraph();
118+
$graph = $this->vertex->getGraph();
119119
foreach ($this->getEdgesMap() as $vid => $unusedEdges) {
120120
$ret[$vid] = $graph->getVertex($vid);
121121
}

src/ShortestPath/Dijkstra.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ public function getEdges()
1616
{
1717
$totalCostOfCheapestPathTo = Array();
1818
// start node distance
19-
$totalCostOfCheapestPathTo[$this->startVertex->getId()] = 0;
19+
$totalCostOfCheapestPathTo[$this->vertex->getId()] = 0;
2020

2121
// just to get the cheapest vertex in the correct order
2222
$cheapestVertex = new SplPriorityQueue();
23-
$cheapestVertex->insert($this->startVertex, 0);
23+
$cheapestVertex->insert($this->vertex, 0);
2424

2525
// predecessor
2626
$predecesVertexOfCheapestPathTo = Array();
27-
$predecesVertexOfCheapestPathTo[$this->startVertex->getId()] = $this->startVertex;
27+
$predecesVertexOfCheapestPathTo[$this->vertex->getId()] = $this->vertex;
2828

2929
// mark vertices when their cheapest path has been found
3030
$usedVertices = Array();
3131

3232
// Repeat until all vertices have been marked
33-
$totalCountOfVertices = $this->startVertex->getGraph()->getNumberOfVertices();
33+
$totalCountOfVertices = $this->vertex->getGraph()->getNumberOfVertices();
3434
for ($i = 0; $i < $totalCountOfVertices; ++$i) {
3535
$currentVertex = NULL;
3636
$currentVertexId = NULL;

src/ShortestPath/MooreBellmanFord.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ private function bigStep(array &$edges, array &$totalCostOfCheapestPathTo, array
5656
public function getEdges()
5757
{
5858
// start node distance
59-
$totalCostOfCheapestPathTo = array($this->startVertex->getId() => 0);
59+
$totalCostOfCheapestPathTo = array($this->vertex->getId() => 0);
6060

6161
// predecessor
62-
$predecessorVertexOfCheapestPathTo = array($this->startVertex->getId() => $this->startVertex);
62+
$predecessorVertexOfCheapestPathTo = array($this->vertex->getId() => $this->vertex);
6363

6464
// repeat (n-1) times
65-
$numSteps = $this->startVertex->getGraph()->getNumberOfVertices() - 1;
66-
$edges = $this->startVertex->getGraph()->getEdges();
65+
$numSteps = $this->vertex->getGraph()->getNumberOfVertices() - 1;
66+
$edges = $this->vertex->getGraph()->getEdges();
6767
$changed = true;
6868
// repeat n-1 times
6969
for ($i = 0; $i < $numSteps && $changed; ++$i) {

0 commit comments

Comments
 (0)