From 824f10add370888dee3e8ae3369161c2a5bc845c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 16 Jul 2013 11:09:12 +0200 Subject: [PATCH 1/3] Add Walk::factoryFromVertices() Move logic from Walk::factoryCycleFromVertices() --- lib/Fhaculty/Graph/Walk.php | 68 +++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/lib/Fhaculty/Graph/Walk.php b/lib/Fhaculty/Graph/Walk.php index 23f04195..0b192801 100644 --- a/lib/Fhaculty/Graph/Walk.php +++ b/lib/Fhaculty/Graph/Walk.php @@ -41,6 +41,42 @@ public static function factoryFromEdges($edges, Vertex $startVertex) return new self($vertices, $edges); } + /** + * create new walk instance between given set of Vertices / array of Vertex instances + * + * @param Vertices|Vertex[] $vertices + * @param int|null $by + * @param boolean $desc + * @return Walk + * @throws UnderflowException if no vertices were given + * @see Edges::getEdgeOrder() for parameters $by and $desc + */ + public static function factoryFromVertices($vertices, $by = null, $desc = false) + { + $edges = array(); + $first = NULL; + $last = NULL; + foreach ($vertices as $vertex) { + // skip first vertex as last is unknown + if ($first === NULL) { + $first = $vertex; + } else { + // pick edge between last vertex and this vertex + if ($by === null) { + $edges []= $last->getEdgesTo($vertex)->getEdgeFirst(); + } else { + $edges []= $last->getEdgesTo($vertex)->getEdgeOrder($by, $desc); + } + } + $last = $vertex; + } + if ($last === NULL) { + throw new UnderflowException('No vertices given'); + } + + return new self($vertices, $edges); + } + /** * create new cycle instance from given predecessor map * @@ -102,41 +138,31 @@ public static function factoryCycleFromPredecessorMap($predecessors, $vertex, $b * @return Walk * @throws UnderflowException if no vertices were given * @see Edges::getEdgeOrder() for parameters $by and $desc + * @uses self::factoryFromVertices() */ public static function factoryCycleFromVertices($vertices, $by = null, $desc = false) { - $edges = array(); - $first = NULL; - $last = NULL; - foreach ($vertices as $vertex) { - // skip first vertex as last is unknown - if ($first === NULL) { - $first = $vertex; - } else { - // pick edge between last vertex and this vertex - if ($by === null) { - $edges []= $last->getEdgesTo($vertex)->getEdgeFirst(); - } else { - $edges []= $last->getEdgesTo($vertex)->getEdgeOrder($by, $desc); - } - } - $last = $vertex; - } - if ($last === NULL) { - throw new UnderflowException('No vertices given'); - } + $cycle = self::factoryFromVertices($vertices, $by, $desc); + + $first = $cycle->getVertexSource(); + $last = $cycle->getVertexTarget(); // additional edge from last vertex to first vertex if ($last !== $first) { + $vertices = $cycle->getVertices()->getVector(); + $edges = $cycle->getEdges()->getVector(); + if ($by === null) { $edges []= $last->getEdgesTo($first)->getEdgeFirst(); } else { $edges []= $last->getEdgesTo($first)->getEdgeOrder($by, $desc); } $vertices []= $first; + + $cycle = new self($vertices, $edges); } - return new self($vertices, $edges); + return $cycle; } /** From b0085981c5571c1292b37020041e9e6ab770e30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 16 Jul 2013 11:13:12 +0200 Subject: [PATCH 2/3] Additional test for Walk::factoryFromVertices() --- tests/Fhaculty/Graph/WalkTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Fhaculty/Graph/WalkTest.php b/tests/Fhaculty/Graph/WalkTest.php index 1dc12ea8..b8f90745 100644 --- a/tests/Fhaculty/Graph/WalkTest.php +++ b/tests/Fhaculty/Graph/WalkTest.php @@ -76,6 +76,14 @@ public function testWalkWithinGraph() $this->assertGraphEquals($graphExpected, $walk->createGraph()); + // construct same partial walk "1 -- 2" + $walkVertices = Walk::factoryFromVertices(array($v1, $v2)); + + $this->assertEquals(2, $walkVertices->getNumberOfVertices()); + $this->assertEquals(1, $walkVertices->getNumberOfEdges()); + + $this->assertGraphEquals($graphExpected, $walkVertices->createGraph()); + return $walk; } From 46637a2bac4d686fd0af27d63a0059c68d87d260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 5 Aug 2013 00:41:02 +0200 Subject: [PATCH 3/3] Add Walk::factoryFromVertices() to CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a0298d9..8cf7f7f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Vertex ([#62](https://github.com/clue/graph/issues/62)) ([#62](https://github.com/clue/graph/issues/62)) * Feature: Add `Algorithm\ShortestPath::hasVertex(Vertex $vertex)` to check whether a path to the given Vertex exists ([#62](https://github.com/clue/graph/issues/62)). +* Feature: Add `Walk::factoryFromVertices()` ([#64](https://github.com/clue/graph/issues/64)). * Fix: Checking `Walk::isValid()` ([#61](https://github.com/clue/graph/issues/61)) * Fix: Missing import prevented `Algorithm\ShortestPath\MooreBellmanFord::getCycleNegative()` from actually