PHP implementation Kruskal Algorithm instance parsing, Kruskal algorithm example
This example shows the implementation of PHP implementation of the Gruscal algorithm (kruscal), shared for everyone for reference. I believe that the PHP program design for everyone has some reference value.
The specific code is as follows:
<?phprequire ' edge.php '; $a = array ( ' a ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' i '); $b = Array ( ' ab ' + = ', ' af ' = ' + ', ' GB ' = ' + ', ' fg ' = ' + '), ' BC ' =& Gt ' + ', ' bi ' and ' + ', ' ci ' = ' 8 ', ' cd ' = ' ', ' di ' and ' + ', ' dg ' ' + ', ' gh ' = ' + ', ' dh ' = ' + ', ' de ' = ' + ', ' eh ' = ' 7 ', ' fe ' = ' + '); $test = new Edge ($ A, $b);p Rint_r ($test->kruscal ());? >
The edge.php file code is as follows:
The Edge class of the <?php//edge set array class Edgearc {private $begin;//start point private $end;//end point private $weight;//Right value public function Edgea RC ($begin, $end, $weight) {$this->begin = $begin; $this->end = $end; $this->weight = $weight; } public Function Getbegin () {return $this->begin; } public Function Getend () {return $this->end; } public Function Getweight () {return $this->weight; }}class Edge {//edge set array implementation diagram private $vexs;//vertex set private $arc;//Edge collection private $arcData;//To build edge information for graphs private $krus;//krus Cal algorithm when storing forest information public function Edge ($vexsData, $arcData) {$this->vexs = $vexsData; $this->arcdata = $arcData; $this->createarc (); }//Create Edge Private Function Createarc () {foreach ($this->arcdata as $key = + $value) {$key = Str_split ($key) ; $this->arc[] = new Edgearc ($key [0], $key [1], $value); }}//pairs of edge arrays by weight value public function Sortarc () {$this->quicklysort (0, Count ($this->arc)-1, $this->arc); Return $this->arc; }//Using the Fast row private function Quicklysort ($begin, $end, & $item) {if ($begin < 0 ($begin >= $end)) return; $key = $this->excutesort ($begin, $end, $item); $this->quicklysort (0, $key-1, $item); $this->quicklysort ($key + 1, $end, $item); } Private Function Excutesort ($begin, $end, & $item) {$key = $item [$begin]; $left = Array (); $right = Array (); for ($i = ($begin + 1), $i <= $end, $i + +) {if ($item [$i]->getweight () <= $key->getweight ()) {$le ft[] = $item [$i]; } else {$right [] = $item [$i]; }} $return = $this->unio ($left, $right, $key); $k = 0; for ($i = $begin; $i <= $end; $i + +) {$item [$i] = $return [$k]; $k + +; } return $begin + count ($left); } Private Function Unio ($left, $right, $key) {return Array_merge ($left, Array ($key), $right); }//kruscal algorithm Public function kruscal () {$this->krus = array (); $this->sortarc (); foreach ($this->vexs as $value) {$this->krus[$value] = "0"; } foreach ($this->arc as $key = + $value) {$begin = $this->findroot ($value->getbegin ()); $end = $this->findroot ($value->getend ()); if ($begin! = $end) {$this->krus[$begin] = $end; echo $value->getbegin (). "-" . $value->getend (). ":" . $value->getweight (). "\ n"; }}}//Find the tail node of the subtree Private function FindRoot ($node) {while ($this->krus[$node]! = "0") {$node = $this-> ; krus[$node]; } return $node; }}?>
Interested readers can debug run this article Kruskal algorithm example, I believe there will be a new harvest.
Kruskal algorithm
Are you sure you want to use the adjacency table? Because in the Kruskal algorithm only needs to store the edge and the expense, with the adjacency table significance is not good, is not bad sorts.
The following Kruskal algorithm is given to calculate the minimum cost of generating the network, and the path in the generated network is output.
#include
#include
using namespace Std;
int p[1001],rank[1001];
int cho[1001];
struct Edge
{
The int u,v,w;//u represents the starting point number, V represents the end number, and W indicates the path cost
}E[15001];
int n,m;//n represents the number of points, m represents the number of paths
void Init ()
{
int i;
for (i=1;i<=n;i++)
{
P[i]=i;
rank[i]=0;
}
}
BOOL CMP (Edge A,edge B)
{
Return A.W <>
}
int Find (int t)
{
if (p[t]!=t)
{
P[t]=find (P[t]);
}
return p[t];
}
int Union (int a,int b)
{
int x, y;
X=find (a);
Y=find (b);
if (Rank[x]>rank[y])
{
P[y]=x;
}
Else
{
P[x]=y;
if (Rank[x]==rank[y])
rank[y]++;
}
return 0;
}
int main ()
{
scanf ("%d%d", &n,&m);
int i,j;
for (i=0;i <>
{
scanf ("%d%d%d", &E[I].U,&E[I].V,&E[I].W);
}
Init ();
Sort (e,e+m,cmp);
int cnt=0,ans=0;
for (i=0;i <>
{
if (Find (e[i].u)!=find (E[I].V))
{
cnt++;
ANS+=E[I].W;
Union (E[I].U,E[I].V);
Cho[++cho[0]]=i;
if (cnt==n-1)
Break
}
}
printf ("%d\n", ans);
for (j=1;j<=cho[0];j++)
{
printf ("%d%d\n", E[CHO[J]].U,E[CHO[J]].V);
}
return 0;
}... Remaining full text >>
How to implement Kruskal algorithm?
The best combination of specific topics to achieve, I have a topic here, there is a complete code, slowly understand is the blog.csdn.net/...751786
There's a lot of it, and it's interesting to see
http://www.bkjia.com/PHPjc/868246.html www.bkjia.com true http://www.bkjia.com/PHPjc/868246.html techarticle PHP Implementation Kruskal Algorithm instance resolution, Kruskal algorithm example shows the implementation of PHP Gruscal algorithm (kruscal) Realization method, to share for everyone ...