PHP实现克鲁斯卡尔算法实例解析
作者:shichen2014 时间:2023-09-08 19:35:57
本文实例展示了PHP实现的格鲁斯卡尔算法(kruscal)的实现方法,分享给大家供大家参考。相信对于大家的PHP程序设计有一定的借鉴价值。
具体代码如下:
<?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
//边集数组的边类
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;
}
}
?>
感兴趣的读者可以调试运行一下本文克鲁斯卡尔算法实例,相信会有新的收获。
标签:PHP,算法
0
投稿
猜你喜欢
说说回车键触发表单提交的问题
2009-02-03 13:25:00
Python爬虫之爬取二手房信息
2021-08-11 19:40:50
ASP程序种如何调用DLL文件
2008-01-15 19:12:00
安装mysql 8.0.17并配置远程访问的方法
2024-01-25 06:58:24
python数字图像处理之高级滤波代码详解
2022-06-30 15:02:09
JavaScript,5种调用函数的方法[译]
2009-02-24 16:26:00
asp.net 字符串、二进制、编码数组转换函数
2024-03-10 07:43:23
IE在DOM操作有表单控件时的bug
2008-08-21 13:00:00
利用ASP发送和接收XML数据的处理方法
2009-02-02 08:57:00
Tensorflow加载模型实现图像分类识别流程详解
2023-12-22 02:31:13
Python 中random 库的详细使用
2022-01-19 05:35:14
SQLSERVER 本地查询更新操作远程数据库的代码
2024-01-21 05:16:47
python学习笔记之调用eval函数出现invalid syntax错误问题
2023-11-03 01:48:30
一个不错的javascript加密解密算法源码
2010-03-28 13:12:00
Python爬取商家联系电话以及各种数据的方法
2023-07-24 18:39:38
python实现字典(dict)和字符串(string)的相互转换方法
2021-10-19 18:22:44
django admin 根据choice字段选择的不同来显示不同的页面方式
2022-04-26 06:39:10
Access报错:文件共享锁定数溢出
2009-03-21 18:32:00
基于js实现的限制文本框只可以输入数字
2024-04-25 13:06:46
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
2023-11-22 20:04:41