關鍵路徑:php實現圖的鄰接表,關鍵路徑,拓樸排序

來源:互聯網
上載者:User

<?php
//調用
require 'algraph.php';
$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
$e = array('ab'=>'3', 'ac'=>'4', 'be'=>'6', 'bd'=>'5', 'cd'=>'8', 'cf'=>'7', 'de'=>'3', 'eg'=>'9', 'eh'=>'4', 'fh'=>'6', 'gj'=>'2', 'hi'=>'5', 'ij'=>'3');
$test = new algraph($a, $e, 1, 1);
print_r($test->criticalpath());
?>
algraph.php
<?php
/**
* php實現圖的鄰接表
*
* @author zhaojiangwei
* @since 2011/11/1 16:00
*/
//頂點類
class vex{
private $data;
private $headlink;//第一條邊
private $enterlimit = 0;//頂點的入度
public function vex($data, $headlink = null){
$this->data = $data;
$this->headlink = $headlink;
}
//入度加+n
public function enterlimitadd($n = 1){
$this->enterlimit += $n;
}
public function getenterlimit(){
return $this->enterlimit;
}
public function getdata(){
return $this->data;
}
public function getheadlink(){
return $this->headlink;
}
public function setheadlink(& $link){
$this->headlink = $link;
}
}
//邊類
class arc{
private $key;//該邊頂點對應在頂點數組的下標
private $weight;//路徑長度
private $next;//下一條邊
public function arc($key, $weight = null, $next = null){
$this->key= $key;
$this->next = $next;
$this->weight= $weight;
}
public function getweight(){
return $this->weight;
}
public function getkey(){
return $this->key;
}
public function getnext(){
return $this->next;
}
public function setnext($next){
$this->next = $next;
}
}
//鄰接表類
class algraph{
private $vexsdata;//外部輸入的頂點資料,類似如array('a', 'b');
private $vexs;//頂點數組
private $arcdata;//外部輸入的邊資料,如array('ab'=>'3'),鍵為邊,值為權值
private $excutedfsresult;//深度優先遍曆後的字串結果
private $haslist; //遍曆時儲存遍曆過的結點下標
private $queue; //廣度優先遍曆時的儲存隊列
private $direct; //是否是有向圖,0為無向,1為有向
private $weight; //是否帶權,0不帶,1帶
//$direct:是否是有向圖,0無向,1有向
//$weight:是否帶權,0不帶,1帶
public function algraph($vexsdata, $arcdata, $direct = 0, $weight = 0){
$this->vexsdata = $vexsdata;
$this->arcdata = $arcdata;
$this->direct = $direct;
$this->weight = $weight;
$this->createheadlist();
$this->createarc();
}
//建立頂點數組
private function createheadlist(){
foreach($this->vexsdata as $value){
$this->vexs[] = new vex($value);
}
}
//建立邊表
private function createarc(){
switch($this->weight){
case '0'://不帶權
$this->createnoweightarc();
break;
case '1'://帶權
$this->createweightarc();
break;
}
}
//建立帶權表
private function createweightarc(){
foreach($this->arcdata as $key=>$value){
$edgenode = str_split($key);
$this->createconnect($edgenode[0], $edgenode[1], $value);
if(!$this->direct){//有向圖
$this->createconnect($edgenode[1], $edgenode[0], $value);
}
}
}
//建立不帶權表
private function createnoweightarc(){
foreach($this->arcdata as $value){
$str = str_split($value);
$this->createconnect($str[0], $str[1]);
if(!$this->direct){
$this->createconnect($str[1], $str[0]);
}
}
}
//依附於邊的倆頂點建立關係
//$weight: 權值,預設為無權值
private function createconnect($first, $last, $weight = null){
$lasttemp=& $this->vexs[$this->getvexbyvalue($last)];
$lasttemp->enterlimitadd(1);//入度+1
$firstnode =& $this->vexs[$this->getvexbyvalue($first)];
$lastnode = new arc($this->getvexbyvalue($last), $weight);
$lastnode->setnext($firstnode->getheadlink());
$firstnode->setheadlink(& $lastnode); 本文連結http://www.cxybl.com/html/wlbc/Php/20120607/28508.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.