<?php
Call
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 ', ' The ' => ', ' 2 ', ' Hi ' => ' 5 ', ' ij ' => ' 3 ');
$test = new Algraph ($a, $e, 1, 1);
Print_r ($test->criticalpath ());
?>
algraph.php
<?php
/**
* The adjacency table of the PHP implementation diagram
*
* @author Zhaojiangwei
* @since 2011/11/1 16:00
*/
Vertex class
Class vex{
Private $data;
private $headlink;//First Edge
Private $enterlimit = entry of 0;//vertices
Public Function Vex ($data, $headlink = null) {
$this->data = $data;
$this->headlink = $headlink;
}
Into the degree plus +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;
}
}
Edge class
Class arc{
Private $key//The edge vertex corresponds to the subscript of the vertex array
private $weight;//Path length
Private $next/Next side
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;
}
}
Adjacency Table Class
Class algraph{
private $vexsdata;//external input vertex data similar to array (' A ', ' B ');
private $vexs;//vertex array
Private $arcdata//external input edge data, such as array (' AB ' => ' 3 '), key is edge, value is weighted
private $excutedfsresult;//Depth-first traversal string result
Private $haslist; Over-time storage of traversed node subscript
Private $queue; Storage queues for breadth-first traversal
Private $direct; Is there a direction graph, 0 is no direction, 1 is a forward
Private $weight; Whether with the right, 0 without, 1 belt
$direct: Is there a direction graph, 0 without direction, 1 to
$weight: Whether with the right, 0 without, 1 belt
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 ();
}
To create an array of vertices
Private Function Createheadlist () {
foreach ($this->vexsdata as $value) {
$this->vexs[] = new Vex ($value);
}
}
Create a side table
Private Function Createarc () {
Switch ($this->weight) {
Case ' 0 '://Without Right
$this->createnoweightarc ();
Break
Case ' 1 '://With Right
$this->createweightarc ();
Break
}
}
Create a weighted table
Private Function Createweightarc () {
foreach ($this->arcdata as $key => $value) {
$edgenode = Str_split ($key);
$this->createconnect ($edgenode [0], $edgenode [1], $value);
if (! $this->direct) {//forward graph
$this->createconnect ($edgenode [1], $edgenode [0], $value);
}
}
}
Create a table with no rights
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]);
}
}
}
Building relationships with two vertices attached to an edge
$weight: Weight, default to no permission value
Private Function Createconnect ($first, $last, $weight = null) {
$lasttemp =& $this->vexs[$this->getvexbyvalue ($last)];
$lasttemp->enterlimitadd (1);//Enter degree +1
$firstnode =& $this->vexs[$this->getvexbyvalue ($first)];
$lastnode = new Arc ($this->getvexbyvalue ($last), $weight);
$lastnode->setnext ($firstnode->getheadlink ());
$firstnode->setheadlink (& $lastnode); This article links http://www.cxybl.com/html/wlbc/Php/20120607/28508.html
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.