示例#1
0
文件: Graph.php 项目: Reinmar/Orient
 /**
  * Adds a new vertex to the current graph.
  *
  * @param   Graph\Vertex $vertex 
  * @return  Congow\Orient\Graph
  * @throws  Congow\Orient\Exception
  */
 public function add(Vertex $vertex)
 {
     foreach ($this->getVertices() as $oldVertex) {
         if ($oldVertex->getId() == $vertex->getId()) {
             $message = 'Unable to insert multiple Vertices with the same ID in a Graph';
             
             throw new Exception($message);
         }
     }
     
     $this->vertices[$vertex->getId()] = $vertex;
     
     return $this;
 }
示例#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 Vertex $vertex 
  */
 protected function calculatePotentials(Vertex $vertex)
 {                
     foreach ($vertex->getConnections() 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();
     $mostAdjacentConnectionId = $vertex->getMostAdjacentConnectionId();
     
     if ($mostAdjacentConnectionId && $mostAdjacentConnectionId != $this->getEndingVertex()->getId()) {
         $vertex = $this->getGraph()->getVertex($mostAdjacentConnectionId);
         
         $this->calculatePotentials($vertex);
     }
     
     foreach ($this->getGraph()->getVertices() as $vertex) {
         if (!$vertex->isPassed()) {
             $this->calculatePotentials($vertex);
         }
     }
 }