/** * Prevent adding a cycle. * * @param type $from_id * @param type $to_id */ function addLink($from_id, $to_id) { $sub = $this->subGraph(array($to_id)); $p = $sub->getParticipants(); if (in_array($from_id, $p)) { throw new \Exception("Cannot add Cycle '{$from_id}' -> '{$to_id}' to " . $this); } else { parent::addLink($from_id, $to_id); } }
/** * Builds a reversed graph without data elements or multigraph. * * By reversing a graph one can easily add a root or do other calculations. * This method is a helper utility. * * Note: It is of no use to reverse a Graph. * Note: data items especially link data is semantic useless when reversed. * * @return DirectedGraph * @see DirectedGraph::addRoot * */ public function getReversedGraph() { $g = new DirectedGraph(); foreach (array_keys($this->_list) as $from_id) { $g->addNode($from_id); foreach ($this->getLinks($from_id) as $to_id) { $g->addLink($to_id, $from_id); } } return $g; }