欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP实现克鲁斯卡尔算法

程序员文章站 2022-06-15 11:40:50
...
PHP实现的格鲁斯卡尔算法(kruscal),如下代码:
  1. require 'edge.php';
  2. $a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
  3. $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');
  4. $test = new Edge($a, $b);
  5. print_r($test->kruscal());
  6. ?>
  7. //边集数组的边类
  8. class EdgeArc{
  9. private $begin;//起始点
  10. private $end;//结束点
  11. private $weight;//权值
  12. public function EdgeArc($begin, $end, $weight){
  13. $this->begin = $begin;
  14. $this->end = $end;
  15. $this->weight = $weight;
  16. }
  17. public function getBegin(){
  18. return $this->begin;
  19. }
  20. public function getEnd(){
  21. return $this->end;
  22. }
  23. public function getWeight(){
  24. return $this->weight;
  25. }
  26. }
  27. class Edge{
  28. //边集数组实现图
  29. private $vexs;//顶点集合
  30. private $arc;//边集合
  31. private $arcData;//要构建图的边信息
  32. private $krus;//kruscal算法时存放森林信息
  33. public function Edge($vexsData, $arcData){
  34. $this->vexs = $vexsData;
  35. $this->arcData = $arcData;
  36. $this->createArc();
  37. }
  38. //创建边
  39. private function createArc(){
  40. foreach($this->arcData as $key=>$value){
  41. $key = str_split($key);
  42. $this->arc[] = new EdgeArc($key[0], $key[1], $value);
  43. }
  44. }
  45. //对边数组按权值排序
  46. public function sortArc(){
  47. $this->quicklySort(0, count($this->arc) - 1, $this->arc);
  48. return $this->arc;
  49. }
  50. //采用快排
  51. private function quicklySort($begin, $end, & $item){
  52. if($begin $begin >= $end))
  53. return;
  54. $key = $this->excuteSort($begin, $end, $item);
  55. $this->quicklySort(0, $key - 1, $item);
  56. $this->quicklySort($key + 1, $end, $item);
  57. }
  58. private function excuteSort($begin, $end, & $item){
  59. $key = $item[$begin];
  60. $left = array();
  61. $right = array();
  62. for($i = ($begin + 1); $i $end; $i ++){
  63. if($item[$i]->getWeight() $key->getWeight()){
  64. $left[] = $item[$i];
  65. }else{
  66. $right[] = $item[$i];
  67. }
  68. }
  69. $return = $this->unio($left, $right, $key);
  70. $k = 0;
  71. for($i = $begin; $i $end; $i ++){
  72. $item[$i] = $return[$k];
  73. $k ++;
  74. }
  75. return $begin + count($left);
  76. }
  77. private function unio($left, $right, $key){
  78. return array_merge($left, array($key), $right);
  79. }
  80. //kruscal算法
  81. public function kruscal(){
  82. $this->krus = array();
  83. $this->sortArc();
  84. foreach($this->vexs as $value){
  85. $this->krus[$value] = "0";
  86. }
  87. foreach($this->arc as $key=>$value){
  88. $begin = $this->findRoot($value->getBegin());
  89. $end = $this->findRoot($value->getEnd());
  90. if($begin != $end){
  91. $this->krus[$begin] = $end;
  92. echo $value->getBegin() . "-" . $value->getEnd() . ":" . $value->getWeight() . "\n";
  93. }
  94. }
  95. }
  96. //查找子树的尾结点
  97. private function findRoot($node){
  98. while($this->krus[$node] != "0"){
  99. $node = $this->krus[$node];
  100. }
  101. return $node;
  102. }
  103. }
  104. ?>