html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP实现克鲁斯卡尔算法_PHP教程_编程技术

[ ] 已经帮助:人解决问题

PHP实现的格鲁斯卡尔算法(kruscal),如下代码:Ad3HTML5中文学习网 - HTML5先行者学习网

  1. <?php 
  2.     require 'edge.php'
  3.     $a = array('a''b''c''d''e''f''g''h''i'); 
  4.     $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'); 
  5.       
  6.     $test = new Edge($a$b); 
  7.     print_r($test->kruscal()); 
  8. ?> 
  9.  
  10. <?php  
  11.  
  12.     //边集数组的边类 
  13.     class EdgeArc{ 
  14.         private $begin;//起始点 
  15.         private $end;//结束点 
  16.         private $weight;//权值 
  17.  
  18.         public function EdgeArc($begin$end$weight){ 
  19.             $this->begin = $begin
  20.             $this->end = $end
  21.             $this->weight = $weight
  22.         } 
  23.  
  24.         public function getBegin(){ 
  25.             return $this->begin; 
  26.         } 
  27.  
  28.         public function getEnd(){ 
  29.             return $this->end
  30.         } 
  31.  
  32.         public function getWeight(){ 
  33.             return $this->weight; 
  34.         } 
  35.     } 
  36.  
  37.  
  38. class Edge{ 
  39.     //边集数组实现图 
  40.  
  41.     private $vexs;//顶点集合 
  42.     private $arc;//边集合 
  43.     private $arcData;//要构建图的边信息 
  44.     private $krus;//kruscal算法时存放森林信息 
  45.  
  46.     public function Edge($vexsData$arcData){ 
  47.         $this->vexs = $vexsData
  48.         $this->arcData = $arcData
  49.         $this->createArc(); 
  50.     } 
  51.  
  52.     //创建边 
  53.     private function createArc(){ 
  54.         foreach($this->arcData as $key=>$value){ 
  55.             $key = str_split($key); 
  56.             $this->arc[] = new EdgeArc($key[0], $key[1], $value); 
  57.         } 
  58.     } 
  59.  
  60.     //对边数组按权值排序 
  61.     public function sortArc(){ 
  62.         $this->quicklySort(0, count($this->arc) - 1, $this->arc);  
  63.         return $this->arc;  
  64.     } 
  65.  
  66.     //采用快排 
  67.     private function quicklySort($begin$end, & $item){ 
  68.         if($begin < 0  ($begin >= $end)) 
  69.             return
  70.          
  71.         $key = $this->excuteSort($begin$end$item); 
  72.         $this->quicklySort(0, $key - 1, $item);  
  73.         $this->quicklySort($key + 1, $end$item); 
  74.     } 
  75.  
  76.     private function excuteSort($begin$end, & $item){ 
  77.         $key = $item[$begin]; 
  78.         $left = array(); 
  79.         $right = array(); 
  80.           
  81.         for($i = ($begin + 1); $i <= $end$i ++){ 
  82.             if($item[$i]->getWeight() <= $key->getWeight()){ 
  83.                 $left[] = $item[$i];    
  84.             }else
  85.                 $right[] = $item[$i]; 
  86.             } 
  87.         } 
  88.          
  89.         $return = $this->unio($left$right$key); 
  90.          
  91.         $k = 0; 
  92.         for($i = $begin$i <= $end$i ++){ 
  93.             $item[$i] = $return[$k]; 
  94.             $k ++; 
  95.         } 
  96.  
  97.         return $begin + count($left); 
  98.     } 
  99.  
  100.     private function unio($left$right$key){ 
  101.         return array_merge($leftarray($key), $right); 
  102.     } 
  103.  
  104.     //kruscal算法 
  105.     public function kruscal(){ 
  106.         $this->krus = array(); 
  107.         $this->sortArc(); 
  108.  
  109.         foreach($this->vexs as $value){ 
  110.             $this->krus[$value] = "0"
  111.         } 
  112.  
  113.         foreach($this->arc as $key=>$value){ 
  114.             $begin = $this->findRoot($value->getBegin()); 
  115.             $end = $this->findRoot($value->getEnd()); 
  116.              
  117.             if($begin != $end){ 
  118.                 $this->krus[$begin] = $end
  119.                 echo $value->getBegin() . "-" . $value->getEnd() . ":" . $value->getWeight() . "/n"
  120.             } 
  121.         } 
  122.     } 
  123.  
  124.     //查找子树的尾结点 
  125.     private function findRoot($node){ 
  126.         while($this->krus[$node] != "0"){ 
  127.             $node = $this->krus[$node]; 
  128.         } 
  129.          
  130.         return $node
  131.     } 
  132. ?> 
Ad3HTML5中文学习网 - HTML5先行者学习网
Ad3HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助