コード例 #1
0
 /**
  *
  * @return Edges
  */
 public function getEdges()
 {
     $returnEdges = array();
     // Create minimum spanning tree
     $minimumSpanningTreeAlgorithm = new MstKruskal($this->graph);
     $minimumSpanningTree = $minimumSpanningTreeAlgorithm->createGraph();
     $alg = new SearchDepthFirst($minimumSpanningTree->getVertices()->getVertexFirst());
     // Depth first search in minmum spanning tree (for the eulerian path)
     $startVertex = NULL;
     $oldVertex = NULL;
     // connect vertices in order of the depth first search
     foreach ($alg->getVertices() as $vertex) {
         // get vertex from the original graph (not from the depth first search)
         $vertex = $this->graph->getVertex($vertex->getId());
         // need to clone the edge from the original graph, therefore i need the original edge
         if ($startVertex === NULL) {
             $startVertex = $vertex;
         } else {
             // get edge(s) to clone, multiple edges are possible (returns an array if undirected edge)
             $returnEdges[] = $oldVertex->getEdgesTo($vertex)->getEdgeFirst();
         }
         $oldVertex = $vertex;
     }
     // connect last vertex with start vertex
     // multiple edges are possible (returns an array if undirected edge)
     $returnEdges[] = $oldVertex->getEdgesTo($startVertex)->getEdgeFirst();
     return new Edges($returnEdges);
 }
コード例 #2
0
 /**
  *
  *
  * @param  Vertex    $source
  * @throws Exception if there is no reachable sink vertex
  *
  * @return Vertex a sink-vertex that is reachable from the source
  * @uses BreadthFirst::getVertices()
  */
 private function getVertexSink(Vertex $source)
 {
     // search for reachable Vertices
     $algBFS = new SearchBreadthFirst($source);
     foreach ($algBFS->getVertices()->getMap() as $vid => $vertex) {
         if ($this->graph->getVertex($vid)->getBalance() - $vertex->getBalance() < 0) {
             return $vertex;
         }
     }
     throw new UnderflowException('No sink vertex connected to given source vertex found');
 }