/** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on succes; non-zero on failure. */ public static function main($args) { printf("Demonstration program number 10.\n"); $status = 0; GraphAsMatrix::main($args); GraphAsLists::main($args); DigraphAsMatrix::main($args); DigraphAsLists::main($args); return $status; }
* Compares this graph with the specified comparable object. * This method is not implemented. * * @param object IComparable $arg * The object with which to compare this graph. */ protected function compareTo(IComparable $arg) { throw new MethodNotImplementedException(); } /** * Main program. * * @param array $args Command-line arguments. * @return integer Zero on success; non-zero on failure. */ public static function main($args) { printf("GraphAsLists main program.\n"); $status = 0; $g = new GraphAsLists(4); AbstractGraph::test($g); $g->purge(); AbstractGraph::testWeighted($g); $g->purge(); return $status; } } if (realpath($argv[0]) == realpath(__FILE__)) { exit(GraphAsLists::main(array_slice($argv, 1))); }
/** * Destructor. */ public function __destruct() { parent::__destruct(); }
/** * Kruskal's algorithm to find a minimum-cost spanning tree * for the given edge-weighted, undirected graph. * Uses a partition and a priority queue. * * @param object IGraph $g An edge-weighted, undirected graph. * It is assumed that the edge weights are <code>Int</code>s * @return object IGraph An unweighted, undirected graph that represents * the minimum-cost spanning tree. */ public static function kruskalsAlgorithm(IGraph $g) { $n = $g->getNumberOfVertices(); $result = new GraphAsLists($n); for ($v = 0; $v < $n; ++$v) { $result->addVertex($v); } $queue = new BinaryHeap($g->getNumberOfEdges()); foreach ($g->getEdges() as $edge) { $weight = $edge->getWeight(); $queue->enqueue(new Association($weight, $edge)); } $partition = new PartitionAsForest($n); while (!$queue->isEmpty() && $partition->getCount() > 1) { $assoc = $queue->dequeueMin(); $edge = $assoc->getValue(); $n0 = $edge->getV0()->getNumber(); $n1 = $edge->getV1()->getNumber(); $s = $partition->findItem($n0); $t = $partition->findItem($n1); if ($s !== $t) { $partition->join($s, $t); $result->addEdge($n0, $n1); } } return $result; }