PHP實現克魯斯卡爾演算法執行個體解析_php技巧

來源:互聯網
上載者:User

本文執行個體展示了PHP實現的格魯斯卡爾演算法(kruscal)的實現方法,分享給大家供大家參考。相信對於大家的PHP程式設計有一定的借鑒價值。

具體代碼如下:

<?phprequire 'edge.php';$a = array(  'a',  'b',  'c',  'd',  'e',  'f',  'g',  'h',  'i');$b = array(  'ab' => '10',  'af' => '11',  'gb' => '16',  'fg' => '17',  'bc' => '18',  'bi' => '12',  'ci' => '8',  'cd' => '22',  'di' => '21',  'dg' => '24',  'gh' => '19',  'dh' => '16',  'de' => '20',  'eh' => '7',  'fe' => '26');$test = new Edge($a, $b);print_r($test->kruscal());?>

edge.php檔案代碼如下:

<?php//邊集數組的邊類class EdgeArc {  private $begin; //起始點  private $end; //結束點  private $weight; //權值  public function EdgeArc($begin, $end, $weight) {    $this->begin = $begin;    $this->end = $end;    $this->weight = $weight;  }  public function getBegin() {    return $this->begin;  }  public function getEnd() {    return $this->end;  }  public function getWeight() {    return $this->weight;  }}class Edge {  //邊集數組實現圖  private $vexs; //頂點集合  private $arc; //邊集合  private $arcData; //要構建圖的邊資訊  private $krus; //kruscal演算法時存放森林資訊  public function Edge($vexsData, $arcData) {    $this->vexs = $vexsData;    $this->arcData = $arcData;    $this->createArc();  }  //建立邊  private function createArc() {    foreach ($this->arcData as $key => $value) {      $key = str_split($key);      $this->arc[] = new EdgeArc($key[0], $key[1], $value);    }  }  //對邊數組按權值排序  public function sortArc() {    $this->quicklySort(0, count($this->arc) - 1, $this->arc);    return $this->arc;  }  //採用快排  private function quicklySort($begin, $end, &$item) {    if ($begin < 0($begin >= $end)) return;    $key = $this->excuteSort($begin, $end, $item);    $this->quicklySort(0, $key - 1, $item);    $this->quicklySort($key + 1, $end, $item);  }  private function excuteSort($begin, $end, &$item) {    $key = $item[$begin];    $left = array();    $right = array();    for ($i = ($begin + 1); $i <= $end; $i++) {      if ($item[$i]->getWeight() <= $key->getWeight()) {        $left[] = $item[$i];      } else {        $right[] = $item[$i];      }    }    $return = $this->unio($left, $right, $key);    $k = 0;    for ($i = $begin; $i <= $end; $i++) {      $item[$i] = $return[$k];      $k++;    }    return $begin + count($left);  }  private function unio($left, $right, $key) {    return array_merge($left, array(      $key    ) , $right);  }  //kruscal演算法  public function kruscal() {    $this->krus = array();    $this->sortArc();    foreach ($this->vexs as $value) {      $this->krus[$value] = "0";    }    foreach ($this->arc as $key => $value) {      $begin = $this->findRoot($value->getBegin());      $end = $this->findRoot($value->getEnd());      if ($begin != $end) {        $this->krus[$begin] = $end;        echo $value->getBegin() . "-" . $value->getEnd() . ":" . $value->getWeight() . "\n";      }    }  }  //尋找子樹的尾結點  private function findRoot($node) {    while ($this->krus[$node] != "0") {      $node = $this->krus[$node];    }    return $node;  }}?> 

感興趣的讀者可以調試運行一下本文克魯斯卡爾演算法執行個體,相信會有新的收穫。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.