Example #1
0
 /**
  * {@inheritdoc}
  */
 public function add(VertexInterface $vertex)
 {
     if (array_key_exists($vertex->getId(), $this->getVertices())) {
         throw new Exception('Unable to insert multiple Vertices with the same ID in a Graph');
     }
     $this->vertices[$vertex->getId()] = $vertex;
     return $this;
 }
Example #2
0
 /**
  * Recursively calculates the potentials of the graph, from the
  * starting point you specify with ->setStartingVertex(), traversing
  * the graph due to Vertex's $connections attribute.
  *
  * @param VertexInterface $vertex
  */
 protected function calculatePotentials(VertexInterface $vertex)
 {
     $connections = $vertex->getConnections();
     $sorted = array_flip($connections);
     krsort($sorted);
     foreach ($connections as $id => $distance) {
         $v = $this->getGraph()->getVertex($id);
         $v->setPotential($vertex->getPotential() + $distance, $vertex);
         foreach ($this->getPaths() as $path) {
             $count = count($path);
             if ($path[$count - 1]->getId() === $vertex->getId()) {
                 $this->paths[] = array_merge($path, array($v));
             }
         }
     }
     $vertex->markPassed();
     // Get loop through the current node's nearest connections
     // to calculate their potentials.
     foreach ($sorted as $id) {
         $vertex = $this->getGraph()->getVertex($id);
         if (!$vertex->isPassed()) {
             $this->calculatePotentials($vertex);
         }
     }
 }