Skip to content

Commit

Permalink
Merge pull request #35 from clue-labs/depthfirst-cleanup
Browse files Browse the repository at this point in the history
Clean up dead code for depth first search
  • Loading branch information
clue committed Oct 12, 2018
2 parents 9a0f93b + dd9c74d commit 364fc3d
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions src/Search/DepthFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,19 @@

namespace Graphp\Algorithms\Search;

use Fhaculty\Graph\Vertex;
use Fhaculty\Graph\Set\Vertices;

class DepthFirst extends Base
{
/**
* calculates an iterative depth-first search
*
* calculates the recursive algorithm
*
* fills $this->visitedVertices
*
* @param Vertex $vertex
* @return Vertices
*/
private function recursiveDepthFirstSearch(Vertex $vertex, array & $visitedVertices)
{
// If I didn't visited this vertex before
if (!isset($visitedVertices[$vertex->getId()])) {
// Add Vertex to already visited vertices
$visitedVertices[$vertex->getId()] = $vertex;

// Get next vertices
$nextVertices = $vertex->getVerticesEdgeTo();

foreach ($nextVertices as $nextVertix) {
// recursive call for next vertices
$this->recursiveDepthFirstSearch($nextVertix, $visitedVertices);
}
}
}

private function iterativeDepthFirstSearch(Vertex $vertex)
public function getVertices()
{
$visited = array();
$todo = array($vertex);
$todo = array($this->vertex);
while ($vertex = array_shift($todo)) {
if (!isset($visited[$vertex->getId()])) {
$visited[$vertex->getId()] = $vertex;
Expand All @@ -48,19 +27,4 @@ private function iterativeDepthFirstSearch(Vertex $vertex)

return new Vertices($visited);
}

/**
* calculates a recursive depth-first search
*
* @return Vertices
*/
public function getVertices()
{
return $this->iterativeDepthFirstSearch($this->vertex);

$visitedVertices = array();
$this->recursiveDepthFirstSearch($this->vertex, $visitedVertices);

return $visitedVertices;
}
}

0 comments on commit 364fc3d

Please sign in to comment.