Step-by-step write algorithm (Kruskal algorithm)

Source: Internet
Author: User

Original: Step by step write algorithm (Kruskal algorithm)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


The Kruskal algorithm is an algorithm for calculating the minimum spanning tree. and prim algorithm (upper, middle, lower) according to the method of searching by the node is different, the Kruskal algorithm is carried out according to the specific segment. Now let's assume that a graph has m nodes, n edges. First of all, we need to think of M nodes as M independent spanning trees, and the n edges are arranged in small to large data. In the N-bar, we take each of these sides in turn, and if we find that the two nodes of the Edge are located on two trees, merge the two trees into a tree; if the tree has two nodes on the same tree, ignore the edge and continue running. When all the edges are traversed, if all spanning trees can be merged into a spanning tree, then it is the smallest spanning tree we need to find, and the other is not the smallest spanning tree.

The algorithm above may sound confusing, and we can use an example to illustrate

/**          9*    D-----------*  3 |           | *    |      6    |*    A  -------  B *    |           | *    |   7       | 5*    -------C----**/
Now there are so 4 points. Where a-d is 3, a-c is 7,a-b for 6,b-d 9,b-c is 5, the following begins to calculate, we first default all the points are separate minimum spanning tree,

/**          *    D *         *    A           B *         *          c**/
The first step, in order from small to large, we join the smallest side a-d,

/**          *    D *  3 |      *    |      *    A           B * * *           c**/
Then we find that the smallest side below is b-c,
/**          *    D *  3 |      *    |      *    A           B *                |*                | 5*           C----**/
Next, we find that the smallest edge is a-B, because point A and point A are above different minimum spanning trees, so we continue to merge,

/          * *    D *  3 |      *    |     6 *    A----------B *                |*                | 5*           C----**/
Next, we will also traverse the a-c,b-d, but we find that the nodes at this point have been traversed, so we all ignore the structure of the minimum spanning tree is the above content.

So the minimum spanning tree data structure is what, should be defined, do not know friends remember? We have discussed this in the prim algorithm,

/* Direct attached edge */typedef struct _dir_line{int start;int end;int weight;struct _dir_line* Next;} dir_line;/* minimum spanning tree */typedef struct _mini_generate_tree{int node_num;int line_num;int* pnode;dir_line* pLine;} mini_generate_tree;/* node Edge information */typedef struct _line{int end;int weight;struct _line* Next;} line;/* node Information */typedef struct _vectex{int start;int number; line* neighbor;struct _vectex* Next;} vectex;/* Figure Information */typedef struct _graph{int count; vectex* Head;} GRAPH;


"Not finished, to be Continued"

Step-by-step write algorithm (Kruskal algorithm)

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.