Use PHP to parse the cruise Card Algorithm instance and the cruise Card Algorithm instance

Source: Internet
Author: User

Use PHP to parse the cruise Card Algorithm instance and the cruise Card Algorithm instance

This example shows how to implement the kruscal algorithm implemented by PHP. I believe it will be of some reference value to everyone's PHP program design.

The Code is as follows:

<?phprequire 'edge.php';$a = array(  'a',  'b',  'c',  'd',  'e',  'f',  'g',  'h',  'i');$b = array(  'ab' => '10',  'af' => '11',  'gb' => '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');$test = new Edge($a, $b);print_r($test->kruscal());?>

The code of the edge. php file is as follows:

<? Php // Edge class EdgeArc {private $ begin; // start point private $ end; // end point private $ weight; // public function EdgeArc ($ 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 {// implementation diagram of the number of Edge sets private $ vexs; // Vertex set private $ arc; // edge set private $ arcData; // edge information of the graph to be constructed private $ krus; // when the kruscal algorithm stores Forest Information, public function Edge ($ vexsData, $ arcData) {$ this-> vexs = $ vexsData; $ this-> arcData = $ arcData; $ this-> createArc ();} // create an edge private function createArc () {foreach ($ this-> arcData as $ key => $ value) {$ key = str_split ($ key); $ this-> arc [] = new EdgeArc ($ key [0], $ key [1], $ value );}} // sort the edge array by weight. public function SortArc () {$ this-> quicklySort (0, count ($ this-> arc)-1, $ this-> arc); return $ this-> arc ;} // use 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]; $ l Eft = array (); $ right = array (); for ($ I = ($ begin + 1); $ I <= $ end; $ I ++) {if ($ item [$ I]-> getWeight () <= $ key-> getWeight () {$ left [] = $ 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_mer Ge ($ 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 end node of the subtree private function findRoot ($ node) {while ($ this-> krus [$ node]! = "0") {$ node = $ this-> krus [$ node] ;}return $ node ;}}?>

If you are interested, you can debug and run the cruise Kar algorithm example in this article. I believe there will be new gains.


Cruise Karl Algorithm

Are you sure you want to use the adjacent table? Because in the cruise Karl algorithm, you only need to store edges and costs. Using an adjacent table is of little significance and sorting is not good.
The following describes and queries the kicar algorithm, calculates the minimum cost of the generated network, and outputs the path of the generated network.
# Include <iostream>
# Include <algorithm>
Using namespace std;
Int p [1001], rank [1001];
Int cho [1001];
Struct edge
{
Int u, v, w; // u indicates the start point number, v indicates the end point number, and w indicates the cost of the path.
} E [1, 15001];
Int n, m; // n indicates the number of points, and m indicates 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 <B. 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 ();
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", & n, & m );
Int I, j;
For (I = 0; I <m; I ++)
{
Scanf ("% 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 <m; 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 \ n", e [cho [j]. u, e [cho [j]. v );
}
Return 0;
}... Remaining full text>

How to Implement the cruise card algorithm?

It is best to combine the specific implementation of the question. I have a question here, which contains the complete code, so I can understand it slowly. blog.csdn.net/...751786
There are many other items in it. You can check them if you are interested.

Contact Us

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.

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.