PHP實(shí)現(xiàn)克魯斯卡爾算法實(shí)例解析
本文實(shí)例展示了PHP實(shí)現(xiàn)的格魯斯卡爾算法(kruscal)的實(shí)現(xiàn)方法,分享給大家供大家參考。相信對(duì)于大家的PHP程序設(shè)計(jì)有一定的借鑒價(jià)值。
具體代碼如下:
<?php require '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
//邊集數(shù)組的邊類
class EdgeArc {
private $begin; //起始點(diǎn)
private $end; //結(jié)束點(diǎn)
private $weight; //權(quán)值
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 {
//邊集數(shù)組實(shí)現(xiàn)圖
private $vexs; //頂點(diǎn)集合
private $arc; //邊集合
private $arcData; //要構(gòu)建圖的邊信息
private $krus; //kruscal算法時(shí)存放森林信息
public function Edge($vexsData, $arcData) {
$this->vexs = $vexsData;
$this->arcData = $arcData;
$this->createArc();
}
//創(chuàng)建邊
private function createArc() {
foreach ($this->arcData as $key => $value) {
$key = str_split($key);
$this->arc[] = new EdgeArc($key[0], $key[1], $value);
}
}
//對(duì)邊數(shù)組按權(quán)值排序
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";
}
}
}
//查找子樹的尾結(jié)點(diǎn)
private function findRoot($node) {
while ($this->krus[$node] != "0") {
$node = $this->krus[$node];
}
return $node;
}
}
?>
感興趣的讀者可以調(diào)試運(yùn)行一下本文克魯斯卡爾算法實(shí)例,相信會(huì)有新的收獲。
相關(guān)文章
PHP實(shí)現(xiàn)限制IP訪問及提交次數(shù)的方法詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)限制IP訪問及提交次數(shù)的方法,涉及php針對(duì)客戶端來訪IP的獲取、判斷以及結(jié)合session記錄IP訪問次數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
SESSION存放在數(shù)據(jù)庫(kù)用法實(shí)例
這篇文章主要介紹了SESSION存放在數(shù)據(jù)庫(kù)用法,自定義了一個(gè)簡(jiǎn)單的針對(duì)數(shù)據(jù)操作的session類并給出了使用該類存儲(chǔ)到數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下2015-08-08
PHP+MySQL實(shí)現(xiàn)輸入頁(yè)碼跳轉(zhuǎn)到指定頁(yè)面功能示例
這篇文章主要介紹了PHP+MySQL實(shí)現(xiàn)輸入頁(yè)碼跳轉(zhuǎn)到指定頁(yè)面功能,結(jié)合實(shí)例形式分析了php連接mysql數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)查詢及分頁(yè)顯示、指定頁(yè)數(shù)跳轉(zhuǎn)顯示等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
php采集中國(guó)代理服務(wù)器網(wǎng)的方法
這篇文章主要介紹了php采集中國(guó)代理服務(wù)器網(wǎng)的方法,涉及php采集的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06

