html5中文学习网

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

PHP实现图的邻接表_PHP教程_编程技术

[ ] 已经帮助:人解决问题
  1. <?php 
  2.     //调用 
  3.     require 'alGraph.php'
  4.     $a = array('a''b''c''d''e''f''g''h''i''j'); 
  5.     $b = array('ab''bc''be''cd''df''fg''gh''ga''hj''gi'); 
  6.  
  7.     $test = new ALGraph($a$b); 
  8.     print_r($test->bfs()); 
  9. ?> 
  10. alGraph.php 
  11. <?php 
  12.     //顶点类 
  13.     class Vex{ 
  14.         private $data
  15.         private $headLink
  16.  
  17.         public function Vex($data$headLink = NULL){ 
  18.             $this->data = $data
  19.             $this->headLink = $headLink
  20.         } 
  21.  
  22.         public function getData(){ 
  23.             return $this->data; 
  24.         } 
  25.  
  26.         public function getHeadLink(){ 
  27.             return $this->headLink; 
  28.         } 
  29.  
  30.         public function setHeadLink(& $link){ 
  31.             $this->headLink = $link
  32.         } 
  33.     } 
  34.     
  35.     //边类 
  36.     class Arc{ 
  37.         private $key
  38.         private $next
  39.           
  40.         public function Arc($key$next = NULL){ 
  41.             $this->key= $key
  42.             $this->next = $next;  
  43.         } 
  44.  
  45.         public function getKey(){ 
  46.             return $this->key; 
  47.         } 
  48.  
  49.         public function getNext(){ 
  50.             return $this->next; 
  51.         } 
  52.  
  53.         public function setNext($next){ 
  54.             $this->next = $next
  55.         } 
  56.     } 
  57.      
  58.     //邻接表类 
  59.     class ALGraph{ 
  60.         private $vexsData
  61.         private $vexs
  62.         private $arcData
  63.         private $excuteDfsResult;//深度优先遍历后的字符串结果 
  64.         private $hasList//遍历时储存遍历过的结点下标 
  65.         private $queue//广度优先遍历时的存储队列 
  66.          
  67.         public function ALGraph($vexsData$arcData){ 
  68.             $this->vexsData = $vexsData
  69.             $this->arcData = $arcData
  70.  
  71.             $this->createHeadList(); 
  72.             $this->createArc();  
  73.         } 
  74.  
  75.         //创建顶点数组 
  76.         private function createHeadList(){ 
  77.             foreach($this->vexsData as $value){ 
  78.                 $this->vexs[] = new Vex($value);  
  79.             } 
  80.         } 
  81.  
  82.         //创建边表 
  83.         private function createArc(){ 
  84.             foreach($this->arcData as $value){ 
  85.                 $str = str_split($value); 
  86.                  
  87.                 $this->createConnect($str[0], $str[1]); 
  88.                 $this->createConnect($str[1], $str[0]); 
  89.             } 
  90.         } 
  91.  
  92.         //依附于边的俩顶点建立关系 
  93.         private function createConnect($first$last){ 
  94.             $firstNode = & $this->vexs[$this->getVexByValue($first)]; 
  95.             $lastNode = new Arc($this->getVexByValue($last)); 
  96.  
  97.             $lastNode->setNext($firstNode->getHeadLink()); 
  98.             $firstNode->setHeadLink(& $lastNode); 
  99.         } 
  100.  
  101.         //根据顶点的值返回顶点在顶点数组中的下标 
  102.         private function getVexByValue($value){ 
  103.             foreach($this->vexs as $k=>$v){ 
  104.                 if($v->getData() == $value){ 
  105.                     return $k;  
  106.                 } 
  107.             } 
  108.         } 
  109.  
  110.         //广度优先遍历 
  111.         public function bfs(){ 
  112.             $this->hasList = array(); 
  113.             $this->queue = array(); 
  114.              
  115.             foreach($this->vexs as $key=>$value){ 
  116.                 if(!in_array($value->getData(), $this->hasList)){ 
  117.                     $this->hasList[] = $value->getData(); 
  118.                     $this->queue[] = $value->getHeadLink(); 
  119.                       
  120.                     while(!emptyempty($this->queue)){ 
  121.                         $node = array_shift($this->queue); 
  122.                          
  123.                         while($node){ 
  124.                             if(!in_array($this->vexs[$node->getKey()]->getData(), $this->hasList)){ 
  125.                                 $info = $this->vexs[$node->getKey()]; 
  126.                                 $this->hasList[] = $info->getData(); 
  127.                                 $this->queue[] = $info->getHeadLink(); 
  128.                             } 
  129.  
  130.                             $node = $node->getNext();  
  131.                         } 
  132.                     } 
  133.                 } 
  134.             } 
  135.  
  136.             return implode($this->hasList); 
  137.         } 
  138.  
  139.         //深度优先遍历入口 
  140.         public function dfs(){ 
  141.             $this->hasList = array(); 
  142.  
  143.             foreach($this->vexs as $key=>$value){ 
  144.                 if(!in_array($key$this->hasList)){ 
  145.                     $this->hasList[] = $key
  146.                     $this->excuteDfs($this->vexs[$key]->getHeadLink()); 
  147.                 } 
  148.             } 
  149.  
  150.             foreach($this->hasList as $key=>$value){ 
  151.                 $this->hasList[$key] = $this->vexs[$value]->getData();    
  152.             } 
  153.  
  154.             return implode($this->hasList); 
  155.         } 
  156.  
  157.         //执行深度遍历 
  158.         private function excuteDfs($arc){ 
  159.             if(!$arc  in_array($arc->getKey(), $this->hasList)){ 
  160.                 return false;    
  161.             } 
  162.  
  163.             $this->hasList[] = $arc->getKey(); 
  164.             $next = $this->vexs[$arc->getKey()]->getHeadLink(); 
  165.          
  166.             while($next){   
  167.                 $this->excuteDfs($next); 
  168.                 $next = $next->getNext(); 
  169.             } 
  170.         } 
  171.  
  172.         public function getVexs(){ 
  173.             return $this->vexs; 
  174.         } 
  175.     } 
  176. ?> 
DFEHTML5中文学习网 - HTML5先行者学习网
DFEHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助