Skip to content

Commit eef93ef

Browse files
committed
Make sure to always operate on Set\Vertices or Set\Edges
1 parent de16e33 commit eef93ef

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

src/ShortestPath/Base.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ public function getEdgesTo(Vertex $endVertex)
8383
/**
8484
* get array of edges (path) from start vertex to given end vertex
8585
*
86-
* @param Vertex $endVertex
87-
* @param array $edges array of all input edges to operate on
86+
* @param Vertex $endVertex
87+
* @param Edges|Edge[] $edges set or array of all input edges to operate on
8888
* @throws OutOfBoundsException if there's no path to the given vertex
8989
* @return Edges
9090
* @uses self::getEdges() if no edges were given
9191
*/
92-
protected function getEdgesToInternal(Vertex $endVertex, array $edges)
92+
protected function getEdgesToInternal(Vertex $endVertex, $edges)
9393
{
9494
$currentVertex = $endVertex;
9595
$path = array();

src/ShortestPath/BreadthFirst.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Fhaculty\Graph\Vertex;
66
use Fhaculty\Graph\Exception\OutOfBoundsException;
77
use Fhaculty\Graph\Set\Vertices;
8+
use Fhaculty\Graph\Set\Edges;
89
use \Exception;
910

1011
/**
@@ -76,7 +77,7 @@ public function getEdgesTo(Vertex $endVertex)
7677
$map = $this->getEdgesMap();
7778

7879
if (isset($map[$endVertex->getId()])) {
79-
return $map[$endVertex->getId()];
80+
return new Edges($map[$endVertex->getId()]);
8081
}
8182
}
8283
throw new OutOfBoundsException('Given target vertex can not be reached from start vertex');
@@ -126,6 +127,6 @@ public function getEdges()
126127
}
127128
}
128129

129-
return $ret;
130+
return new Edges($ret);
130131
}
131132
}

src/ShortestPath/MooreBellmanFord.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Fhaculty\Graph\Algorithm\ShortestPath;
44

55
use Fhaculty\Graph\Edge\Base as Edge;
6+
use Fhaculty\Graph\Set\Edges;
67
use Fhaculty\Graph\Walk;
78
use Fhaculty\Graph\Exception\NegativeCycleException;
89
use Fhaculty\Graph\Exception\UnderflowException;
@@ -97,7 +98,7 @@ public function getEdges()
9798
// Check for negative cycles (only if last step didn't already finish anyway)
9899
// something is still changing...
99100
if ($changed && $changed = $this->bigStep($edges, $totalCostOfCheapestPathTo, $predecessorVertexOfCheapestPathTo)) {
100-
$cycle = Walk::factoryCycleFromPredecessorMap($predecessorVertexOfCheapestPathTo, $changed, Edge::ORDER_WEIGHT);
101+
$cycle = Walk::factoryCycleFromPredecessorMap($predecessorVertexOfCheapestPathTo, $changed, Edges::ORDER_WEIGHT);
101102
throw new NegativeCycleException('Negative cycle found', 0, NULL, $cycle);
102103
}
103104

tests/ShortestPath/BaseShortestPathTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public function testGraphTrivial()
2626
$this->assertFalse($alg->hasVertex($v1));
2727
//$this->assertEquals(0, $alg->getDistance($v1));
2828
$this->assertEquals(array(), $alg->getDistanceMap());
29-
$this->assertEquals(array(), $alg->getEdges());
29+
$this->assertEquals(array(), $alg->getEdges()->getVector());
3030
//$this->assertEquals(array(), $alg->getEdgesTo($v1));
31-
$this->assertEquals(array(), $alg->getVertices());
32-
$this->assertEquals(array(), $alg->getVerticesId());
31+
$this->assertEquals(array(), $alg->getVertices()->getVector());
32+
$this->assertEquals(array(), $alg->getVertices()->getIds());
3333

3434
$clone = $alg->createGraph();
3535
$this->assertGraphEquals($graph,$clone);
@@ -44,15 +44,15 @@ public function testGraphSingleLoop()
4444

4545
$alg = $this->createAlg($v1);
4646

47-
$this->assertEquals(array($e1), $alg->getEdges());
47+
$this->assertEquals(array($e1), $alg->getEdges()->getVector());
4848

4949
$expectedWeight = $this->getExpectedWeight(array($e1));
5050
$this->assertTrue($alg->hasVertex($v1));
5151
$this->assertEquals($expectedWeight, $alg->getDistance($v1));
5252
$this->assertEquals(array(1 => $expectedWeight), $alg->getDistanceMap());
53-
$this->assertEquals(array($e1), $alg->getEdgesTo($v1));
54-
$this->assertEquals(array(1 => $v1), $alg->getVertices());
55-
$this->assertEquals(array(1), $alg->getVerticesId());
53+
$this->assertEquals(array($e1), $alg->getEdgesTo($v1)->getVector());
54+
$this->assertEquals(array(1 => $v1), $alg->getVertices()->getMap());
55+
$this->assertEquals(array(1), $alg->getVertices()->getIds());
5656
}
5757

5858
public function testGraphCycle()
@@ -70,12 +70,12 @@ public function testGraphCycle()
7070

7171
$expectedWeight = $this->getExpectedWeight(array($e1));
7272
$this->assertTrue($alg->hasVertex($v2));
73-
$this->assertEquals(array($e1), $alg->getEdgesTo($v2));
73+
$this->assertEquals(array($e1), $alg->getEdgesTo($v2)->getVector());
7474
$this->assertEquals($expectedWeight, $alg->getDistance($v2));
7575

7676
$expectedWeight = $this->getExpectedWeight(array($e1, $e2));
7777
$this->assertTrue($alg->hasVertex($v1));
78-
$this->assertEquals(array($e1, $e2), $alg->getEdgesTo($v1));
78+
$this->assertEquals(array($e1, $e2), $alg->getEdgesTo($v1)->getVector());
7979
$this->assertEquals($expectedWeight, $alg->getDistance($v1));
8080

8181
$walk = $alg->getWalkTo($v1);
@@ -133,11 +133,11 @@ public function testGraphTwoComponents()
133133
$expectedWeight = $this->getExpectedWeight(array($e1));
134134
$this->assertEquals($expectedWeight, $alg->getDistance($v2));
135135
$this->assertEquals(array(2 => $expectedWeight), $alg->getDistanceMap());
136-
$this->assertEquals(array($e1), $alg->getEdges());
136+
$this->assertEquals(array($e1), $alg->getEdges()->getVector());
137137
// $this->assertEquals(array(), $alg->getEdgesTo($v1));
138-
$this->assertEquals(array($e1), $alg->getEdgesTo($v2));
139-
$this->assertEquals(array(2 => $v2), $alg->getVertices());
140-
$this->assertEquals(array(2), $alg->getVerticesId());
138+
$this->assertEquals(array($e1), $alg->getEdgesTo($v2)->getVector());
139+
$this->assertEquals(array(2 => $v2), $alg->getVertices()->getMap());
140+
$this->assertEquals(array(2), $alg->getVertices()->getIds());
141141
}
142142

143143
protected function getExpectedWeight($edges)

tests/ShortestPath/BreadthFirstTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function testGraphParallelNegative()
2525

2626
$this->assertEquals(1, $alg->getDistance($v2));
2727
$this->assertEquals(array(2 => 1), $alg->getDistanceMap());
28-
$this->assertEquals(array($e1), $alg->getEdges());
29-
$this->assertEquals(array($e1), $alg->getEdgesTo($v2));
30-
$this->assertEquals(array(2 => $v2), $alg->getVertices());
31-
$this->assertEquals(array(2), $alg->getVerticesId());
28+
$this->assertEquals(array($e1), $alg->getEdges()->getVector());
29+
$this->assertEquals(array($e1), $alg->getEdgesTo($v2)->getVector());
30+
$this->assertEquals(array(2 => $v2), $alg->getVertices()->getMap());
31+
$this->assertEquals(array(2), $alg->getVertices()->getIds());
3232
}
3333

3434
protected function getExpectedWeight($edges)

tests/ShortestPath/MooreBellmanFordTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function testGraphParallelNegative()
2626
// $this->assertEquals(0, $alg->getDistance($v1));
2727
$this->assertEquals(-1, $alg->getDistance($v2));
2828
$this->assertEquals(array(2 => -1), $alg->getDistanceMap());
29-
$this->assertEquals(array($e2), $alg->getEdges());
29+
$this->assertEquals(array($e2), $alg->getEdges()->getVector());
3030
//$this->assertEquals(array(), $alg->getEdgesTo($v1));
31-
$this->assertEquals(array($e2), $alg->getEdgesTo($v2));
32-
$this->assertEquals(array(2 => $v2), $alg->getVertices());
33-
$this->assertEquals(array(2), $alg->getVerticesId());
31+
$this->assertEquals(array($e2), $alg->getEdgesTo($v2)->getVector());
32+
$this->assertEquals(array(2 => $v2), $alg->getVertices()->getMap());
33+
$this->assertEquals(array(2), $alg->getVertices()->getIds());
3434

3535
return $alg;
3636
}

0 commit comments

Comments
 (0)