相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說明
- thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
PHP實(shí)現(xiàn)圖的鄰接矩陣表示及幾種簡(jiǎn)單遍歷算法分析
本文實(shí)例講述了PHP實(shí)現(xiàn)圖的鄰接矩陣表示及幾種簡(jiǎn)單遍歷算法。分享給大家供大家參考,具體如下:
在web開發(fā)中圖這種數(shù)據(jù)結(jié)構(gòu)的應(yīng)用比樹要少很多,但在一些業(yè)務(wù)中也常有出現(xiàn),下面介紹幾種圖的尋徑算法,并用PHP加以實(shí)現(xiàn).
佛洛依德算法,主要是在頂點(diǎn)集內(nèi),按點(diǎn)與點(diǎn)相鄰邊的權(quán)重做遍歷,如果兩點(diǎn)不相連則權(quán)重?zé)o窮大,這樣通過多次遍歷可以得到點(diǎn)到點(diǎn)的最短路徑,邏輯上最好理解,實(shí)現(xiàn)也較為簡(jiǎn)單,時(shí)間復(fù)雜度為O(n^3);
迪杰斯特拉算法,OSPF中實(shí)現(xiàn)最短路由所用到的經(jīng)典算法,djisktra算法的本質(zhì)是貪心算法,不斷的遍歷擴(kuò)充頂點(diǎn)路徑集合S,一旦發(fā)現(xiàn)更短的點(diǎn)到點(diǎn)路徑就替換S中原有的最短路徑,完成所有遍歷后S便是所有頂點(diǎn)的最短路徑集合了.迪杰斯特拉算法的時(shí)間復(fù)雜度為O(n^2);
克魯斯卡爾算法,在圖內(nèi)構(gòu)造最小生成樹,達(dá)到圖中所有頂點(diǎn)聯(lián)通.從而得到最短路徑.時(shí)間復(fù)雜度為O(N*logN);
<?php /** * PHP 實(shí)現(xiàn)圖鄰接矩陣 */ class MGraph{ private $vexs; //頂點(diǎn)數(shù)組 private $arc; //邊鄰接矩陣,即二維數(shù)組 private $arcData; //邊的數(shù)組信息 private $direct; //圖的類型(無向或有向) private $hasList; //嘗試遍歷時(shí)存儲(chǔ)遍歷過的結(jié)點(diǎn) private $queue; //廣度優(yōu)先遍歷時(shí)存儲(chǔ)孩子結(jié)點(diǎn)的隊(duì)列,用數(shù)組模仿 private $infinity = 65535;//代表無窮,即兩點(diǎn)無連接,建帶權(quán)值的圖時(shí)用,本示例不帶權(quán)值 private $primVexs; //prim算法時(shí)保存頂點(diǎn) private $primArc; //prim算法時(shí)保存邊 private $krus;//kruscal算法時(shí)保存邊的信息 public function MGraph($vexs, $arc, $direct = 0){ $this->vexs = $vexs; $this->arcData = $arc; $this->direct = $direct; $this->initalizeArc(); $this->createArc(); } private function initalizeArc(){ foreach($this->vexs as $value){ foreach($this->vexs as $cValue){ $this->arc[$value][$cValue] = ($value == $cValue ? 0 : $this->infinity); } } } //創(chuàng)建圖 $direct:0表示無向圖,1表示有向圖 private function createArc(){ foreach($this->arcData as $key=>$value){ $strArr = str_split($key); $first = $strArr[0]; $last = $strArr[1]; $this->arc[$first][$last] = $value; if(!$this->direct){ $this->arc[$last][$first] = $value; } } } //floyd算法 public function floyd(){ $path = array();//路徑數(shù)組 $distance = array();//距離數(shù)組 foreach($this->arc as $key=>$value){ foreach($value as $k=>$v){ $path[$key][$k] = $k; $distance[$key][$k] = $v; } } for($j = 0; $j < count($this->vexs); $j ++){ for($i = 0; $i < count($this->vexs); $i ++){ for($k = 0; $k < count($this->vexs); $k ++){ if($distance[$this->vexs[$i]][$this->vexs[$k]] > $distance[$this->vexs[$i]][$this->vexs[$j]] + $distance[$this->vexs[$j]][$this->vexs[$k]]){ $path[$this->vexs[$i]][$this->vexs[$k]] = $path[$this->vexs[$i]][$this->vexs[$j]]; $distance[$this->vexs[$i]][$this->vexs[$k]] = $distance[$this->vexs[$i]][$this->vexs[$j]] + $distance[$this->vexs[$j]][$this->vexs[$k]]; } } } } return array($path, $distance); } //djikstra算法 public function dijkstra(){ $final = array(); $pre = array();//要查找的結(jié)點(diǎn)的前一個(gè)結(jié)點(diǎn)數(shù)組 $weight = array();//權(quán)值和數(shù)組 foreach($this->arc[$this->vexs[0]] as $k=>$v){ $final[$k] = 0; $pre[$k] = $this->vexs[0]; $weight[$k] = $v; } $final[$this->vexs[0]] = 1; for($i = 0; $i < count($this->vexs); $i ++){ $key = 0; $min = $this->infinity; for($j = 1; $j < count($this->vexs); $j ++){ $temp = $this->vexs[$j]; if($final[$temp] != 1 && $weight[$temp] < $min){ $key = $temp; $min = $weight[$temp]; } } $final[$key] = 1; for($j = 0; $j < count($this->vexs); $j ++){ $temp = $this->vexs[$j]; if($final[$temp] != 1 && ($min + $this->arc[$key][$temp]) < $weight[$temp]){ $pre[$temp] = $key; $weight[$temp] = $min + $this->arc[$key][$temp]; } } } return $pre; } //kruscal算法 private function kruscal(){ $this->krus = array(); foreach($this->vexs as $value){ $krus[$value] = 0; } foreach($this->arc as $key=>$value){ $begin = $this->findRoot($key); foreach($value as $k=>$v){ $end = $this->findRoot($k); if($begin != $end){ $this->krus[$begin] = $end; } } } } //查找子樹的尾結(jié)點(diǎn) private function findRoot($node){ while($this->krus[$node] > 0){ $node = $this->krus[$node]; } return $node; } //prim算法,生成最小生成樹 public function prim(){ $this->primVexs = array(); $this->primArc = array($this->vexs[0]=>0); for($i = 1; $i < count($this->vexs); $i ++){ $this->primArc[$this->vexs[$i]] = $this->arc[$this->vexs[0]][$this->vexs[$i]]; $this->primVexs[$this->vexs[$i]] = $this->vexs[0]; } for($i = 0; $i < count($this->vexs); $i ++){ $min = $this->infinity; $key; foreach($this->vexs as $k=>$v){ if($this->primArc[$v] != 0 && $this->primArc[$v] < $min){ $key = $v; $min = $this->primArc[$v]; } } $this->primArc[$key] = 0; foreach($this->arc[$key] as $k=>$v){ if($this->primArc[$k] != 0 && $v < $this->primArc[$k]){ $this->primArc[$k] = $v; $this->primVexs[$k] = $key; } } } return $this->primVexs; } //一般算法,生成最小生成樹 public function bst(){ $this->primVexs = array($this->vexs[0]); $this->primArc = array(); next($this->arc[key($this->arc)]); $key = NULL; $current = NULL; while(count($this->primVexs) < count($this->vexs)){ foreach($this->primVexs as $value){ foreach($this->arc[$value] as $k=>$v){ if(!in_array($k, $this->primVexs) && $v != 0 && $v != $this->infinity){ if($key == NULL || $v < current($current)){ $key = $k; $current = array($value . $k=>$v); } } } } $this->primVexs[] = $key; $this->primArc[key($current)] = current($current); $key = NULL; $current = NULL; } return array('vexs'=>$this->primVexs, 'arc'=>$this->primArc); } //一般遍歷 public function reserve(){ $this->hasList = array(); foreach($this->arc as $key=>$value){ if(!in_array($key, $this->hasList)){ $this->hasList[] = $key; } foreach($value as $k=>$v){ if($v == 1 && !in_array($k, $this->hasList)){ $this->hasList[] = $k; } } } foreach($this->vexs as $v){ if(!in_array($v, $this->hasList)) $this->hasList[] = $v; } return implode($this->hasList); } //廣度優(yōu)先遍歷 public function bfs(){ $this->hasList = array(); $this->queue = array(); foreach($this->arc as $key=>$value){ if(!in_array($key, $this->hasList)){ $this->hasList[] = $key; $this->queue[] = $value; while(!empty($this->queue)){ $child = array_shift($this->queue); foreach($child as $k=>$v){ if($v == 1 && !in_array($k, $this->hasList)){ $this->hasList[] = $k; $this->queue[] = $this->arc[$k]; } } } } } return implode($this->hasList); } //執(zhí)行深度優(yōu)先遍歷 public function excuteDfs($key){ $this->hasList[] = $key; foreach($this->arc[$key] as $k=>$v){ if($v == 1 && !in_array($k, $this->hasList)) $this->excuteDfs($k); } } //深度優(yōu)先遍歷 public function dfs(){ $this->hasList = array(); foreach($this->vexs as $key){ if(!in_array($key, $this->hasList)) $this->excuteDfs($key); } return implode($this->hasList); } //返回圖的二維數(shù)組表示 public function getArc(){ return $this->arc; } //返回結(jié)點(diǎn)個(gè)數(shù) public function getVexCount(){ return count($this->vexs); } } $a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'); $b = array('ab'=>'10', 'af'=>'11', 'bg'=>'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');//鍵為邊,值權(quán)值 $test = new MGraph($a, $b); print_r($test->bst());