# Data Structure: Minimum Spanning Tree-Kruskal Algorithm

Source: Internet
Author: User

Kruskal Algorithm

Kruskal Algorithm

Another common algorithm for solving the minimal spanning tree is the Kruskal algorithm, which is more intuitive than the prim algorithm. Intuitively, the Kruskal algorithm selects the smallest weight from the remaining edge each time. Of course, the existing edge cannot generate a loop.

Manual solving will find that the Kruskal algorithm is very simple. The following is an example.

First, right values of the edge are sorted in order: 1 () 2 () 4 () 6 () 8 () 10 () 12) 15 (4, 5) 20 (0, 1)

The first choice is edge 1 (), 2 (), 4 (), 6 ().

Obviously, if the selected side 8 () has a ring, you must discard 8 () and select the next 10 (). The figure becomes like this.

Obviously, 12 () is also not desirable. 15 () is selected, the number of edges has reached the requirement, and the algorithm ends. The final figure is like this.

Algorithm logic is easy to understand, but it is tricky to use code to determine whether the current edge will cause the appearance of the ring.

Algorithm Description

In order to judge the appearance of the ring, we can understand the Kruskal algorithm from another angle: in the initial stage, n vertices in the graph are considered as independent n connected components. From the tree perspective, N root nodes. The edge selection standard is as follows: if the two vertices on the edge belong to two different connected components, this edge can be used; otherwise, the edge with the smallest weight is considered.

So the question comes again. How can we determine whether two vertices belong to the same connected component? This can be solved by referring to and querying the set. The idea is: if the root nodes of the two vertices are the same, they obviously belong to the same connected component. This also implies that the parent node should be updated when a new edge is added. For details, refer to the Code:

Code

`# Include <iostream> # include <algorithm> using namespace STD; # define maxweight 100/* global variable numv vertex number nume Edge Number */INT numv, nume; // edge typedef struct edge_tag {int tail; int head; int weight;} edge; // Detect Edge bool checkedge (INT tail, int head, int weight) {If (tail = head | tail <0 | tail> = numv | HEAD <0 | HEAD> = numv | weight <= 0 | weight> = maxweight) return false; return true;} // input edge void inputedge (edge * edge, int nu Me) {int I, tail, Head, weight; cout <"Start, end, and weight of the input edge" <Endl; I = 0; while (I <nume) {CIN> tail> head> weight; while (! Checkedge (tail, Head, weight) {cout <"input error! Re-enter "<Endl; CIN> tail> head> weight;} edge [I]. tail = tail; edge [I]. head = head; edge [I]. weight = weight; I ++ ;}} int CMP (const void * edge1, const void * edge2) {return (edge *) edge1) -> weight-(edge *) edge2)-> weight ;} // common operations of the query set/* compression query to find the root node of the Child */INT find (INT child, int * parent) {If (parent [child] = Child) return child; parent [child] = find (parent [child], parent); // compression path return parent [child];} // merge bool Union (edge * E, int * parent, int * Childs) {// if it is in the same tree, it cannot be merged. Otherwise, the ring int root1 and root2 may appear; root1 = find (e-> tail, parent); root2 = find (e-> head, parent); If (root1! = Root2) {// merge the small tree to the root of the big tree. If (Childs [root1]> Childs [root2]) {parent [root2] = root1; childs [root1] + = Childs [root2] + 1;} else {parent [root1] = root2; Childs [root2] + = Childs [root2] + 1 ;} return true;} return false;}/* obtain the minimum spanning tree using the Kruskal algorithm */void Kruskal (INT numv, int nume) {// edge * edge = new edge [nume]; inputedge (edge, nume ); /* parent [I] is the parent vertex of vertex I. Childs [I] is the child of vertex I. the plural number of child is children, here Childs is fabricated */int * parent = new in T [numv]; int * Childs = new int [numv]; // when initializing for (INT I = 0; I <numv; I ++, each vertex is the root, and no child sets the parent node of each vertex as its own subscript. It also indicates the category */parent [I] = I; Childs [I] = 0 ;} // sort edges by weight values from small to qsort (edge, nume, sizeof (edge), CMP); int I, count; I = COUNT = 0; cout <"Minimum Spanning Tree edge... "<Endl; while (I <nume) {If (Union (& edge [I], parent, Childs) {cout <edge [I]. tail <"---" <edge [I]. head <Endl; count ++;} If (COUNT = numv-1) // The number of edges reaches Require break; I ++;} If (count! = Numv-1) cout <"this graph is not a connected graph! The minimum spanning tree cannot be formed. "<Endl; Delete [] edge; Delete [] parent; Delete [] Childs;} // check the number of vertices and edges bool checkve (INT numv, int nume) {If (numv <= 0 | nume <= 0 | nume> numv * (numv-1)/2) return false; return true;} int main () {cout <"******** Kruskal ***** by David *****" <Endl; cout <"Number of input vertices and edges "; cin> numv> nume; while (! Checkve (numv, nume) {cout <"there is a problem with the input data! Re-enter "; CIN> numv> nume;} cout <Endl <" Kruskal... "<Endl; Kruskal (numv, nume); System (" pause "); Return 0 ;}`
Run

If this is helpful, try again!

Column directory:

• Data Structure and algorithm directory
• C pointer

Related Keywords:

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.