/** * @param RuleComparisonInterface $rule * @return Graph */ public function build(RuleComparisonInterface $rule) { $words = $this->dictionary->getWords(); $graph = new Graph(); foreach ($words as $firstWord) { foreach ($words as $secondWord) { if ($rule->alike($firstWord, $secondWord)) { $graph->addRib($firstWord, $secondWord); } } } return $graph; }
public function traversal(Graph $graph, $fromVertex, $toVertex) { $graphNodes = $graph->getNodes(); $visited = $this->fillUnvisitedVertexes($graphNodes); $queue = $this->createQueue($fromVertex); $visited[$fromVertex] = true; $transitions = []; $transitions[$fromVertex] = $this->createTransitionsList($fromVertex); while ($this->notReached($toVertex, $queue)) { $vertex = $queue->dequeue(); if ($this->vertexExists($graphNodes, $vertex)) { foreach ($graphNodes[$vertex] as $relatedVertex) { if (!$visited[$relatedVertex]) { $queue->enqueue($relatedVertex); $visited[$relatedVertex] = true; $transitions[$relatedVertex] = clone $transitions[$vertex]; $transitions[$relatedVertex]->push($relatedVertex); } } } } return isset($transitions[$toVertex]) ? $this->toArray($transitions[$toVertex]) : []; }