Exemple #1
0
 /**
  * Constructs a PartitionTree
  * for the specified element of the universal set
  * a the specified Partition.
  *
  * @param object PartitionAsForest $partition The partition.
  * @param integer $item An element of the universal set.
  */
 public function __construct(PartitionAsForest $partition, $item)
 {
     parent::__construct($partition->getUniverseSize());
     $this->partition = $partition;
     $this->item = $item;
     $this->parent = NULL;
     $this->rank = 0;
     $this->count = 1;
 }
Exemple #2
0
 /**
  * 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 7.\n");
     $status = 0;
     SetAsArray::main($args);
     SetAsBitVector::main($args);
     MultisetAsArray::main($args);
     MultisetAsLinkedList::main($args);
     PartitionAsForest::main($args);
     return $status;
 }
 /**
  * Destructor.
  */
 public function __destruct()
 {
     parent::__destruct();
 }
Exemple #4
0
 /**
  * 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;
 }
        throw new MethodNotImplemented();
    }
    /**
     * Compares this partition with the specified comparable object.
     * This method is not implemented.
     *
     * @param object IComparable $obj
     * The object with which this partition is compared.
     */
    protected function compareTo(IComparable $arg)
    {
        throw new MethodNotImplemented();
    }
    /**
     * Main program.
     *
     * @param array $args Command-line arguments.
     * @return integer Zero on success; non-zero on failure.
     */
    public static function main($args)
    {
        printf("PartitionAsForest main program.\n");
        $status = 0;
        $p = new PartitionAsForest(5);
        AbstractPartition::test($p);
        return $status;
    }
}
if (realpath($argv[0]) == realpath(__FILE__)) {
    exit(PartitionAsForest::main(array_slice($argv, 1)));
}