Minimum Spanning Tree in Graph Theory -- Kruskal Algorithm

Source: Internet
Author: User

The Kruskal algorithm is edge-dominated and always selects the edge of the currently available minimum weight.

The main idea is

1: Set the connection network of N vertices to G (V, E). First, construct a non-connected graph T = (V, empty set) with only n vertices without edges ), each vertex is connected by a grid.

2: When an edge with the minimum weight is selected in E, if the two vertices of the edge fall on different connected components, the edge is added to T, otherwise, the edge will be permanently removed and will not be used in the future, and a new edge with the smallest weight will be selected.

3: This is repeated until all vertices are on the same connected component.

The Kruskal algorithm has two key steps:

 

1: select the edge with the smallest current weight from E, during implementation, the minimum heap can be used to store all edges in E or the structure can be used to store the information of all edges (two vertices and weights of edges) in an edge array of the structure type, and sort by weight from small to large, and then select each edge in order

2: After selecting the edge with the smallest weight value, determine whether the two vertices belong to the same connected component. If so, remove them, if not, combine the connected components of the two vertices into a connected component. During implementation, you can use the query set to determine whether two vertices belong to the same connected component and combine the two connected components into one connected component.

Next we will introduce and query the set:

 

The query set is used to determine whether two elements belong to the same set, and merge the two sets into a set.

The query set has two operations: Search and merge during processing. To implement and query the set description and implementation, the elements added to the set are usually represented as a tree structure, this set is represented by the serial number of the root node. Therefore, an array of parent [] is defined, for example, parent [4] = 5, which means that the father of node 4 is node 5, parent [7] =-4 indicates the root node of the set on node 7 and the set has four elements.

There are three main functions to implement and query the set. The Code is as follows:

I:

Void ufset () // initialization {for (I = 1; I <= N; I ++) parent [I] =-1 ;}

 

II:

Int find (int x) // query and return the root node {int s; // query position for (S = x; parent [s]> = 0; S = parent [s]); While (s! = X) // optimization scheme... compression path to accelerate subsequent search operations {int TMP = parent [X]; parent [x] = s; X = TMP;} return s ;}

 

The so-called compression path is to add a while loop. Each time the node from node X to the root node of the set is directly set as the child node of the root node. Although the time is increased, the subsequent search will be faster.

III:

// Merge the elements of two different sets to connect any two elements of the two sets.

Void Union (INT R1, int R2) {int R1 = find (R1), R2 = find (R2); // R1 is the root node of R1, the root node where R2 is R2 int TMP = parent [R1] + parent [R2]; // sum of the number of nodes in the two sets (negative) // if the number of Tree nodes where R2 is located, the number of Tree nodes where R1 is located (note that parent [R1] is a negative number) if (parent [R1]> parent [R2]) // optimization scheme ..... weighting law {parent [R1] = R2; // use the tree where R1 is located as the Child tree of R2. parent [R2] = TMP; // update the parent [] value of R2.} else {parent [R2] = R1; // use the tree of R2. as the Child tree of R1. parent [R1] = TMP; // update the parent [] value of R1 }}

 

The following is the main code of the Kruskal algorithm:

Void Kruskal () {int sumweight = 0; // weight of the Spanning Tree int num = 0; // number of selected edges int U, V; ufset (); // initialize parent [] for (I = 0; I <m; I ++) {u = edges [I]. u; V = edges [I]. v; If (find (u )! = Find (V) // determines whether it belongs to the same connected component {cout <u <''<v <'' <edges [I]. W <Endl; sumweight + = edges [I]. w; num ++; Union (u, v); // merge two sets} If (Num> = N-1) break ;} cout <"weight of MST is" <sumweight <Endl ;}

 

Perfect addition:

I:

Edge representation

Struct edge {// edge
Int U, V, W; // vertex of the edge, weight
} Edges [maxm];

Ii. Sort edge weights in ascending order using the sort function

Int CMP (edge a, edge B)
{
Return A. W <B. W;
}

The template is generally used. If it is better, please kindly advise me.

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.