Skip to content

Commit a092d61

Browse files
committed
Consistently throw OutOfBoundsException for unreachable vertices
1 parent 1c21011 commit a092d61

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/ShortestPath/Base.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
use Fhaculty\Graph\Algorithm\BaseVertex;
66
use Fhaculty\Graph\Walk;
7-
8-
use Fhaculty\Graph\Exception\UnderflowException;
9-
7+
use Fhaculty\Graph\Exception\OutOfBoundsException;
108
use Fhaculty\Graph\Exception\InvalidArgumentException;
11-
129
use Fhaculty\Graph\Vertex;
1310
use Fhaculty\Graph\Edge\Base as Edge;
1411

@@ -19,7 +16,7 @@ abstract class Base extends BaseVertex
1916
*
2017
* @param Vertex $endVertex
2118
* @return Walk
22-
* @throws Exception when there's no walk from start to end vertex
19+
* @throws OutOfBoundsException if there's no path to the given end vertex
2320
* @uses self::getEdgesTo()
2421
* @uses Walk::factoryFromEdges()
2522
*/
@@ -32,7 +29,7 @@ public function getWalkTo(Vertex $endVertex)
3229
* get array of edges (path) from start vertex to given end vertex
3330
*
3431
* @param Vertex $endVertex
35-
* @throws Exception
32+
* @throws OutOfBoundsException if there's no path to the given end vertex
3633
* @return Edge[]
3734
* @uses AlgorithmSp::getEdges()
3835
* @uses AlgorithmSp::getEdgesToInternal()
@@ -47,7 +44,7 @@ public function getEdgesTo(Vertex $endVertex)
4744
*
4845
* @param Vertex $endVertex
4946
* @param array $edges array of all input edges to operate on
50-
* @throws Exception
47+
* @throws OutOfBoundsException if there's no path to the given vertex
5148
* @return Edge[]
5249
* @uses AlgorithmSp::getEdges() if no edges were given
5350
*/
@@ -69,7 +66,7 @@ protected function getEdgesToInternal(Vertex $endVertex, array $edges)
6966
} // ignore: this edge does not point TO current vertex
7067
}
7168
if ($pre === NULL) {
72-
throw new UnderflowException('No edge leading to vertex');
69+
throw new OutOfBoundsException('No edge leading to vertex');
7370
}
7471
}
7572

@@ -138,7 +135,7 @@ public function getDistanceMap()
138135
foreach ($this->vertex->getGraph()->getVertices() as $vid => $vertex) {
139136
try {
140137
$ret[$vid] = $this->sumEdges($this->getEdgesToInternal($vertex, $edges));
141-
} catch (UnderflowException $ignore) {
138+
} catch (OutOfBoundsException $ignore) {
142139
} // ignore vertices that can not be reached
143140
}
144141

@@ -150,7 +147,7 @@ public function getDistanceMap()
150147
*
151148
* @param Vertex $endVertex
152149
* @return float
153-
* @throws Exception if given vertex is invalid or there's no path to given end vertex
150+
* @throws OutOfBoundsException if there's no path to the given end vertex
154151
* @uses AlgorithmSp::getEdgesTo()
155152
* @uses AlgorithmSp::sumEdges()
156153
*/

src/ShortestPath/BreadthFirst.php

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

39+
// $edges[$this->vertex->getId()] = array();
40+
3941
$vertexCurrent = $this->vertex;
4042
$edgesCurrent = array();
4143

@@ -62,15 +64,14 @@ public function getEdgesMap()
6264

6365
public function getEdgesTo(Vertex $endVertex)
6466
{
65-
if ($endVertex->getGraph() !== $this->vertex->getGraph()) {
66-
throw new InvalidArgumentException('Given target vertex does not belong to the same graph instance');
67-
}
68-
$map = $this->getEdgesMap();
69-
if (!isset($map[$endVertex->getId()])) {
70-
throw new OutOfBoundsException('Given target vertex can not be reached from start vertex');
71-
}
67+
if ($endVertex->getGraph() === $this->vertex->getGraph()) {
68+
$map = $this->getEdgesMap();
7269

73-
return $map[$endVertex->getId()];
70+
if (isset($map[$endVertex->getId()])) {
71+
return $map[$endVertex->getId()];
72+
}
73+
}
74+
throw new OutOfBoundsException('Given target vertex can not be reached from start vertex');
7475
}
7576

7677
/**
@@ -94,23 +95,24 @@ public function getDistanceMap()
9495
*
9596
* @param Vertex $endVertex
9697
* @return boolean
97-
* @uses AlgorithmSpBreadthFirst::getEdgesMap()
98+
* @uses self::getEdgesTo()
9899
*/
99-
public function hasVertex(Vertex $endVertex)
100+
public function hasVertex(Vertex $vertex)
100101
{
101-
if ($endVertex->getGraph() !== $this->vertex->getGraph()) {
102-
throw new InvalidArgumentException('Given target vertex does not belong to the same graph instance');
102+
try {
103+
$this->getEdgesTo($vertex);
103104
}
104-
$map = $this->getEdgesMap();
105-
106-
return isset($map[$endVertex->getId()]);
105+
catch (OutOfBoundsException $e) {
106+
return false;
107+
}
108+
return true;
107109
}
108110

109111
/**
110112
* get array of all target vertices this vertex has a path to
111113
*
112114
* @return Vertex[]
113-
* @uses AlgorithmSpBreadthFirst::getDistanceMap()
115+
* @uses self::getEdgesMap()
114116
*/
115117
public function getVertices()
116118
{

0 commit comments

Comments
 (0)