Пример #1
0
 /**
  * Forms the clusters by removing maximum weighted edges.
  * performs breadth-first search to cluster the recipes.
  *
  * @param int $k queue size
  * @param int $size number of recipes.
  * @return array $cluster clusters of recipes.
  */
 function formCluster($k, $size)
 {
     $this->cluster_heap->top();
     $nodeQueue = new Queue($k);
     $cluster_count = $size * CLUSTER_RATIO;
     $cluster = array();
     /*
        Idea remove $cluster_count many weightiest edges from tree
        to get a forest. As do this add to queue end points of
        removed edges.
     */
     for ($j = 0; $j < $cluster_count - 1; $j++) {
         $max_edge = $this->cluster_heap->extract();
         $cluster1_start = $max_edge->getStartVertex()->getLabel();
         $cluster2_start = $max_edge->getEndVertex()->getLabel();
         $this->adjMatrix[$cluster1_start][$cluster2_start] = -1;
         $this->adjMatrix[$cluster2_start][$cluster1_start] = -1;
         $nodeQueue->enqueue($cluster1_start);
         $nodeQueue->enqueue($cluster2_start);
     }
     $queue = new Queue($k);
     $i = 0;
     // Now use Queue above to make clusters (trees in resulting forest)
     while (!$nodeQueue->isEmpty()) {
         $node = $nodeQueue->dequeue();
         if ($this->vertices[$node]->isVisited() == false) {
             $this->vertices[$node]->visited();
             $cluster[$i][] = $this->vertices[$node]->getLabel();
             $queue->enqueue($this->vertices[$node]->getLabel());
             while (!$queue->isEmpty()) {
                 $node = $queue->dequeue();
                 while (($nextnode = $this->getNextVertex($node)) != -1) {
                     $this->vertices[$nextnode]->visited();
                     $cluster[$i][] = $this->vertices[$nextnode]->getLabel();
                     $queue->enqueue($this->vertices[$nextnode]->getLabel());
                 }
             }
         }
         $i++;
     }
     return $cluster;
 }