|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
告诉你了一个方式,但是缺少努力这一环节,那也是白搭。 - <?php //挪用
require 'alGraph.php'; $a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'); $b = array('ab', 'bc', 'be', 'cd', 'df', 'fg', 'gh', 'ga', 'hj', 'gi'); $test = new ALGraph($a, $b); print_r($test->bfs()); ?> alGraph.php <?php //极点
类 class Vex{ private $data; private $headLink; public function Vex($data, $headLink = NULL){ $this->data = $data; $this->headLink = $headLink; } public function getData(){ return $this->data; } public function getHeadLink(){ return $this->headLink; } public function setHeadLink(& $link){ $this->headLink = $link; } } //边类 class Arc{ private $key; private $next; public function Arc($key, $next = NULL){ $this->key= $key; $this->next = $next; } public function getKey(){ return $this->key; } public function getNext(){ return $this->next; } public function setNext($next){ $this->next = $next; } } //邻接表类 class ALGraph{ private $vexsData; private $vexs; private $arcData; private $excuteDfsResult;//深度优先遍历后的字符串了局
private $hasList; //遍用时
贮存
遍历过的结点下标 private $queue; //广度优先遍用时
的存储队列 public function ALGraph($vexsData, $arcData){ $this->vexsData = $vexsData; $this->arcData = $arcData; $this->createHeadList(); $this->createArc(); } //创立
极点
数组 private function createHeadList(){ foreach($this->vexsData as $value){ $this->vexs[] = new Vex($value); } } //创立
边表 private function createArc(){ foreach($this->arcData as $value){ $str = str_split($value); $this->createConnect($str[0], $str[1]); $this->createConnect($str[1], $str[0]); } } //依靠
于边的俩极点
创立
关系 private function createConnect($first, $last){ $firstNode = & $this->vexs[$this->getVexByValue($first)]; $lastNode = new Arc($this->getVexByValue($last)); $lastNode->setNext($firstNode->getHeadLink()); $firstNode->setHeadLink(& $lastNode); } //依据
极点
的值前往
极点
在极点
数组中的下标 private function getVexByValue($value){ foreach($this->vexs as $k=>$v){ if($v->getData() == $value){ return $k; } } } //广度优先遍历 public function bfs(){ $this->hasList = array(); $this->queue = array(); foreach($this->vexs as $key=>$value){ if(!in_array($value->getData(), $this->hasList)){ $this->hasList[] = $value->getData(); $this->queue[] = $value->getHeadLink(); while(!emptyempty($this->queue)){ $node = array_shift($this->queue); while($node){ if(!in_array($this->vexs[$node->getKey()]->getData(), $this->hasList)){ $info = $this->vexs[$node->getKey()]; $this->hasList[] = $info->getData(); $this->queue[] = $info->getHeadLink(); } $node = $node->getNext(); } } } } return implode($this->hasList); } //深度优先遍历进口
public function dfs(){ $this->hasList = array(); foreach($this->vexs as $key=>$value){ if(!in_array($key, $this->hasList)){ $this->hasList[] = $key; $this->excuteDfs($this->vexs[$key]->getHeadLink()); } } foreach($this->hasList as $key=>$value){ $this->hasList[$key] = $this->vexs[$value]->getData(); } return implode($this->hasList); } //履行
深度遍历 private function excuteDfs($arc){ if(!$arc in_array($arc->getKey(), $this->hasList)){ return false; } $this->hasList[] = $arc->getKey(); $next = $this->vexs[$arc->getKey()]->getHeadLink(); while($next){ $this->excuteDfs($next); $next = $next->getNext(); } } public function getVexs(){ return $this->vexs; } } ?>
复制代码 不可能吃饭的时候咬了自己一下舌头就从此不吃饭了不是?放下畏惧,继续努力,咱们是来征服它的,而不是被它征服的,振奋起来吧同志。 |
|