Adjacency matrix prim:php and Primm (prim algorithm), Freud (Floyd), Dijkstra (Dijkstra) algorithm
Last Update:2017-02-28
Source: Internet
Author: User
<?php
Require ' mgraph.php ';
$a = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ');
$b = Array (' AB ' => ', ' af ' => ', ' bg ' => ', ' FG ' => ', ' BC ' => ', ' bi ' => ', ' ci ' => ' 8 ', ' CD ' => ', ' di ' => ', ' DG ' => ', ' gh ' => ', ' dh ' => ', ' de ' => ', ' eh ' => ' 7 ', ' Fe ' => ' 26 '); Key is edge, value weight
$test = new Mgraph ($a, $b);
Print_r ($test->prim ());
?>
mgraph.php
<?php
/**
* PHP implementation diagram adjacency matrix
*
* @author Zhaojiangwei
* @since 2011/10/31 17:23
*/
Class mgraph{
Private $vexs; Vertex array
Private $arc; Edge adjacency matrix, or two-dimensional array
Private $arcdata; Array information for edges
Private $direct; Type of diagram (no direction or direction)
Private $haslist; Try to store the traversed nodes.
Private $queue; Breadth-first-time queues that store children's nodes, modeled with arrays
Private $infinity = 65535;//represents infinity, that is, two-point no connection, the use of a weighted map, this example does not have a weighted value
Private $primvexs; Save vertices when prim algorithm
Private $primarc; Save Edge when Prim algorithm
private $krus;//kruscal information to save edges when the algorithm is used
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);
}
}
}
Create a figure $direct: 0 for a graph without direction, 1 for a forward graph
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 algorithm
Public Function Floyd () {
$path = Array ();//path arrays
$distance = Array ();//Distance arrays
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 algorithm
Public Function Dijkstra () {
$final = Array ();
$pre = Array ();//The previous node set of the node to find
$weight = Array ();//weight value and array
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 algorithm
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); This article links http://www.cxybl.com/html/wlbc/Php/20120607/28507.html